Skip to content

Commit 9414e2a

Browse files
committed
Add RawRc methods for dyn Any type
1 parent 0fac2bb commit 9414e2a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

library/alloc/src/raw_rc/raw_rc.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::alloc::{AllocError, Allocator};
2+
use core::any::Any;
23
use core::cell::UnsafeCell;
34
#[cfg(not(no_global_oom_handling))]
45
use core::clone::CloneToUninit;
@@ -657,3 +658,26 @@ impl<T, A> RawRc<[MaybeUninit<T>], A> {
657658
unsafe { self.cast_with(|ptr| NonNull::new_unchecked(ptr.as_ptr() as _)) }
658659
}
659660
}
661+
662+
impl<A> RawRc<dyn Any, A> {
663+
pub(crate) fn downcast<T>(self) -> Result<RawRc<T, A>, Self>
664+
where
665+
T: Any,
666+
{
667+
if unsafe { self.as_ptr().as_ref() }.is::<T>() {
668+
Ok(unsafe { self.downcast_unchecked() })
669+
} else {
670+
Err(self)
671+
}
672+
}
673+
674+
/// # Safety
675+
///
676+
/// `self` must point to a valid `T` value.
677+
pub(crate) unsafe fn downcast_unchecked<T>(self) -> RawRc<T, A>
678+
where
679+
T: Any,
680+
{
681+
unsafe { self.cast() }
682+
}
683+
}

0 commit comments

Comments
 (0)