// Take a look at the license at the top of the repository in the LICENSE file. use std::{ marker::PhantomData, rc::{self, Rc}, sync::{self, Arc}, }; // rustdoc-stripper-ignore-next /// Trait for generalizing downgrading a strong reference to a weak reference. pub trait Downgrade where Self: Sized, { // rustdoc-stripper-ignore-next /// Weak reference type. type Weak; // rustdoc-stripper-ignore-next /// Downgrade to a weak reference. fn downgrade(&self) -> Self::Weak; } // rustdoc-stripper-ignore-next /// Trait for generalizing upgrading a weak reference to a strong reference. pub trait Upgrade where Self: Sized, { // rustdoc-stripper-ignore-next /// Strong reference type. type Strong; // rustdoc-stripper-ignore-next /// Try upgrading a weak reference to a strong reference. fn upgrade(&self) -> Option; } impl Upgrade for crate::WeakRef { type Strong = T; fn upgrade(&self) -> Option { self.upgrade() } } impl Downgrade for PhantomData { type Weak = PhantomData; fn downgrade(&self) -> Self::Weak { PhantomData } } impl Downgrade for &T { type Weak = T::Weak; fn downgrade(&self) -> Self::Weak { T::downgrade(*self) } } impl Downgrade for Arc { type Weak = sync::Weak; fn downgrade(&self) -> Self::Weak { Arc::downgrade(self) } } impl Upgrade for PhantomData { type Strong = PhantomData; fn upgrade(&self) -> Option { Some(PhantomData) } } impl Upgrade for sync::Weak { type Strong = Arc; fn upgrade(&self) -> Option { self.upgrade() } } impl Downgrade for Rc { type Weak = rc::Weak; fn downgrade(&self) -> Self::Weak { Rc::downgrade(self) } } impl Upgrade for rc::Weak { type Strong = Rc; fn upgrade(&self) -> Option { self.upgrade() } }