Rust-Ruru: ruru — native Ruby extensions written in Rust

Ruru (Rust + Ruby)

Native Ruby extensions in Rust

Documentation Build Status Build status Gitter Code Triagers Badge


Documentation
Website

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

#[macro_use]
extern crate ruru;

use ruru::{Boolean, Class, Object, RString};

methods!(
   RString,
   itself,

   fn string_is_blank() -> Boolean {
       Boolean::new(itself.to_string().chars().all(|c| c.is_whitespace()))
   }
);

#[no_mangle]
pub extern fn initialize_string() {
    Class::from_existing("String").define(|itself| {
        itself.def("blank?", string_is_blank);
    });
}

Simple Sidekiq-compatible server

Link to the repository

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.

#[macro_use]
extern crate ruru;

use std::error::Error;
use ruru::{Class, Object, RString, VerifiedObject, VM};

class!(Request);

impl VerifiedObject for Request {
    fn is_correct_type<T: Object>(object: &T) -> bool {
        object.respond_to("body")
    }

    fn error_message() -> &'static str {
        "Not a valid request"
    }
}

class!(Server);

methods!(
    Server,
    itself,

    fn process_request(request: Request) -> RString {
        let body = request
            .and_then(|request| request.send("body", vec![]).try_convert_to::<RString>())
            .map(|body| body.to_string());

        // Either request does not respond to `body` or `body` is not a String
        if let Err(ref error) = body {
            VM::raise(error.to_exception(), error.description());
        }

        let formatted_body = format!("[BODY] {}", body.unwrap());

        RString::new(&formatted_body)
    }
);

#[no_mangle]
pub extern fn initialize_server() {
    Class::new("Server", None).define(|itself| {
        itself.def("process_request", process_request);
    });
}

Wrapping Rust data to Ruby objects

Wrap Servers to RubyServer objects

#[macro_use] extern crate ruru;
#[macro_use] extern crate lazy_static;

use ruru::{AnyObject, Class, Fixnum, Object, RString, VM};

// The structure which we want to wrap
pub struct Server {
    host: String,
    port: u16,
}

impl Server {
    fn new(host: String, port: u16) -> Self {
        Server {
            host: host,
            port: port,
        }
    }

    fn host(&self) -> &str {
        &self.host
    }

    fn port(&self) -> u16 {
        self.port
    }
}

wrappable_struct!(Server, ServerWrapper, SERVER_WRAPPER);

class!(RubyServer);

methods!(
    RubyServer,
    itself,

    fn ruby_server_new(host: RString, port: Fixnum) -> AnyObject {
        let server = Server::new(host.unwrap().to_string(),
                                 port.unwrap().to_i64() as u16);

        Class::from_existing("RubyServer").wrap_data(server, &*SERVER_WRAPPER)
    }

    fn ruby_server_host() -> RString {
        let host = itself.get_data(&*SERVER_WRAPPER).host();

        RString::new(host)
    }

    fn ruby_server_port() -> Fixnum {
        let port = itself.get_data(&*SERVER_WRAPPER).port();

        Fixnum::new(port as i64)
    }
);

fn main() {
    let data_class = Class::from_existing("Data");

    Class::new("RubyServer", Some(&data_class)).define(|itself| {
        itself.def_self("new", ruby_server_new);

        itself.def("host", ruby_server_host);
        itself.def("port", ruby_server_port);
    });
}

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.

#[macro_use] extern crate ruru;

use ruru::{Class, Fixnum, Object, VM};

class!(Calculator);

methods!(
    Calculator,
    itself,

    fn heavy_computation() -> Fixnum {
        let computation = || { 2 * 2 };
        let unblocking_function = || {};

        // release GVL for current thread until `computation` is completed
        let result = VM::thread_call_without_gvl(
            computation,
            Some(unblocking_function)
        );

        Fixnum::new(result)
    }
);

fn main() {
    Class::new("Calculator", None).define(|itself| {
        itself.def("heavy_computation", heavy_computation);
    });
}

Defining a new class

Let's say you have a Calculator class.

class Calculator
  def pow_3(number)
    (1..number).each_with_object({}) do |index, hash|
      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.

#[macro_use]
extern crate ruru;

use std::error::Error;
use ruru::{Class, Fixnum, Hash, Object, VM};

class!(Calculator);

methods!(
    Calculator,
    itself,

    fn pow_3(number: Fixnum) -> Hash {
        let mut result = Hash::new();

        // Raise an exception if `number` is not a Fixnum
        if let Err(ref error) = number {
            VM::raise(error.to_exception(), error.description());
        }

        for i in 1..number.unwrap().to_i64() + 1 {
            result.store(Fixnum::new(i), Fixnum::new(i.pow(3)));
        }

        result
    }
);

#[no_mangle]
pub extern fn initialize_calculator() {
    Class::new("Calculator", None).define(|itself| {
        itself.def("pow_3", pow_3);
    });
}

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()

Class::from_existing("Calculator").define(|itself| {
    itself.def("pow_3", pow_3);
});

Class definition DSL

Class::new("Hello", None).define(|itself| {
    itself.const_set("GREETING", &RString::new("Hello, World!").freeze());

    itself.attr_reader("reader");

    itself.def_self("greeting", greeting);
    itself.def("many_greetings", many_greetings);

    itself.define_nested_class("Nested", None).define(|itself| {
        itself.def_self("nested_greeting", nested_greeting);
    });
});

Which corresponds to the following Ruby code:

class Hello
  GREETING = "Hello, World".freeze

  attr_reader :reader

  def self.greeting
    # ...
  end

  def many_greetings
    # ...
  end

  class Nested
    def self.nested_greeting
      # ...
    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: 'John')
  .account_balance

account_balance = default_balance unless account_balance.is_a?(Fixnum)
#[macro_use]
extern crate ruru;

use ruru::{Array, Class, Fixnum, Hash, Object, RString, Symbol};

fn main() {
    let default_balance = 0;
    let mut conditions = Hash::new();

    conditions.store(
        Symbol::new("age"),
        Array::new().push(Fixnum::new(18)).push(Fixnum::new(19))
    );

    conditions.store(
        Symbol::new("name"),
        RString::new("John")
    );

    // Fetch user and his balance
    // and set it to 0 if balance is not a Fixnum (for example `nil`)
    let account_balance =
        Class::from_existing("User")
            .send("find_by", vec![conditions.to_any_object()])
            .send("account_balance", vec![])
            .try_convert_to::<Fixnum>()
            .map(|balance| balance.to_i64())
            .unwrap_or(default_balance);
}

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.

  1. Your local MRI copy has to be built with the --enable-shared option. For example, using rbenv:
CONFIGURE_OPTS=--enable-shared rbenv install 2.3.0
  1. Add Ruru to Cargo.toml
[dependencies]
ruru = "0.9.0"
  1. Compile your library as a dylib
[lib]
crate-type = ["dylib"]
  1. Create a function which will initialize the extension
#[no_mangle]
pub extern fn initialize_my_app() {
    Class::new("SomeClass");

    // ... etc
}
  1. Build extension
$ cargo build --release

or using Thermite

$ rake thermite:build
  1. On the ruby side, open the compiled dylib and call the function to initialize extension
require 'fiddle'

library = Fiddle::dlopen('path_to_dylib/libmy_library.dylib')

Fiddle::Function.new(library['initialize_my_app'], [], Fiddle::TYPE_VOIDP).call
  1. Ruru is ready ❤️

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.

Comments

  • Ruby 2.5 Warnings on Data classes
    Ruby 2.5 Warnings on Data classes

    Mar 14, 2018

    The docs recommend inheriting from Data following this idiom:

    let data_class = Class::from_existing("Data");
    Class::new("MyClass", Some(&Data)).define(...)
    

    On Ruby 2.5, this prints warning: constant ::Data is deprecated every time Data is accessed. I guess that there is some way to access this via the C extension API but I haven't found it yet.

    Reply
  • Why is the `RString` API using Rust's `String`/`str` instead of `Vec<u8>`/`[u8]`?
    Why is the `RString` API using Rust's `String`/`str` instead of `Vec`/`[u8]`?

    May 20, 2018

    My understanding is that Ruby's strings are sequences of arbitrary bytes, even if the associated encoding is UTF-8. So the natural mapping to Rust would be as [u8] and Vec<u8> instead of str and String for most purposes. A couple of helpers using str should be fine (e.g. the from_utf8 function).

    Functions which convert a ruby string to &str or String without verifying UTF-8 validity must be marked unsafe, otherwise they're unsound.

    Reply
  • String methods to convert to &[u8] and Vec<u8>
    String methods to convert to &[u8] and Vec

    Jun 16, 2018

    Resolves #97

                                                                                                                                                                                                           
    Reply
  • Project forked and continued at danielpclark/rutie
    Project forked and continued at danielpclark/rutie

    Dec 20, 2018

    Ruru has been inactive for some time so an official fork has been made called Rutie . Much additional work and development has been done now in Rutie and it's actively maintained.

    Reply
  • worker thread cleanup and `Drop`
    worker thread cleanup and `Drop`

    Feb 26, 2019

    I haven't been able to find any documentation about how worker threads which don't interact directly with ruby objects are handled in terms of Drop. In my case I have existing rust code which establishes a long-running thread worker to handle incoming "jobs" via a channel. The thread "lives" in a struct that implements Drop, which tells the thread to stop waiting for new messages and terminate when it's dropped (glossing over details here). In this case, my expectation is the rust/ruru interface to ruby can cleanly send data (perhaps copied data) from ruby over the channel to the thread worker. Would the thread worker need to be setup with the Thread struct in ruru (Thread::call_without_gvl)? Or could I use std::thread? Will Drop be called when the parent struct is destroyed?

    Thanks for any help you can provide.

    Reply
  • RString utf8 fixes
    RString utf8 fixes

    Jun 6, 2019

    Closes #67 as per https://github.com/d-unseductable/ruru/issues/67#issuecomment-353020580

    Reply
  • feat(vm): add thread call gvl functions
    feat(vm): add thread call gvl functions

    Aug 24, 2016

    add thread call gvl functions from ruby vm.

    Reply
  • Add multiple methods to Array
    Add multiple methods to Array

    Oct 6, 2016

    • Bump version to 0.9.0
    • Add unshift, shift, dup, to_s and reverse to Array
    • Update ruby-sys required version to 0.3.0

    I would like to add some tests. I was thinking of creating a ruby project and add rspec tests. What do you think @d-unseductable ?

    Reply
  • Add RString#new_utf8 and string::new_utf8
    Add RString#new_utf8 and string::new_utf8

    Apr 29, 2017

    These functions will return a new Ruby string with UTF8 encoding. This is for the issue described in d-unseductable/ruru#67

    Reply
  • links on crate homepage are broken
    links on crate homepage are broken

    May 5, 2016

    Hi there, I was just looking at https://crates.io/crates/ruby-sys and found that the homepage and documentation links are not working.

    I-bug 
    Reply
  • Create the ruby-sys crate
    Create the ruby-sys crate

    Apr 25, 2016

    This is a big PR, but it's not that big.

    Cargo has a convention of -sys packages. The idea is that the low-level stuff should be a package, and then higher-level bindings should be built on top of it.

    ruru is that kind of package, and src/unsafe_bindings is really a -sys crate. So, I made one. :smile:

    I went ahead and pushed it up, so that nobody else could squat it while we discuss this PR: https://crates.io/crates/ruby-sys If this PR is accepted, I'll make you an owner of the package as well. If you don't want this PR, well, ruby-sys should still exist, imho :smile:

    Reply
  • Attaching Rust state/data to Ruby instances
    Attaching Rust state/data to Ruby instances

    Oct 19, 2016

    Is there a clean way to attach Rust state/data to a Ruby class? Something like instance variables which are only visible to the Rust env?

    The reason I'm asking is because I'd like to expose a Rust implementation of a Ruby class, but use Rust's more robust data structures and memory management for the internal heavy lifting.

    question 
    Reply