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