1 | use super::*; |
2 | use std::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>(crate::imp::IAgileReference, PhantomData<T>); |
8 | |
9 | impl<T: ComInterface> AgileReference<T> { |
10 | /// Creates an agile reference to the object. |
11 | pub fn new(object: &T) -> Result<Self> { |
12 | unsafe { crate::imp::RoGetAgileReference(crate::imp::AGILEREFERENCE_DEFAULT, &T::IID, object.as_unknown()).map(|reference: IAgileReference| Self(reference, Default::default())) } |
13 | } |
14 | |
15 | /// Retrieves a proxy to the target of the `AgileReference` object that may safely be used within any thread context in which get is called. |
16 | pub fn resolve(&self) -> Result<T> { |
17 | unsafe { self.0.Resolve() } |
18 | } |
19 | } |
20 | |
21 | unsafe impl<T: ComInterface> Send for AgileReference<T> {} |
22 | unsafe impl<T: ComInterface> Sync for AgileReference<T> {} |
23 | |
24 | impl<T> std::fmt::Debug for AgileReference<T> { |
25 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
26 | write!(f, "AgileReference( {:?})" , &self.0) |
27 | } |
28 | } |
29 | |