Search code examples
rustffi

Is it possible to call a Rust function taking a Vec from C?


Suppose I have the following Rust library:

// lib.rs
#![crate_type = staticlib]

#[no_mangle]
pub extern fn do_something(number: i32) {
    // something
}

#[no_mangle]
pub extern fn do_something_else(collection: &Vec<i32>) {
    // something 
}

I know that, to call do_something from C, I'd just need to declare an extern function taking an int32_t, but is it possible to call do_something_else? If so, how?


Solution

  • You can, but the better question is should you?

    Since you cannot construct a Vec from C, you'd have to construct it in Rust and then return a pointer to C. C code would own the pointer to the Vec and would then pass it back when calling do_something_else.

    Then there's the problem that you can't really modify the Vec in C either, other than by creating new FFI methods that mirror all of the Rust methods.

    You also probably shouldn't take a &Vec<i32> because Rust references are guaranteed to not be NULL, and there's nothing that enforces that when called from C. It's better to take a *const Vec<i32>, assert that it's non-NULL and convert it to a reference.

    Chances are that you want to accept a C array through the FFI boundary. C arrays are a pointer and a length, so you'd accept both and reconstitute a Rust slice (since you wouldn't own the array):

    use std::slice;
    
    pub extern fn do_something_else(p: *const i32, len: libc::size_t) {
        let slice = unsafe {
            assert!(!p.is_null());
            slice::from_raw_parts(p, len)
        };
    }
    

    Obligatory link to The Rust FFI Omnibus.


    If you really needed to do what you asked, it would probably look something like this:

    extern crate libc;
    
    #[no_mangle]
    pub extern fn make_vec() -> *mut Vec<i32> {
        Box::into_raw(Box::new(Vec::new()))
    }
    
    #[no_mangle]
    pub extern fn add_number(vec: *mut Vec<i32>, val: libc::int32_t)  {
        let vec = unsafe {
            assert!(!vec.is_null());
            &mut *vec
        };
    
        vec.push(val);    
    }
    
    #[no_mangle]
    pub extern fn print_vec(vec: *const Vec<i32>)  {
        let vec = unsafe {
            assert!(!vec.is_null());
            &*vec
        };
    
        println!("{:?}", vec);    
    }
    
    #[no_mangle]
    pub extern fn drop_vec(vec: *mut Vec<i32>)  {
        unsafe {
            assert!(!vec.is_null());
            Box::from_raw(vec);
        }
    }
    

    And would be used like (untested):

    // Add extern declarations
    
    int main(int argc, char *argv[]) {
        void *v = make_vec(); // Use a real typedef here
        add_number(v, 42);
        print_vec(v);
        drop_vec(v);
    }
    

    You'd want to run this under valgrind to make sure I didn't do anything stupid memory-wise.