Created
May 15, 2015 08:59
-
-
Save jsyeo/fdc82851b3bcd83faf70 to your computer and use it in GitHub Desktop.
Calling rust from ruby ffi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [package] | |
| name = "double" | |
| version = "0.1.0" | |
| authors = ["Jason Yeo <[email protected]>"] | |
| [lib] | |
| name = "double" | |
| crate-type = ["dylib"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[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