Skip to content

Commit 532882d

Browse files
committed
Add RawRc methods for dyn Any type
1 parent 2332c17 commit 532882d

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
use core::clone::CloneToUninit;
45
#[cfg(not(no_global_oom_handling))]
@@ -744,6 +745,29 @@ impl<T, A> RawRc<[MaybeUninit<T>], A> {
744745
}
745746
}
746747

748+
impl<A> RawRc<dyn Any, A> {
749+
pub(crate) fn downcast<T>(self) -> Result<RawRc<T, A>, Self>
750+
where
751+
T: Any,
752+
{
753+
if unsafe { self.as_ptr().as_ref() }.is::<T>() {
754+
Ok(unsafe { self.downcast_unchecked() })
755+
} else {
756+
Err(self)
757+
}
758+
}
759+
760+
/// # Safety
761+
///
762+
/// `self` must point to a valid `T` value.
763+
pub(crate) unsafe fn downcast_unchecked<T>(self) -> RawRc<T, A>
764+
where
765+
T: Any,
766+
{
767+
unsafe { self.cast() }
768+
}
769+
}
770+
747771
/// Decrements strong reference count in a reference-counted allocation with a value object that is
748772
/// pointed to by `value_ptr`.
749773
#[inline]

0 commit comments

Comments
 (0)