From: Wedson Almeida Filho Date: Wed, 28 Dec 2022 06:03:42 +0000 (+0000) Subject: rust: sync: allow coercion from `Arc` to `Arc` X-Git-Tag: v6.6.17~5570^2~12 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f75cb6fce4c91847d3b7cf2c5fc7c8eb4bc2d8f0;p=platform%2Fkernel%2Flinux-rpi.git rust: sync: allow coercion from `Arc` to `Arc` The coercion is only allowed if `U` is a compatible dynamically-sized type (DST). For example, if we have some type `X` that implements trait `Y`, then this allows `Arc` to be coerced into `Arc`. Suggested-by: Gary Guo Signed-off-by: Wedson Almeida Filho Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Vincenzo Palazzo Acked-by: Boqun Feng Signed-off-by: Miguel Ojeda --- diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 1a10f7c..4bde65e 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -13,8 +13,10 @@ #![no_std] #![feature(allocator_api)] +#![feature(coerce_unsized)] #![feature(core_ffi_c)] #![feature(receiver_trait)] +#![feature(unsize)] // Ensure conditional compilation based on the kernel configuration works; // otherwise we may silently break things like initcall handling. diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index e2eb0e6..dbc7596 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -17,7 +17,11 @@ use crate::{bindings, error::Result, types::Opaque}; use alloc::boxed::Box; -use core::{marker::PhantomData, ops::Deref, ptr::NonNull}; +use core::{ + marker::{PhantomData, Unsize}, + ops::Deref, + ptr::NonNull, +}; /// A reference-counted pointer to an instance of `T`. /// @@ -82,6 +86,23 @@ use core::{marker::PhantomData, ops::Deref, ptr::NonNull}; /// obj.use_reference(); /// obj.take_over(); /// ``` +/// +/// Coercion from `Arc` to `Arc`: +/// +/// ``` +/// use kernel::sync::Arc; +/// +/// trait MyTrait {} +/// +/// struct Example; +/// impl MyTrait for Example {} +/// +/// // `obj` has type `Arc`. +/// let obj: Arc = Arc::try_new(Example)?; +/// +/// // `coerced` has type `Arc`. +/// let coerced: Arc = obj; +/// ``` pub struct Arc { ptr: NonNull>, _p: PhantomData>, @@ -96,6 +117,10 @@ struct ArcInner { // This is to allow [`Arc`] (and variants) to be used as the type of `self`. impl core::ops::Receiver for Arc {} +// This is to allow coercion from `Arc` to `Arc` if `T` can be converted to the +// dynamically-sized type (DST) `U`. +impl, U: ?Sized> core::ops::CoerceUnsized> for Arc {} + // SAFETY: It is safe to send `Arc` to another thread when the underlying `T` is `Sync` because // it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs // `T` to be `Send` because any thread that has an `Arc` may ultimately access `T` directly, for