std_vendor.rs 1.0 KB

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