1 | use super::*; |
2 | use std::marker::PhantomData; |
3 | |
4 | /// `Weak` holds a non-owning reference to an object. |
5 | #[derive (Clone, PartialEq, Eq, Default)] |
6 | pub struct Weak<I: ComInterface>(Option<crate::imp::IWeakReference>, PhantomData<I>); |
7 | |
8 | impl<I: ComInterface> Weak<I> { |
9 | /// Creates a new `Weak` object without any backing object. |
10 | pub fn new() -> Self { |
11 | Self(None, PhantomData) |
12 | } |
13 | |
14 | /// Attempts to upgrade the weak reference to a strong reference. |
15 | pub fn upgrade(&self) -> Option<I> { |
16 | self.0.as_ref().and_then(|inner: &IWeakReference| unsafe { inner.Resolve().ok() }) |
17 | } |
18 | |
19 | pub(crate) fn downgrade(source: &crate::imp::IWeakReferenceSource) -> Result<Self> { |
20 | let reference: Option = unsafe { source.GetWeakReference().ok() }; |
21 | Ok(Self(reference, PhantomData)) |
22 | } |
23 | } |
24 | |