1use alloc::{rc, sync};
2
3pub trait StrongRef {
5 type Weak: WeakRef<Strong = Self>;
9
10 fn downgrade(&self) -> Self::Weak;
14
15 fn ptr_eq(&self, other: &Self) -> bool;
19}
20
21pub trait WeakRef {
23 type Strong: StrongRef<Weak = Self>;
27
28 fn upgrade(&self) -> Option<Self::Strong>;
32
33 fn is_expired(&self) -> bool {
38 self.upgrade().is_none()
39 }
40}
41
42impl<T: ?Sized> StrongRef for rc::Rc<T> {
43 type Weak = rc::Weak<T>;
44
45 fn downgrade(&self) -> Self::Weak {
46 rc::Rc::downgrade(self)
47 }
48
49 fn ptr_eq(&self, other: &Self) -> bool {
50 rc::Rc::ptr_eq(self, other)
51 }
52}
53
54impl<T: ?Sized> WeakRef for rc::Weak<T> {
55 type Strong = rc::Rc<T>;
56
57 fn upgrade(&self) -> Option<Self::Strong> {
58 self.upgrade()
59 }
60
61 fn is_expired(&self) -> bool {
62 self.strong_count() == 0
63 }
64}
65
66impl<T: ?Sized> StrongRef for sync::Arc<T> {
67 type Weak = sync::Weak<T>;
68
69 fn downgrade(&self) -> Self::Weak {
70 sync::Arc::downgrade(self)
71 }
72
73 fn ptr_eq(&self, other: &Self) -> bool {
74 sync::Arc::ptr_eq(self, other)
75 }
76}
77
78impl<T: ?Sized> WeakRef for sync::Weak<T> {
79 type Strong = sync::Arc<T>;
80
81 fn upgrade(&self) -> Option<Self::Strong> {
82 self.upgrade()
83 }
84
85 fn is_expired(&self) -> bool {
86 self.strong_count() == 0
87 }
88}