Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Ruru (Rust + Ruby = :heart:)
Native Ruby extensions in Rust
Have you ever considered rewriting some parts of your slow Ruby application?
Just replace your Ruby application with Rust, method by method, class by class. It does not require you to change the interface of your classes or to change any other Ruby code.
As simple as Ruby, as efficient as Rust.
Examples
The famous String#blank?
method
The fast String#blank?
implementation by Yehuda Katz
methods!;
Simple Sidekiq-compatible server
Rack middleware
Set the X-RUST
header to Hello from Rust!
class!;
methods!;
pub extern
Ruby:
use RustMiddleware
Defining a new class
Let's say you have a Calculator
class.
(1..number).each_with_object({}) do
hash[index] = index ** 3
end
end
end
# ... somewhere in the application code ...
Calculator.new.pow_3(5) #=> { 1 => 1, 2 => 8, 3 => 27, 4 => 64, 5 => 125 }
You have found that it's very slow to call pow_3
for big numbers and decided to replace the whole class
with Rust.
class!;
methods!;
pub extern
Ruby:
# No Calculator class in Ruby anymore
# ... somewhere in the application ...
Calculator.new.pow_3(5) #=> { 1 => 1, 2 => 8, 3 => 27, 4 => 64, 5 => 125 }
Nothing has changed in the API of class, thus there is no need to change any code elsewhere in the app.
Replacing only several methods instead of the whole class
If the Calculator
class from the example above has more Ruby methods, but we want to
replace only pow_3
, use Class::from_existing()
.define;
from_existing
Calling Ruby code from Rust
Getting an account balance of some User
whose name is John and who is 18 or 19 years old.
User
.find_by(age: [18, 19], name: )
.account_balance
let mut conditions = new;
conditions.store;
conditions.store;
let account_balance =
from_existing
.send
.send
.
.to_i64;
Check out Documentation for more examples!
... and why is FFI not enough?
-
No support of native Ruby types;
-
No way to create a standalone application to run the Ruby VM separately;
-
No way to call your Ruby code from Rust;
How do I use it?
Warning! The crate is a WIP.
There are two ways of using Ruru:
-
Standalone application - Rust is run first as a compiled executable file and then it calls Ruby code (see docs for
VM::init()
) -
Running Rust code from a Ruby application
The second way requires additional steps (to be improved):
- Your local MRI copy has to be built with the
--enable-shared
option. For example, using rbenv:
CONFIGURE_OPTS=--enable-shared
- Add Ruru to
Cargo.toml
[]
= ">= 0.5.0"
- Compile your library as a
dylib
[]
= ["dylib"]
- Create a function which will initialize the extension
pub extern
- Open the library and call the function from Ruby
= Fiddle::dlopen()
Fiddle::Function.new(library[], [], Fiddle::TYPE_VOIDP).call
library
- Ruru is ready :heart: