use alloc::{rc, sync};
pub trait StrongRef {
type Weak: WeakRef<Strong = Self>;
fn downgrade(&self) -> Self::Weak;
fn ptr_eq(&self, other: &Self) -> bool;
}
pub trait WeakRef {
type Strong: StrongRef<Weak = Self>;
fn upgrade(&self) -> Option<Self::Strong>;
fn is_expired(&self) -> bool {
self.upgrade().is_none()
}
}
impl<T: ?Sized> StrongRef for rc::Rc<T> {
type Weak = rc::Weak<T>;
fn downgrade(&self) -> Self::Weak {
rc::Rc::downgrade(self)
}
fn ptr_eq(&self, other: &Self) -> bool {
rc::Rc::ptr_eq(self, other)
}
}
impl<T: ?Sized> WeakRef for rc::Weak<T> {
type Strong = rc::Rc<T>;
fn upgrade(&self) -> Option<Self::Strong> {
self.upgrade()
}
fn is_expired(&self) -> bool {
self.strong_count() == 0
}
}
impl<T: ?Sized> StrongRef for sync::Arc<T> {
type Weak = sync::Weak<T>;
fn downgrade(&self) -> Self::Weak {
sync::Arc::downgrade(self)
}
fn ptr_eq(&self, other: &Self) -> bool {
sync::Arc::ptr_eq(self, other)
}
}
impl<T: ?Sized> WeakRef for sync::Weak<T> {
type Strong = sync::Arc<T>;
fn upgrade(&self) -> Option<Self::Strong> {
self.upgrade()
}
fn is_expired(&self) -> bool {
self.strong_count() == 0
}
}