-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[please do not merge] Rewrite one method in rust #787
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,4 @@ node_modules | |
|
||
# SVN working copy | ||
/subversion_eads/* | ||
lib_jobs_rs/target |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,11 @@ module LcCallSlips | |
# Marc::DataField for the keywords that a selector | ||
# is interested in. | ||
class KeywordField | ||
extend FFI::Library | ||
c_lib_extension = /darwin/.match?(RUBY_PLATFORM) ? 'dylib' : 'so' | ||
ffi_lib Rails.root.join('lib_jobs_rs', 'target', 'release', "liblib_jobs.#{c_lib_extension}") | ||
attach_function :normalize_keyword, [:string], :strptr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
def initialize(field:, keywords:) | ||
@field = field | ||
@keywords = keywords | ||
|
@@ -41,7 +46,7 @@ def word_is_keyword?(word) | |
end | ||
|
||
def normalize(word) | ||
word.sub(/[[:punct:]]?$/, '') | ||
normalize_keyword(word)[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of the original Ruby implementation, use the |
||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compile the rust code each time we start rails or run the tests |
||
Dir.chdir(Rails.root.join("lib_jobs_rs")) do | ||
`cargo build --release` | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "lib_jobs" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use std::ffi::{CStr, CString}; | ||
|
||
/// Make a keyword suitable for comparing to other keywords | ||
/// | ||
/// # Examples | ||
/// ``` | ||
/// use std::ffi::{CStr, CString}; | ||
/// | ||
/// let original = CString::new("My title /").unwrap().into_raw(); | ||
/// let result = unsafe { lib_jobs::normalize_keyword(original) }; | ||
/// assert_eq!(unsafe { CStr::from_ptr(result) }.to_str().unwrap(), "My title "); | ||
/// ``` | ||
/// | ||
/// # Safety | ||
/// | ||
/// Since this function reads arbitrary memory from a C pointer, | ||
/// there are some safety considerations described in | ||
/// <https://doc.rust-lang.org/std/ffi/struct.CStr.html#method.from_ptr> | ||
#[no_mangle] | ||
pub unsafe extern "C" fn normalize_keyword(raw_string_ptr: *const i8) -> *const i8 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is mainly glue code: taking the pointer that ruby provided, calling the logic of our function, and then returning a new pointer to the corrected string. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need the unsafe keyword in the function signature since the only unsafe call is wrapped in an unsafe block. We don't need to use an unsafe block in the test if this function isn't marked as unsafe. |
||
let original_string = match unsafe { CStr::from_ptr(raw_string_ptr) }.to_str() { | ||
Ok(string) => string, | ||
Err(_) => "" | ||
}; | ||
CString::new(normalize_string(original_string)) | ||
.expect("Could not create a CString, check for 0 byte errors") | ||
.into_raw() | ||
} | ||
|
||
fn normalize_string(original_string: &str) -> &str { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The actual Rust implementation of the original Ruby method |
||
match original_string.strip_suffix(|last_character: char| last_character.is_ascii_punctuation()) | ||
{ | ||
Some(cleaned) => cleaned, | ||
None => original_string, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::ffi::{CStr, CString}; | ||
#[test] | ||
fn normalize_keyword() { | ||
let original = CString::new("31 pages.").unwrap().into_raw(); | ||
let result = unsafe { super::normalize_keyword(original) }; | ||
assert_eq!( | ||
unsafe { CStr::from_ptr(result) }.to_str().unwrap(), | ||
"31 pages" | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod keyword_field; | ||
pub use keyword_field::normalize_keyword; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Annoyingly, Mac and Linux use different file extension for dynamic libraries.