You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My use case is pretty simple. I need to do rust <-> rust FFI for webassembly.
Basically, i want my app to be "scriptable" using wasm plugins. So, i would provide a bunch of functions (like drawing) from the host. When the guest plugin is loaded, it will be linked with these host functions as "imports". Then, the guest can call the host functions to draw stuff.
So, the host will have code like this:
// host.rsstructPoint{x:f32,y:f32}implPoint{/// Draw a line from self -> topubfndraw_line(&self,to:&Point){// ... host side implementation using opengl or something.}}
Then, there will be a "guest_imports.rs" crate which will just include the FFI declarations necessary. Basically, bindings generated from C headers.
// generated from host.rsstructPoint{x:f32,y:f32,}// will be provided (linked) as imports by host at load time.extern"C"{pubfnpoint_draw_line(this:*constPoint,to:*constPoint);}implPoint{/// automatically reconstruct the safe API on the guest side using the FFI bindings generated from the hostpubfndraw_line(&self,to:&Point){unsafe{point_draw_line(selfas*constPoint, to as*constPoint)}}}
Now, a guest plugin can simply depend on the above crate, with almost the same API (except with dynamically lined unsafe bindings under the hood).
Right now, trying to write rust <-> rust FFI is a horrible experience.
Write the host functionality.
use safer_ffi to generate the extern FFI fn declarations or do it manually.
If you used safer_ffi, now you need to use bindgen to convert c headers to rust.
Then, reconstruct the safe API using the FFI fns. copy paste the docs too :(
This leads to a lot of duplication and manual maintenance. For some reason, its more work to do rust <-> rust FFI than rust <-> C/Cpp FFI.
But, i was wondering if it is possible for safer_ffi to instead directly generate the rust ffi declarations (and even reconstruct the safe API using the declarations) automatically and skip the intermediary c language completely.
The text was updated successfully, but these errors were encountered:
Yes, this is indeed a desired goal of this library, hopefully implemented this year (cannot promise a shorter timeframe since I don't have that much time to dedicate to this yet). For what is worth, you may be able to give ::stabby a try: it has similar design ideas to safer-ffi, but focuses on Rust-to-Rust FFI 🙂
Nah, that's for passing ABI-stable data structures (eg: string) across FFI boundary in the same memory space. That would be useless for wasm guest <-> native host FFI, as wasm guests cannot access host memory.
All I want is for safer_ffi to reconstruct the same API on the other side using C FFI declarations. For example,
// host crate implementing the actual functionalitystructPoint{x:f32,y:f32}implPoint{fndraw_line(&self,other:&Point){// impl using skia or opengl etc. on native host}}// safer_ffi generates these declarations for guest crates (for C, C++ etc..)structPoint{x:f32,y:f32}extern"C"fndraw_line(s:*constPoint,other:*constPoint);// imported from host// crate for rust guest crates - regenerate the same safe API (like original) wrapping FFI structPoint{x:f32,y:f32}implPoint{fndraw_line(&self,other:&Point){// calling host fns linked at startup - no idea about how its implemented on hostunsafe{draw_line(selfas*constPoint, other as*constPoint)}}}
The first and second are already done by safer_ffi. Its the last part of [re]generating the safe API on top of unsafe FFI, which would help a lot here. The host still needs to export these FFI functions, but that's out of scope here.
My use case is pretty simple. I need to do rust <-> rust FFI for webassembly.
Basically, i want my app to be "scriptable" using wasm plugins. So, i would provide a bunch of functions (like drawing) from the host. When the guest plugin is loaded, it will be linked with these host functions as "imports". Then, the guest can call the host functions to draw stuff.
So, the host will have code like this:
Then, there will be a "guest_imports.rs" crate which will just include the FFI declarations necessary. Basically, bindings generated from C headers.
Now, a guest plugin can simply depend on the above crate, with almost the same API (except with dynamically lined unsafe bindings under the hood).
Right now, trying to write rust <-> rust FFI is a horrible experience.
safer_ffi
to generate the extern FFI fn declarations or do it manually.safer_ffi
, now you need to usebindgen
to convert c headers to rust.This leads to a lot of duplication and manual maintenance. For some reason, its more work to do rust <-> rust FFI than rust <-> C/Cpp FFI.
But, i was wondering if it is possible for safer_ffi to instead directly generate the rust ffi declarations (and even reconstruct the safe API using the declarations) automatically and skip the intermediary c language completely.
The text was updated successfully, but these errors were encountered: