coffret

Crates.iocoffret
lib.rscoffret
version0.0.3
sourcesrc
created_at2022-04-30 06:19:54.134239
updated_at2022-06-06 05:16:04.943639
descriptionA crate for gems
homepage
repository
max_upload_size
id578014
size5,862
Kondo Uchio (udzura)

documentation

README

Coffret

A fancy wrapper of MRI C API.

Aiming to be nix for Ruby gem development.

Example

Before

use rb_sys::*;

fn test_show_self() // ...

#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn Init_hi_rust() {
    let name = CString::new("Rust").unwrap();
    let object = unsafe { rb_cObject };
    let klass = unsafe { rb_define_class(name.as_ptr(), object) };

    let function_name = CString::new("mostrarme").unwrap();
    let callback = unsafe {
        std::mem::transmute::<unsafe extern "C" fn(VALUE) -> VALUE, unsafe extern "C" fn() -> VALUE>(
            test_show_self,
        )
    };

    unsafe { rb_define_method(klass, function_name.as_ptr(), Some(callback), 0) }
}

After

use coffret::class;
use coffret::exception;

fn test_show_self() // ...

fn init_hi_rust_internal() -> Result<(), Box<dyn Error>> {
    let object = class::object_class();
    let klass = class::define_class("Rust", object);

    let callback = class::make_callback(&test_show_self);

    class::define_method(klass, "mostrarme", callback, 0);
    Ok(())
}

#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn Init_hi_rust() {
    match init_hi_rust_internal() {
        // Rust Error to Ruby's Exception. Isn't it cool?
        Err(e) => exception::rustly_raise(e.as_ref()),
        Ok(_) => {}
    }
}

MRI has bunch of APIs, so it is now under construction && any P/Rs are welcomed.

License

MIT.

Commit count: 0

cargo fmt