-
Notifications
You must be signed in to change notification settings - Fork 510
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
Make cast
method produced by implement
macro unsafe
#1753
Conversation
cast
method produces by implement
macrocast
method produced by implement
macro unsafe
ff2300d
to
efdbbc8
Compare
I'm not aware of any need that the |
@@ -5,12 +5,12 @@ use windows::UI::Xaml::*; | |||
// TODO: This is a compile-only test for now until #81 is further along and can provide composable test classes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: @kennykerr this refers to issue #81 which has been closed. Is there a more appropriate issue to link to here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, we should create one. I've been reluctant to delve into this as it would take a few hundred lines of code to test and only applies to Xaml which I've been contemplating dropping support for entirely as it is largely unusable in Rust anyway.
@rylev An FAQ/wiki entry describing the usage of |
Fixes #1183
note: this PR originally removed the
cast
method all together, but I then discovered that's not really desirable, so I added it back in.*The
implement
macro creates acast
method for the types usingimplement
macro that allowed users to query for arbitrary interfaces directly from the implementing type. This method, however, is very hard to use properly as it required that not onlyself
be heap allocated and pinned but that it be allocated inside the type's corresponding_Impl
struct. While this is often the case, the compiler does not enforce this being the case. When there are safety invariants the compiler can't enforce,unsafe
is the right tool for informing users to be careful.This PR makes the
cast
methodunsafe
which should at least draw the user's attention to the requirements described above.Additionally, this PR adds an
alloc
method. This method does the dance of placing the type inside the_Impl
struct, heap allocating it, and then callingQueryInterface
to query for the supplied interface. This is essentially the dynamic equivalent of the variousFrom
impls theimplement
macro already creates (which can skip callingQueryInterface
since we know those interfaces are implemented).alloc
still has the issue of consumingself
meaning that oncealloc
is called the user can no longer use the original implementation type. However, if the user needs access to the original type, the user can query for one of the interfaces used in theimplement
macro which will implement theAsImpl
trait allowing the user to retrieve an&
reference to the original type. Unfortunately, if the call toalloc
fails (presumably because the type does not implement the interface being queried for), the user will no longer have any access to the underlying type (and it will be freed).Some questions and thoughts:
cast
unsafe is quite unfortunate, but currently the best thing we can do.self: &Pin<Box<Self>>
which is certainly not pretty._Impl
type. I think, unfortunately, the right thing to do is to make all trait methods for a COM interface unsafe. Then it would be up to the caller to ensure this invariant.alloc
function actually useful? Would it perhaps be a better idea to remove it?alloc
, should we consider returningself
on failure so the user can keep using it?