1use super::*;
2use core::marker::PhantomData;
3
4/// `Weak` holds a non-owning reference to an object.
5#[derive(Clone, PartialEq, Eq, Default)]
6pub struct Weak<I: Interface>(Option<imp::IWeakReference>, PhantomData<I>);
7
8impl<I: Interface> Weak<I> {
9 /// Creates a new `Weak` object without any backing object.
10 pub const 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
17 .as_ref()
18 .and_then(|inner: &IWeakReference| unsafe { inner.Resolve().ok() })
19 }
20
21 pub(crate) fn downgrade(source: &imp::IWeakReferenceSource) -> Result<Self> {
22 let reference: Option = unsafe { source.GetWeakReference().ok() };
23 Ok(Self(reference, PhantomData))
24 }
25}
26
27unsafe impl<I: Interface> Send for Weak<I> {}
28unsafe impl<I: Interface> Sync for Weak<I> {}
29