#Accessing structs via FFI

3 messages · Page 1 of 1 (latest)

empty edge
#

Hi,

I am trying to do some FFI. On the Rust side I have this struct

#[repr(C)]
pub struct LoginResult<T>
where
    T: Default,
{
    pub item: T,   // null if none
    pub code: u32, // 0 = ok, nonzero = error
}

This is represented Deno side like so

  login: {
    parameters: ["buffer", "buffer"],
    result: { "struct": [{ "struct": ["u32", "buffer", "i32"] }, "u32"] },
  },

The login function works, but I am not sure how to safely access the struct data.

vestal estuary
#

You can't use a generic in a struct you want to share in ffi different types will give different memory layout

merry chasm
# empty edge Hi, I am trying to do some FFI. On the Rust side I have this struct ``` #[repr...

If your login API is something like:

#[repr(C)]
pub struct Data {
  a: u32,
  b: *const (), // or *mut ()
  c: i32,
}

#[no_mangle]
fn login(arg0: *mut (), arg1: *mut ()) -> LoginResult<Data>;

then that definition seems fully correct. The returned struct is stored in a libffi-allocated buffer of memory that is given to you in the returned Uint8Array: that memory is fully JS owned at that point and entirely safe to access.