Debian Codegraph @

third_party/rust/windows-core/src/agile_reference.rs

raw
1 use super::*;
2 use core::marker::PhantomData;
3
4 /// A type representing an agile reference to a COM/WinRT object.
5 #[repr(transparent)]
6 #[derive(Clone, PartialEq, Eq)]
7 pub struct AgileReference<T>(imp::IAgileReference, PhantomData<T>);
8
9 impl<T: Interface> AgileReference<T> {
10 /// Creates an agile reference to the object.
11 pub fn new(object: &T) -> Result<Self> {
12 // TODO: this assert is required until we can catch this at compile time using an "associated const equality" constraint.
13 // For example, <T: Interface<UNKNOWN = true>>
14 // https://github.com/rust-lang/rust/issues/92827
15 assert!(T::UNKNOWN);
16 unsafe {
17 imp::RoGetAgileReference(
18 imp::AGILEREFERENCE_DEFAULT,
19 &T::IID,
20 core::mem::transmute::<&T, &IUnknown>(object),
21 )
22 .map(|reference| Self(reference, Default::default()))
23 }
24 }
25
26 /// Retrieves a proxy to the target of the `AgileReference` object that may safely be used within any thread context in which get is called.
27 pub fn resolve(&self) -> Result<T> {
28 unsafe { self.0.Resolve() }
29 }
30 }
31
32 unsafe impl<T: Interface> Send for AgileReference<T> {}
33 unsafe impl<T: Interface> Sync for AgileReference<T> {}
34
35 impl<T> core::fmt::Debug for AgileReference<T> {
36 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
37 write!(f, "AgileReference({:?})", &self.0)
38 }
39 }