Skip to content

Commit 0c5de38

Browse files
committed
Add RawRc methods for dyn Any type
1 parent 0713c4f commit 0c5de38

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))]
@@ -758,6 +759,29 @@ impl<T, A> RawRc<[MaybeUninit<T>], A> {
758759
}
759760
}
760761

762+
impl<A> RawRc<dyn Any, A> {
763+
pub(crate) fn downcast<T>(self) -> Result<RawRc<T, A>, Self>
764+
where
765+
T: Any,
766+
{
767+
if unsafe { self.as_ptr().as_ref() }.is::<T>() {
768+
Ok(unsafe { self.downcast_unchecked() })
769+
} else {
770+
Err(self)
771+
}
772+
}
773+
774+
/// # Safety
775+
///
776+
/// `self` must point to a valid `T` value.
777+
pub(crate) unsafe fn downcast_unchecked<T>(self) -> RawRc<T, A>
778+
where
779+
T: Any,
780+
{
781+
unsafe { self.cast() }
782+
}
783+
}
784+
761785
/// Decrements strong reference count in a reference-counted allocation with a value object that is
762786
/// pointed to by `value_ptr`.
763787
#[inline]

0 commit comments

Comments
 (0)