Add Ruby to Rust example
This commit is contained in:
8
ruby-to-rust/Cargo.toml
Normal file
8
ruby-to-rust/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "ruby-to-rust"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Alex Crichton <alex@alexcrichton.com>"]
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "double_input"
|
||||||
|
crate-type = ["dylib"]
|
||||||
9
ruby-to-rust/Makefile
Normal file
9
ruby-to-rust/Makefile
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
all: target/libdouble_input.so
|
||||||
|
ruby src/main.rb
|
||||||
|
|
||||||
|
target/libdouble_input.so: src/lib.rs Cargo.toml
|
||||||
|
cargo build
|
||||||
|
(cd target && ln -nsf debug/libdouble_input-* libdouble_input.so)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf target
|
||||||
4
ruby-to-rust/src/lib.rs
Normal file
4
ruby-to-rust/src/lib.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#[no_mangle]
|
||||||
|
pub extern fn double_input(input: i32) -> i32 {
|
||||||
|
input * 2
|
||||||
|
}
|
||||||
11
ruby-to-rust/src/main.rb
Normal file
11
ruby-to-rust/src/main.rb
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
require 'ffi'
|
||||||
|
|
||||||
|
module Hello
|
||||||
|
extend FFI::Library
|
||||||
|
ffi_lib 'target/libdouble_input.so'
|
||||||
|
attach_function :double_input, [ :int ], :int
|
||||||
|
end
|
||||||
|
|
||||||
|
input = 4
|
||||||
|
output = Hello.double_input(input)
|
||||||
|
puts "#{input} * 2 = #{output}"
|
||||||
Reference in New Issue
Block a user