add {Arc, Rc}::downcast_unchecked
This commit is contained in:
parent
4b043faba3
commit
1ac54401d1
2 changed files with 80 additions and 5 deletions
|
@ -1241,8 +1241,6 @@ impl<T: Clone> Rc<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rc<dyn Any> {
|
impl Rc<dyn Any> {
|
||||||
#[inline]
|
|
||||||
#[stable(feature = "rc_downcast", since = "1.29.0")]
|
|
||||||
/// Attempt to downcast the `Rc<dyn Any>` to a concrete type.
|
/// Attempt to downcast the `Rc<dyn Any>` to a concrete type.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
@ -1261,6 +1259,8 @@ impl Rc<dyn Any> {
|
||||||
/// print_if_string(Rc::new(my_string));
|
/// print_if_string(Rc::new(my_string));
|
||||||
/// print_if_string(Rc::new(0i8));
|
/// print_if_string(Rc::new(0i8));
|
||||||
/// ```
|
/// ```
|
||||||
|
#[inline]
|
||||||
|
#[stable(feature = "rc_downcast", since = "1.29.0")]
|
||||||
pub fn downcast<T: Any>(self) -> Result<Rc<T>, Rc<dyn Any>> {
|
pub fn downcast<T: Any>(self) -> Result<Rc<T>, Rc<dyn Any>> {
|
||||||
if (*self).is::<T>() {
|
if (*self).is::<T>() {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -1272,6 +1272,42 @@ impl Rc<dyn Any> {
|
||||||
Err(self)
|
Err(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Downcasts the `Rc<dyn Any>` to a concrete type.
|
||||||
|
///
|
||||||
|
/// For a safe alternative see [`downcast`].
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// #![feature(downcast_unchecked)]
|
||||||
|
///
|
||||||
|
/// use std::any::Any;
|
||||||
|
/// use std::rc::Rc;
|
||||||
|
///
|
||||||
|
/// let x: Rc<dyn Any> = Rc::new(1_usize);
|
||||||
|
///
|
||||||
|
/// unsafe {
|
||||||
|
/// assert_eq!(*x.downcast_unchecked::<usize>(), 1);
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// The contained value must be of type `T`. Calling this method
|
||||||
|
/// with the incorrect type is *undefined behavior*.
|
||||||
|
///
|
||||||
|
///
|
||||||
|
/// [`downcast`]: Self::downcast
|
||||||
|
#[inline]
|
||||||
|
#[unstable(feature = "downcast_unchecked", issue = "90850")]
|
||||||
|
pub unsafe fn downcast_unchecked<T: Any>(self) -> Rc<T> {
|
||||||
|
unsafe {
|
||||||
|
let ptr = self.ptr.cast::<RcBox<T>>();
|
||||||
|
mem::forget(self);
|
||||||
|
Rc::from_inner(ptr)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: ?Sized> Rc<T> {
|
impl<T: ?Sized> Rc<T> {
|
||||||
|
|
|
@ -1691,8 +1691,6 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Arc<dyn Any + Send + Sync> {
|
impl Arc<dyn Any + Send + Sync> {
|
||||||
#[inline]
|
|
||||||
#[stable(feature = "rc_downcast", since = "1.29.0")]
|
|
||||||
/// Attempt to downcast the `Arc<dyn Any + Send + Sync>` to a concrete type.
|
/// Attempt to downcast the `Arc<dyn Any + Send + Sync>` to a concrete type.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
@ -1711,9 +1709,11 @@ impl Arc<dyn Any + Send + Sync> {
|
||||||
/// print_if_string(Arc::new(my_string));
|
/// print_if_string(Arc::new(my_string));
|
||||||
/// print_if_string(Arc::new(0i8));
|
/// print_if_string(Arc::new(0i8));
|
||||||
/// ```
|
/// ```
|
||||||
|
#[inline]
|
||||||
|
#[stable(feature = "rc_downcast", since = "1.29.0")]
|
||||||
pub fn downcast<T>(self) -> Result<Arc<T>, Self>
|
pub fn downcast<T>(self) -> Result<Arc<T>, Self>
|
||||||
where
|
where
|
||||||
T: Any + Send + Sync + 'static,
|
T: Any + Send + Sync,
|
||||||
{
|
{
|
||||||
if (*self).is::<T>() {
|
if (*self).is::<T>() {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -1725,6 +1725,45 @@ impl Arc<dyn Any + Send + Sync> {
|
||||||
Err(self)
|
Err(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Downcasts the `Arc<dyn Any + Send + Sync>` to a concrete type.
|
||||||
|
///
|
||||||
|
/// For a safe alternative see [`downcast`].
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// #![feature(downcast_unchecked)]
|
||||||
|
///
|
||||||
|
/// use std::any::Any;
|
||||||
|
/// use std::sync::Arc;
|
||||||
|
///
|
||||||
|
/// let x: Arc<dyn Any + Send + Sync> = Arc::new(1_usize);
|
||||||
|
///
|
||||||
|
/// unsafe {
|
||||||
|
/// assert_eq!(*x.downcast_unchecked::<usize>(), 1);
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// The contained value must be of type `T`. Calling this method
|
||||||
|
/// with the incorrect type is *undefined behavior*.
|
||||||
|
///
|
||||||
|
///
|
||||||
|
/// [`downcast`]: Self::downcast
|
||||||
|
#[inline]
|
||||||
|
#[unstable(feature = "downcast_unchecked", issue = "90850")]
|
||||||
|
pub unsafe fn downcast_unchecked<T>(self) -> Arc<T>
|
||||||
|
where
|
||||||
|
T: Any + Send + Sync,
|
||||||
|
{
|
||||||
|
unsafe {
|
||||||
|
let ptr = self.ptr.cast::<ArcInner<T>>();
|
||||||
|
mem::forget(self);
|
||||||
|
Arc::from_inner(ptr)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Weak<T> {
|
impl<T> Weak<T> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue