4 releases (2 breaking)

0.3.0 Jul 12, 2021
0.2.0 Jan 20, 2021
0.1.1 Jan 4, 2019
0.1.0 Jan 3, 2019

#289 in Encoding

Download history 1018/week @ 2022-10-07 1457/week @ 2022-10-14 1159/week @ 2022-10-21 1100/week @ 2022-10-28 1023/week @ 2022-11-04 1666/week @ 2022-11-11 1738/week @ 2022-11-18 1089/week @ 2022-11-25 973/week @ 2022-12-02 1208/week @ 2022-12-09 568/week @ 2022-12-16 144/week @ 2022-12-23 1166/week @ 2022-12-30 1599/week @ 2023-01-06 1797/week @ 2023-01-13 1995/week @ 2023-01-20

6,611 downloads per month

MIT license

52KB
1K SLoC

Usage example:

Create a new rust crate and make sure to specify crate-type to be "dylib". Also add rutie, rutie-serde, serde and possibly serde_derive as a dependency.

[package]
edition = "2018"

[lib]
crate-type = ["dylib"]
name = "ruby_rust_demo"

[dependencies]
rutie = "0.8"
rutie-serde = "0.2"
serde = "1.0"
serde_derive = "1.0"

The usage is very similar to how you would use rutie on it's own, but instead of calling rutie_methods! macro, you call rutie_serde_methods!. This macro takes care of deserializing arguments and serializing return values. It also captures all panics inside those methods and raises them as an exception in ruby.

use rutie::{class, Class, Object};
use rutie_serde::{ruby_class, rutie_serde_methods};
use serde_derive::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
pub struct User {
    pub name: String,
    pub id: u64,
}

class!(HelloWorld);
rutie_serde_methods!(
    HelloWorld,
    _itself,
    ruby_class!(Exception),
    fn hello(name: String) -> String {
        format!("Hello {}", name)
    }
    fn hello_user(user: User) -> String {
        format!("Hello {:?}", user)
    }
);

#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn Init_ruby_rust_demo() {
    let mut class = Class::new("RubyRustDemo", None);
    class.define(|itself| itself.def_self("hello", hello));
    class.define(|itself| itself.def_self("hello_user", hello_user));
}

Dependencies

~530–720KB
~12K SLoC