xref: /linux-6.15/rust/kernel/sync/arc/std_vendor.rs (revision 2f390cc5)
11edd0337SAsahi Lina // SPDX-License-Identifier: Apache-2.0 OR MIT
21edd0337SAsahi Lina 
3*2f390cc5SMiguel Ojeda //! Rust standard library vendored code.
4*2f390cc5SMiguel Ojeda //!
51edd0337SAsahi Lina //! The contents of this file come from the Rust standard library, hosted in
61edd0337SAsahi Lina //! the <https://github.com/rust-lang/rust> repository, licensed under
71edd0337SAsahi Lina //! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright details,
81edd0337SAsahi Lina //! see <https://github.com/rust-lang/rust/blob/master/COPYRIGHT>.
91edd0337SAsahi Lina 
101edd0337SAsahi Lina use crate::sync::{arc::ArcInner, Arc};
111edd0337SAsahi Lina use core::any::Any;
121edd0337SAsahi Lina 
131edd0337SAsahi Lina impl Arc<dyn Any + Send + Sync> {
141edd0337SAsahi Lina     /// Attempt to downcast the `Arc<dyn Any + Send + Sync>` to a concrete type.
downcast<T>(self) -> core::result::Result<Arc<T>, Self> where T: Any + Send + Sync,151edd0337SAsahi Lina     pub fn downcast<T>(self) -> core::result::Result<Arc<T>, Self>
161edd0337SAsahi Lina     where
171edd0337SAsahi Lina         T: Any + Send + Sync,
181edd0337SAsahi Lina     {
191edd0337SAsahi Lina         if (*self).is::<T>() {
201edd0337SAsahi Lina             // SAFETY: We have just checked that the type is correct, so we can cast the pointer.
211edd0337SAsahi Lina             unsafe {
221edd0337SAsahi Lina                 let ptr = self.ptr.cast::<ArcInner<T>>();
231edd0337SAsahi Lina                 core::mem::forget(self);
241edd0337SAsahi Lina                 Ok(Arc::from_inner(ptr))
251edd0337SAsahi Lina             }
261edd0337SAsahi Lina         } else {
271edd0337SAsahi Lina             Err(self)
281edd0337SAsahi Lina         }
291edd0337SAsahi Lina     }
301edd0337SAsahi Lina }
31