Skip to content

Commit c68950e

Browse files
committed
Implement necessary traits for RawUniqueRc
1 parent 17fab92 commit c68950e

File tree

1 file changed

+123
-1
lines changed

1 file changed

+123
-1
lines changed

library/alloc/src/raw_rc/raw_unique_rc.rs

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
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

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

0 commit comments

Comments
 (0)