Skip to content

Commit 85fca42

Browse files
committed
Implement necessary traits for RawWeak
1 parent 12b73db commit 85fca42

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

library/alloc/src/raw_rc/raw_weak.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
use core::alloc::{AllocError, Allocator};
22
use core::cell::UnsafeCell;
3+
use core::fmt::{self, Debug, Formatter};
4+
use core::marker::Unsize;
35
use core::mem::{self, DropGuard};
46
use core::num::NonZeroUsize;
7+
use core::ops::{CoerceUnsized, DispatchFromDyn};
58
use core::ptr::{self, NonNull};
69

10+
use crate::alloc::Global;
711
use crate::raw_rc::rc_layout::{RcLayout, RcLayoutExt};
812
use crate::raw_rc::rc_value_pointer::RcValuePointer;
913
use crate::raw_rc::{RefCounter, RefCounts, rc_alloc};
@@ -435,6 +439,38 @@ impl<T, A> RawWeak<[T], A> {
435439
}
436440
}
437441

442+
impl<T, U, A> CoerceUnsized<RawWeak<U, A>> for RawWeak<T, A>
443+
where
444+
T: Unsize<U> + ?Sized,
445+
U: ?Sized,
446+
{
447+
}
448+
449+
impl<T, A> Debug for RawWeak<T, A>
450+
where
451+
T: ?Sized,
452+
{
453+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
454+
f.write_str("(Weak)")
455+
}
456+
}
457+
458+
impl<T, A> Default for RawWeak<T, A>
459+
where
460+
A: Default,
461+
{
462+
fn default() -> Self {
463+
Self::new_dangling()
464+
}
465+
}
466+
467+
impl<T, U> DispatchFromDyn<RawWeak<U, Global>> for RawWeak<T, Global>
468+
where
469+
T: Unsize<U> + ?Sized,
470+
U: ?Sized,
471+
{
472+
}
473+
438474
// We choose `NonZeroUsize::MAX` as the address for dangling weak pointers because:
439475
//
440476
// - It does not point to any object that is stored inside a reference-counted allocation. Because

0 commit comments

Comments
 (0)