-
Notifications
You must be signed in to change notification settings - Fork 40
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
Possibility of exposing a Trait as a vtable in current release? #147
Comments
@danielhenrymantilla any thoughts? |
@tomleavy yes, this is definitely on If you are feeling adventurous, feel free to The idea would be:
You'll need the trait signatures to be made FFI-compatible by hand (matching #[derive_ReprC(dyn)]
pub trait FfiSomeTrait {
fn ffi_some_method(&self, input: c_slice::Ref<'_, u8>) -> repr_c::Vec<u8>; // repr_c::Result does not exist yet
fn ffi_some_other_method(&mut self) -> repr_c::String;
}
impl<T : SomeTrait> FfiSomeTrait for T {
fn ffi_some_method(&self, input: c_slice::Ref<'_, u8>) -> repr_c::Vec<u8> {
self.some_method(input.into()).into()
}
fn ffi_some_other_method(&mut self) -> repr_c::String {
self.some_other_method().into()
}
}
|
Back to a high-level view of the project, I should have this properly merged in |
This is awesome, thanks @danielhenrymantilla . Question about VirtualPtr, is that the type I would actually use when I want to utilize the trait someplace? My example calling #[ffi_export]
pub fn some_struct_new(input: VirtualPtr<dyn '_ + FfiSomeTrait>) -> Box<SomeStruct> {
Box::new(SomeStruct::new(input))
} |
Yeah, that's the idea 🙂 |
Using the https://github.com/getditto/safer_ffi/tree/dyn-traits-part-2 branch, I defined the following trait but I can't seem to get the corresponding vtable definition in the generated C header: pub trait ExampleTrait {
fn method_ref(&self, s: &str, i: i32) -> String;
fn method_mut(&mut self, s: &str, i: i32) -> String;
}
#[derive_ReprC(dyn)]
pub trait FfiExampleTrait {
fn method_ref(&self, s: char_p::Ref<'_>, i: i32) -> repr_c::String;
fn method_mut(&mut self, s: char_p::Ref<'_>, i: i32) -> repr_c::String;
} I couldn't find any use of Thank you. |
@stefunctional this may be because the FFI type that makes use of said vtable is
So try adding: // any of the two types involved should trigger the vtable definitions
#[ffi_export]
fn lets_see(
_a: VirtualPtr<dyn 'static + Send + Sync + FfiExampleTrait>,
_b: VirtualPtr<dyn '_ + FfiExampleTrait>,
)
{} |
That was it. Thank you very much for the quick answer! |
Do you need help to get the |
Hey @stefunctional sorry for the late reply 🙇 I appreciate the offer, truly, and may take you up on it for later ideas 😄 but in this case the remaining effort was mostly:
and both of these tasks were thus about dealing with my messy code, so I was the best suited for them. I have been quite busy with other stuff this past month, hence this being a bit more delayed than anticipated, but now all this has been released as |
I'm gonna close this since #155, in a way, finished fixing it, but we can keep discussing about |
No worries. Thank you very much for your work on this. I just skimmed through the change log and I am excited to try out this new version. |
I would like to take a Trait that I have and allow someone from the C side to provide an implementation of it in order to create a generic Rust struct internally. Based on the current documentation and my browsing of issues here I'm not sure I would accomplish that (even in a workaround type of way) and how that will improve with any changes that are coming.
Usage would be something like this
My assumption was that I would be able to do something like the following but I'm struggling with the specifics
The text was updated successfully, but these errors were encountered: