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
How to cast C++ IUnknown
pointer to Rust IUnknown
struct?
#1975
Comments
It's not particularly easy because it's not particularly safe. There's also the question of whether the C++ pointer is owned or dangling. Do you want the Rust If we assume you're only borrowing someone else's pointer, then it would look something like this: let raw: *mut std::ffi::c_void = ptr; // someone else will call `ptr->Release()`
let unknown: &windows::core::IUnknown = std::mem::transmute(&raw); You can now use If we assume you're taking ownership of the pointer, then it would look something like this: let raw: *mut std::ffi::c_void = ptr; // nobody else will touch `ptr` again
let unknown: windows::core::IUnknown = std::mem::transmute(raw); You can now use |
Thanks for the feedback. My case is borrowing C++ pointer and it was indeed ownership issue. I worked around it via I understood that this kind of stuff is hard (or almost impossible) to be safe but I'm wondering if there can be a higher level and a bit safer helper method. Particularly I'm working on rust binding for GStreamer D3D11 library and plugin |
Such helper API would certainly be useful, even if it has to be unsafe fn from_raw_owned(ptr: *mut c_void) -> IUnknown;
unsafe fn from_raw_borrowed<'a>(ptr: &'a *mut c_void) -> &'a IUnknown; Both could document the pre-conditions for calling these safely much clearer than |
Thanks for the suggestion! I'm just not convinced this is any safer or even any clearer. These would just wrap I'm interested in what @rylev has to say. |
You need that anyway if you want to deal with raw pointers :) The main advantage of those functions over just transmuting is that they make it clearer what is allowed to do and what not, and how memory layouts are matching. And more importantly, they're much more discoverable than knowing that you can transmute things under certain conditions. For example you could create an Similarly, the documentation would make it clear that in the In any case, these functions would still be (Note that the FWIW, in the GObject (GTK/GStreamer/etc) Rust bindings we provide such functions and people generally seem to use them correctly while that's much less often the case with transmute. |
I am absolutely in favor of these functions existing for many of the same reasons as @sdroege listed. I think any time we're requiring a transmute, we have a failure in the API design. |
Sounds good - I'll try to get to it as I have time. |
IUnknown
pointer to Rust IUnknown
struct?
Sorry for the delay - here you go: #2010 |
I'm currently trying to cast C++ IUnknown pointer (
*mut c_void
) to rust IUnknown viastd::mem::transmute_copy
but not successful so far.
Is it supposed to be working or is there any guide for the scenario?
The text was updated successfully, but these errors were encountered: