Skip to content

Instantly share code, notes, and snippets.

@jsyeo
Created May 15, 2015 08:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsyeo/fdc82851b3bcd83faf70 to your computer and use it in GitHub Desktop.
Save jsyeo/fdc82851b3bcd83faf70 to your computer and use it in GitHub Desktop.
Calling rust from ruby ffi
[package]
name = "double"
version = "0.1.0"
authors = ["Jason Yeo <jasonyeo88@gmail.com>"]
[lib]
name = "double"
crate-type = ["dylib"]
require 'ffi'
module MyFabulousLib
extend FFI::Library
ffi_lib "./target/release/libdouble.so"
attach_function :double, [:int], :int
end
puts MyFabulousLib.double(5)
puts MyFabulousLib.double(8)
#[no_mangle]
pub extern fn double(x: i32) -> i32 {
println!("Hello from rust!");
x * 2
}
#[test]
fn it_works() {
assert_eq!(double(0), 0);
assert_eq!(double(2), 4);
assert_eq!(double(-1), -2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment