Skip to content

Commit 1f7648e

Browse files
committed
Implement necessary traits for RawUniqueRc
1 parent db6cfb1 commit 1f7648e

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,12 +1,16 @@
11
use core::alloc::Allocator;
22
#[cfg(not(no_global_oom_handling))]
33
use core::convert;
4-
use core::marker::PhantomData;
4+
use core::fmt::{self, Debug, Display, Formatter, Pointer};
5+
use core::hash::{Hash, Hasher};
6+
use core::marker::{PhantomData, Unsize};
57
#[cfg(not(no_global_oom_handling))]
68
use core::mem::{self, SizedTypeProperties};
9+
use core::ops::{CoerceUnsized, DispatchFromDyn};
710
#[cfg(not(no_global_oom_handling))]
811
use core::ops::{Residual, Try};
912

13+
use crate::alloc::Global;
1014
use crate::raw_rc::RefCounter;
1115
use crate::raw_rc::raw_rc::RawRc;
1216
#[cfg(not(no_global_oom_handling))]
@@ -170,3 +174,121 @@ impl<T, A> RawUniqueRc<T, A> {
170174
try { map_result(unsafe { RawUniqueRc::from_weak_with_value(allocation, mapped_value) }) }
171175
}
172176
}
177+
178+
impl<T, A> AsMut<T> for RawUniqueRc<T, A>
179+
where
180+
T: ?Sized,
181+
{
182+
fn as_mut(&mut self) -> &mut T {
183+
unsafe { self.weak.as_ptr().as_mut() }
184+
}
185+
}
186+
187+
impl<T, A> AsRef<T> for RawUniqueRc<T, A>
188+
where
189+
T: ?Sized,
190+
{
191+
fn as_ref(&self) -> &T {
192+
unsafe { self.weak.as_ptr().as_ref() }
193+
}
194+
}
195+
196+
impl<T, U, A> CoerceUnsized<RawUniqueRc<U, A>> for RawUniqueRc<T, A>
197+
where
198+
T: Unsize<U> + ?Sized,
199+
U: ?Sized,
200+
A: Allocator,
201+
{
202+
}
203+
204+
impl<T, A> Debug for RawUniqueRc<T, A>
205+
where
206+
T: Debug + ?Sized,
207+
{
208+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
209+
<T as Debug>::fmt(self.as_ref(), f)
210+
}
211+
}
212+
213+
impl<T, U> DispatchFromDyn<RawUniqueRc<U, Global>> for RawUniqueRc<T, Global>
214+
where
215+
T: Unsize<U> + ?Sized,
216+
U: ?Sized,
217+
{
218+
}
219+
220+
impl<T, A> Display for RawUniqueRc<T, A>
221+
where
222+
T: Display + ?Sized,
223+
{
224+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
225+
<T as Display>::fmt(self.as_ref(), f)
226+
}
227+
}
228+
229+
impl<T, A> Eq for RawUniqueRc<T, A> where T: Eq + ?Sized {}
230+
231+
impl<T, A> Hash for RawUniqueRc<T, A>
232+
where
233+
T: Hash + ?Sized,
234+
{
235+
fn hash<H: Hasher>(&self, state: &mut H) {
236+
T::hash(self.as_ref(), state);
237+
}
238+
}
239+
240+
impl<T, A> Ord for RawUniqueRc<T, A>
241+
where
242+
T: Ord + ?Sized,
243+
{
244+
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
245+
T::cmp(self.as_ref(), other.as_ref())
246+
}
247+
}
248+
249+
impl<T, A> PartialEq for RawUniqueRc<T, A>
250+
where
251+
T: PartialEq + ?Sized,
252+
{
253+
fn eq(&self, other: &Self) -> bool {
254+
T::eq(self.as_ref(), other.as_ref())
255+
}
256+
257+
fn ne(&self, other: &Self) -> bool {
258+
T::ne(self.as_ref(), other.as_ref())
259+
}
260+
}
261+
262+
impl<T, A> PartialOrd for RawUniqueRc<T, A>
263+
where
264+
T: PartialOrd + ?Sized,
265+
{
266+
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
267+
T::partial_cmp(self.as_ref(), other.as_ref())
268+
}
269+
270+
fn lt(&self, other: &Self) -> bool {
271+
T::lt(self.as_ref(), other.as_ref())
272+
}
273+
274+
fn le(&self, other: &Self) -> bool {
275+
T::le(self.as_ref(), other.as_ref())
276+
}
277+
278+
fn gt(&self, other: &Self) -> bool {
279+
T::gt(self.as_ref(), other.as_ref())
280+
}
281+
282+
fn ge(&self, other: &Self) -> bool {
283+
T::ge(self.as_ref(), other.as_ref())
284+
}
285+
}
286+
287+
impl<T, A> Pointer for RawUniqueRc<T, A>
288+
where
289+
T: ?Sized,
290+
{
291+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
292+
<&T as Pointer>::fmt(&self.as_ref(), f)
293+
}
294+
}

0 commit comments

Comments
 (0)