Skip to content

Commit 8bc9dea

Browse files
committed
Implement necessary traits for RawUniqueRc
1 parent 82525d0 commit 8bc9dea

File tree

1 file changed

+124
-3
lines changed

1 file changed

+124
-3
lines changed

library/alloc/src/raw_rc/raw_unique_rc.rs

Lines changed: 124 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use core::alloc::Allocator;
2-
use core::marker::PhantomData;
2+
use core::fmt::{self, Debug, Display, Formatter, Pointer};
3+
use core::hash::{Hash, Hasher};
4+
use core::marker::{PhantomData, Unsize};
5+
use core::ops::{CoerceUnsized, DispatchFromDyn};
36

4-
use crate::raw_rc::RefCounter;
7+
use crate::alloc::Global;
58
use crate::raw_rc::raw_rc::RawRc;
69
use crate::raw_rc::raw_weak::RawWeak;
7-
use crate::raw_rc::rc_value_pointer::RcValuePointer;
10+
use crate::raw_rc::{RcValuePointer, RefCounter};
811

912
/// A uniquely owned `RawRc` that allows multiple weak references but only one strong reference.
1013
/// `RawUniqueRc` does not implement `Drop`, user should call `RawUniqueRc::drop` manually to drop
@@ -98,3 +101,121 @@ impl<T, A> RawUniqueRc<T, A> {
98101
unsafe { Self::from_weak_with_value(RawWeak::new_uninit_in::<0>(alloc), value) }
99102
}
100103
}
104+
105+
impl<T, A> AsMut<T> for RawUniqueRc<T, A>
106+
where
107+
T: ?Sized,
108+
{
109+
fn as_mut(&mut self) -> &mut T {
110+
unsafe { self.weak.as_ptr().as_mut() }
111+
}
112+
}
113+
114+
impl<T, A> AsRef<T> for RawUniqueRc<T, A>
115+
where
116+
T: ?Sized,
117+
{
118+
fn as_ref(&self) -> &T {
119+
unsafe { self.weak.as_ptr().as_ref() }
120+
}
121+
}
122+
123+
impl<T, U, A> CoerceUnsized<RawUniqueRc<U, A>> for RawUniqueRc<T, A>
124+
where
125+
T: Unsize<U> + ?Sized,
126+
U: ?Sized,
127+
A: Allocator,
128+
{
129+
}
130+
131+
impl<T, A> Debug for RawUniqueRc<T, A>
132+
where
133+
T: Debug + ?Sized,
134+
{
135+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
136+
<T as Debug>::fmt(self.as_ref(), f)
137+
}
138+
}
139+
140+
impl<T, U> DispatchFromDyn<RawUniqueRc<U, Global>> for RawUniqueRc<T, Global>
141+
where
142+
T: Unsize<U> + ?Sized,
143+
U: ?Sized,
144+
{
145+
}
146+
147+
impl<T, A> Display for RawUniqueRc<T, A>
148+
where
149+
T: Display + ?Sized,
150+
{
151+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
152+
<T as Display>::fmt(self.as_ref(), f)
153+
}
154+
}
155+
156+
impl<T, A> Eq for RawUniqueRc<T, A> where T: Eq + ?Sized {}
157+
158+
impl<T, A> Hash for RawUniqueRc<T, A>
159+
where
160+
T: Hash + ?Sized,
161+
{
162+
fn hash<H: Hasher>(&self, state: &mut H) {
163+
T::hash(self.as_ref(), state);
164+
}
165+
}
166+
167+
impl<T, A> Ord for RawUniqueRc<T, A>
168+
where
169+
T: Ord + ?Sized,
170+
{
171+
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
172+
T::cmp(self.as_ref(), other.as_ref())
173+
}
174+
}
175+
176+
impl<T, A> PartialEq for RawUniqueRc<T, A>
177+
where
178+
T: PartialEq + ?Sized,
179+
{
180+
fn eq(&self, other: &Self) -> bool {
181+
T::eq(self.as_ref(), other.as_ref())
182+
}
183+
184+
fn ne(&self, other: &Self) -> bool {
185+
T::ne(self.as_ref(), other.as_ref())
186+
}
187+
}
188+
189+
impl<T, A> PartialOrd for RawUniqueRc<T, A>
190+
where
191+
T: PartialOrd + ?Sized,
192+
{
193+
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
194+
T::partial_cmp(self.as_ref(), other.as_ref())
195+
}
196+
197+
fn lt(&self, other: &Self) -> bool {
198+
T::lt(self.as_ref(), other.as_ref())
199+
}
200+
201+
fn le(&self, other: &Self) -> bool {
202+
T::le(self.as_ref(), other.as_ref())
203+
}
204+
205+
fn gt(&self, other: &Self) -> bool {
206+
T::gt(self.as_ref(), other.as_ref())
207+
}
208+
209+
fn ge(&self, other: &Self) -> bool {
210+
T::ge(self.as_ref(), other.as_ref())
211+
}
212+
}
213+
214+
impl<T, A> Pointer for RawUniqueRc<T, A>
215+
where
216+
T: ?Sized,
217+
{
218+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
219+
<&T as Pointer>::fmt(&self.as_ref(), f)
220+
}
221+
}

0 commit comments

Comments
 (0)