rust: sync: arc: Implement Arc<dyn Any + Send + Sync>::downcast()
This mirrors the standard library's alloc::sync::Arc::downcast(). Based on the Rust standard library implementation, ver 1.62.0, licensed under "Apache-2.0 OR MIT", from: https://github.com/rust-lang/rust/tree/1.62.0/library/alloc/src For copyright details, please see: https://github.com/rust-lang/rust/blob/1.62.0/COPYRIGHT Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com> Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com> Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com> Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Asahi Lina <lina@asahilina.net> Link: https://lore.kernel.org/r/20230224-rust-arc-v2-1-5c97a865b276@asahilina.net [ Moved `mod std_vendor;` up. ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
parent
39867fec28
commit
1edd03378e
@ -30,6 +30,8 @@ use core::{
|
|||||||
ptr::NonNull,
|
ptr::NonNull,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mod std_vendor;
|
||||||
|
|
||||||
/// A reference-counted pointer to an instance of `T`.
|
/// A reference-counted pointer to an instance of `T`.
|
||||||
///
|
///
|
||||||
/// The reference count is incremented when new instances of [`Arc`] are created, and decremented
|
/// The reference count is incremented when new instances of [`Arc`] are created, and decremented
|
||||||
|
28
rust/kernel/sync/arc/std_vendor.rs
Normal file
28
rust/kernel/sync/arc/std_vendor.rs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||||
|
|
||||||
|
//! The contents of this file come from the Rust standard library, hosted in
|
||||||
|
//! the <https://github.com/rust-lang/rust> repository, licensed under
|
||||||
|
//! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright details,
|
||||||
|
//! see <https://github.com/rust-lang/rust/blob/master/COPYRIGHT>.
|
||||||
|
|
||||||
|
use crate::sync::{arc::ArcInner, Arc};
|
||||||
|
use core::any::Any;
|
||||||
|
|
||||||
|
impl Arc<dyn Any + Send + Sync> {
|
||||||
|
/// Attempt to downcast the `Arc<dyn Any + Send + Sync>` to a concrete type.
|
||||||
|
pub fn downcast<T>(self) -> core::result::Result<Arc<T>, Self>
|
||||||
|
where
|
||||||
|
T: Any + Send + Sync,
|
||||||
|
{
|
||||||
|
if (*self).is::<T>() {
|
||||||
|
// SAFETY: We have just checked that the type is correct, so we can cast the pointer.
|
||||||
|
unsafe {
|
||||||
|
let ptr = self.ptr.cast::<ArcInner<T>>();
|
||||||
|
core::mem::forget(self);
|
||||||
|
Ok(Arc::from_inner(ptr))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user