1 // SPDX-License-Identifier: Apache-2.0 OR MIT 2 3 //! Rust standard library vendored code. 4 //! 5 //! The contents of this file come from the Rust standard library, hosted in 6 //! the <https://github.com/rust-lang/rust> repository, licensed under 7 //! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright details, 8 //! see <https://github.com/rust-lang/rust/blob/master/COPYRIGHT>. 9 10 use crate::sync::{arc::ArcInner, Arc}; 11 use core::any::Any; 12 13 impl Arc<dyn Any + Send + Sync> { 14 /// Attempt to downcast the `Arc<dyn Any + Send + Sync>` to a concrete type. 15 pub fn downcast<T>(self) -> core::result::Result<Arc<T>, Self> 16 where 17 T: Any + Send + Sync, 18 { 19 if (*self).is::<T>() { 20 // SAFETY: We have just checked that the type is correct, so we can cast the pointer. 21 unsafe { 22 let ptr = self.ptr.cast::<ArcInner<T>>(); 23 core::mem::forget(self); 24 Ok(Arc::from_inner(ptr)) 25 } 26 } else { 27 Err(self) 28 } 29 } 30 } 31