diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2022-06-20 14:56:35 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-20 14:56:35 +0200 |
| commit | 7372bf88ee17088023590f73fee380db97ee68e0 (patch) | |
| tree | 439b196e3146b8d185eb0e85223c85b1d55ebf47 /library/alloc/src | |
| parent | 99620ad7215d58ada72eabb2d7467fd685ac1090 (diff) | |
| parent | 1ac54401d1b7224f1b8744b2b1f130d4d3cdfe34 (diff) | |
| download | rust-7372bf88ee17088023590f73fee380db97ee68e0.tar.gz rust-7372bf88ee17088023590f73fee380db97ee68e0.zip | |
Rollup merge of #96609 - ibraheemdev:arc-downcast-unchecked, r=m-ou-se
Add `{Arc, Rc}::downcast_unchecked`
Part of #90850.
Diffstat (limited to 'library/alloc/src')
| -rw-r--r-- | library/alloc/src/rc.rs | 40 | ||||
| -rw-r--r-- | library/alloc/src/sync.rs | 45 |
2 files changed, 80 insertions, 5 deletions
diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 2b3736019ba..a248cd458df 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -1254,8 +1254,6 @@ impl<T: Clone> Rc<T> { } impl Rc<dyn Any> { - #[inline] - #[stable(feature = "rc_downcast", since = "1.29.0")] /// Attempt to downcast the `Rc<dyn Any>` to a concrete type. /// /// # Examples @@ -1274,6 +1272,8 @@ impl Rc<dyn Any> { /// print_if_string(Rc::new(my_string)); /// 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>> { if (*self).is::<T>() { unsafe { @@ -1285,6 +1285,42 @@ impl Rc<dyn Any> { 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> { diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 55d51e0a3c4..2670b15982a 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -1705,8 +1705,6 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Arc<T> { } 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. /// /// # Examples @@ -1725,9 +1723,11 @@ impl Arc<dyn Any + Send + Sync> { /// print_if_string(Arc::new(my_string)); /// print_if_string(Arc::new(0i8)); /// ``` + #[inline] + #[stable(feature = "rc_downcast", since = "1.29.0")] pub fn downcast<T>(self) -> Result<Arc<T>, Self> where - T: Any + Send + Sync + 'static, + T: Any + Send + Sync, { if (*self).is::<T>() { unsafe { @@ -1739,6 +1739,45 @@ impl Arc<dyn Any + Send + Sync> { 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> { |
