summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_ext_cargo_builder/rust_ruby_example/src/lib.rs
blob: 0626f04e0f6dd21b48370904c4cf33e0b1691fed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
extern crate rb_sys;

use rb_sys::{
    rb_define_module, rb_define_module_function, rb_string_value_cstr, rb_utf8_str_new, VALUE,
};
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_long};

#[inline]
unsafe fn cstr_to_string(str: *const c_char) -> String {
    CStr::from_ptr(str).to_string_lossy().into_owned()
}

#[no_mangle]
unsafe extern "C" fn pub_reverse(_klass: VALUE, mut input: VALUE) -> VALUE {
    let ruby_string = cstr_to_string(rb_string_value_cstr(&mut input));
    let reversed = ruby_string.to_string().chars().rev().collect::<String>();
    let reversed_cstring = CString::new(reversed).unwrap();
    let size = ruby_string.len() as c_long;

    rb_utf8_str_new(reversed_cstring.as_ptr(), size)
}

#[cfg(rubygems)]
#[no_mangle]
pub extern "C" fn hello_from_rubygems() {}

#[cfg(rubygems_0_0_0)]
#[no_mangle]
pub extern "C" fn should_never_exist() {}

#[cfg(rubygems_x_x_x)]
#[no_mangle]
pub extern "C" fn hello_from_rubygems_version() {}

#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn Init_rust_ruby_example() {
    let name = CString::new("RustRubyExample").unwrap();
    let function_name = CString::new("reverse").unwrap();
    // bindgen does not properly detect the arity of the ruby callback function, so we have to transmute
    let callback = unsafe {
        std::mem::transmute::<
            unsafe extern "C" fn(VALUE, VALUE) -> VALUE,
            unsafe extern "C" fn() -> VALUE,
        >(pub_reverse)
    };
    let klass = unsafe { rb_define_module(name.as_ptr()) };

    unsafe { rb_define_module_function(klass, function_name.as_ptr(), Some(callback), 1) }
}