-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issues with more complex types #7
Comments
It turns out structs do work if you change the representation to that of a C struct. You need the following code: #[repr(C)]
pub struct Test {
pub x: i32,
}
#[no_mangle]
pub unsafe extern "C" fn a_function_from_rust() -> Test {
Test {x: 1}
} The I'm gonna update this issue as I discover more and keep it open until things are documented. |
@runfalk For the first issue, you can't return a Something like this: #[no_mangle]
pub unsafe extern "C" fn vec_ptr_from_rust() -> *const u8 {
let v = vec![1, 2, 3];
let v_ptr = v.as_ptr();
mem::forget(v);
v_ptr
} You need to forget the When you're done using the object in Python (e.g. loading it into a numpy array), you'll need to free the memory by exposing another FFI function that calls: |
@compressed thank you for your reply. I did figure out a similar solution. I'm using it for a C-wrapper around a rust raw image parsing library: https://github.com/runfalk/pyrawloader/blob/master/rust/src/lib.rs There's an example on how to do the de-allocation function too. @mitsuhiko would you be interested in a PR with some basic documentation on how to pass common types? If so, which format? |
I tried out the library but have been unable to use anything but the simplest types (
i32
andbool
works). I copied the example project and tested different return values. To compile I usepython setup.py build develop
This gives me an error that the function doesn't exist:
The contents of
_rawloader__ffi.py
looks like this:The second issue is with structs:
I get the following error message:
The contents of
_rawloader__ffi.py
looks like this:As far as I can see there is no information about the
x
field in the struct.The text was updated successfully, but these errors were encountered: