use std::{
marker::PhantomData,
rc::{self, Rc},
sync::{self, Arc},
};
pub trait Downgrade
where
Self: Sized,
{
type Weak;
fn downgrade(&self) -> Self::Weak;
}
pub trait Upgrade
where
Self: Sized,
{
type Strong;
fn upgrade(&self) -> Option<Self::Strong>;
}
impl<T: Downgrade + crate::ObjectType> Upgrade for crate::WeakRef<T> {
type Strong = T;
fn upgrade(&self) -> Option<Self::Strong> {
self.upgrade()
}
}
impl<T> Downgrade for PhantomData<T> {
type Weak = PhantomData<T>;
fn downgrade(&self) -> Self::Weak {
PhantomData
}
}
impl<T: Downgrade> Downgrade for &T {
type Weak = T::Weak;
fn downgrade(&self) -> Self::Weak {
T::downgrade(*self)
}
}
impl<T> Downgrade for Arc<T> {
type Weak = sync::Weak<T>;
fn downgrade(&self) -> Self::Weak {
Arc::downgrade(self)
}
}
impl<T> Upgrade for PhantomData<T> {
type Strong = PhantomData<T>;
fn upgrade(&self) -> Option<Self::Strong> {
Some(PhantomData)
}
}
impl<T> Upgrade for sync::Weak<T> {
type Strong = Arc<T>;
fn upgrade(&self) -> Option<Self::Strong> {
self.upgrade()
}
}
impl<T> Downgrade for Rc<T> {
type Weak = rc::Weak<T>;
fn downgrade(&self) -> Self::Weak {
Rc::downgrade(self)
}
}
impl<T> Upgrade for rc::Weak<T> {
type Strong = Rc<T>;
fn upgrade(&self) -> Option<Self::Strong> {
self.upgrade()
}
}