From 515ebb2db14356833d3e7b70b034170978e50666 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 19 Apr 2015 10:38:48 -0700 Subject: [PATCH] Add Ruby to Rust example --- ruby-to-rust/Cargo.toml | 8 ++++++++ ruby-to-rust/Makefile | 9 +++++++++ ruby-to-rust/src/lib.rs | 4 ++++ ruby-to-rust/src/main.rb | 11 +++++++++++ 4 files changed, 32 insertions(+) create mode 100644 ruby-to-rust/Cargo.toml create mode 100644 ruby-to-rust/Makefile create mode 100644 ruby-to-rust/src/lib.rs create mode 100644 ruby-to-rust/src/main.rb diff --git a/ruby-to-rust/Cargo.toml b/ruby-to-rust/Cargo.toml new file mode 100644 index 0000000..72caf09 --- /dev/null +++ b/ruby-to-rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ruby-to-rust" +version = "0.1.0" +authors = ["Alex Crichton "] + +[lib] +name = "double_input" +crate-type = ["dylib"] diff --git a/ruby-to-rust/Makefile b/ruby-to-rust/Makefile new file mode 100644 index 0000000..582f0ca --- /dev/null +++ b/ruby-to-rust/Makefile @@ -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 diff --git a/ruby-to-rust/src/lib.rs b/ruby-to-rust/src/lib.rs new file mode 100644 index 0000000..bf573d1 --- /dev/null +++ b/ruby-to-rust/src/lib.rs @@ -0,0 +1,4 @@ +#[no_mangle] +pub extern fn double_input(input: i32) -> i32 { + input * 2 +} diff --git a/ruby-to-rust/src/main.rb b/ruby-to-rust/src/main.rb new file mode 100644 index 0000000..e106332 --- /dev/null +++ b/ruby-to-rust/src/main.rb @@ -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}"