Skip to content
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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ commands:
description: "Install correct version of bundler and load from cache or bundle install"
steps:
- run: sudo apt-get --allow-releaseinfo-change update
- run: sudo apt-get install cifs-utils unixodbc unixodbc-dev
- run: sudo apt-get install cifs-utils unixodbc unixodbc-dev cargo
- run:
name: Configure Bundler
command: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ node_modules

# SVN working copy
/subversion_eads/*
lib_jobs_rs/target
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ gem 'capistrano-yarn', '~> 2.0'
gem 'devise', '>= 4.6.0'
gem 'ed25519', '~> 1.3'
gem 'faraday', '~> 2.7'
gem 'ffi'
gem "flipflop", git: "https://github.com/voormedia/flipflop.git", ref: "0d70d8e33483a9c0282ed8d6bca9c5ccd61e61e8"
gem 'foreman'
gem 'honeybadger'
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ DEPENDENCIES
ed25519 (~> 1.3)
factory_bot_rails
faraday (~> 2.7)
ffi
flipflop!
foreman
honeybadger
Expand Down
7 changes: 6 additions & 1 deletion app/models/oclc/lc_call_slips/keyword_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Member Author

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.

ffi_lib Rails.root.join('lib_jobs_rs', 'target', 'release', "liblib_jobs.#{c_lib_extension}")
attach_function :normalize_keyword, [:string], :strptr
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normalize_keyword is the name of our rust function, and we specify that it takes a string and returns a string pointer.


def initialize(field:, keywords:)
@field = field
@keywords = keywords
Expand Down Expand Up @@ -41,7 +46,7 @@ def word_is_keyword?(word)
end

def normalize(word)
word.sub(/[[:punct:]]?$/, '')
normalize_keyword(word)[0]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the original Ruby implementation, use the normalize_keyword function that we attached earlier.

end
end
end
Expand Down
4 changes: 4 additions & 0 deletions config/initializers/cargo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true
Copy link
Member Author

Choose a reason for hiding this comment

The 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
7 changes: 7 additions & 0 deletions lib_jobs_rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions lib_jobs_rs/Cargo.toml
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]
50 changes: 50 additions & 0 deletions lib_jobs_rs/src/keyword_field.rs
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 {
Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
Copy link
Member Author

Choose a reason for hiding this comment

The 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"
);
}
}
2 changes: 2 additions & 0 deletions lib_jobs_rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod keyword_field;
pub use keyword_field::normalize_keyword;
Loading