Ruru (Rust + Ruby)
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.
Contents
Examples
The famous String#blank?
method
The fast String#blank?
implementation by Yehuda Katz
extern crate ruru;
use ;
methods!;
pub extern
Simple Sidekiq-compatible server
Safe conversions
Since 0.8.0 safe conversions are available for built-in Ruby types and for custom types.
Let's imagine that we are writing an HTTP server. It should handle requests which are passed from Ruby side.
Any object which responds to #body
method is considered as a valid request.
extern crate ruru;
use Error;
use ;
class!;
class!;
methods!;
pub extern
Wrapping Rust data to Ruby objects
Wrap Server
s to RubyServer
objects
extern crate ruru;
extern crate lazy_static;
use ;
// The structure which we want to wrap
wrappable_struct!;
class!;
methods!;
True parallelism
Ruru provides a way to enable true parallelism for Ruby threads by releasing GVL (GIL).
It means that a thread with released GVL runs in parallel with other threads without being interrupted by GVL.
Current example demonstrates a "heavy" computation (2 * 2
for simplicity) run in parallel.
extern crate ruru;
use ;
class!;
methods!;
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.
extern crate ruru;
use Error;
use ;
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
Class definition DSL
.define;
new
Which corresponds to the following Ruby code:
= .freeze
attr_reader :reader
# ...
end
# ...
end
# ...
end
end
end
See documentation for Class
and Object
for more information.
Calling Ruby code from Rust
Getting an account balance of some User
whose name is John and who is 18 or 19 years old.
default_balance = 0
account_balance = User
.find_by(age: [18, 19], name: )
.account_balance
account_balance = default_balance unless account_balance.is_a?(Fixnum)
extern crate ruru;
use ;
Check out Documentation for many 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.
It is recommended to use Thermite gem, a Rake-based helper for building and distributing Rust-based Ruby extensions.
To be able to use Ruru, make sure that your Ruby version is 2.3.0 or higher.
- 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.9.0"
- Compile your library as a
dylib
[]
= ["dylib"]
- Create a function which will initialize the extension
pub extern
- Build extension
or using Thermite
- On the ruby side, open the compiled
dylib
and call the function to initialize extension
= Fiddle::dlopen()
Fiddle::Function.new(library[], [], Fiddle::TYPE_VOIDP).call
library
- Ruru is ready :heart:
Contributors are welcome!
If you have any questions, join Ruru on Gitter
License
MIT License
Copyright (c) 2015-2016 Dmitry Gritsay
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Icon is designed by Github.