Skip to content

Commit 05b25c9

Browse files
committed
Add RawRc methods for dyn Any type
1 parent 61ef0b2 commit 05b25c9

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;
@@ -707,6 +708,29 @@ impl<T, A> RawRc<[MaybeUninit<T>], A> {
707708
}
708709
}
709710

711+
impl<A> RawRc<dyn Any, A> {
712+
pub(crate) fn downcast<T>(self) -> Result<RawRc<T, A>, Self>
713+
where
714+
T: Any,
715+
{
716+
if unsafe { self.as_ptr().as_ref() }.is::<T>() {
717+
Ok(unsafe { self.downcast_unchecked() })
718+
} else {
719+
Err(self)
720+
}
721+
}
722+
723+
/// # Safety
724+
///
725+
/// `self` must point to a valid `T` value.
726+
pub(crate) unsafe fn downcast_unchecked<T>(self) -> RawRc<T, A>
727+
where
728+
T: Any,
729+
{
730+
unsafe { self.cast() }
731+
}
732+
}
733+
710734
/// Decrements strong reference count in a reference-counted allocation with a value object that is
711735
/// pointed to by `value_ptr`.
712736
#[inline]

0 commit comments

Comments
 (0)