mirror of
https://gitee.com/bianbu-linux/linux-6.6
synced 2025-04-24 14:07:52 -04:00
rust: sync: add support for dispatching on Arc and ArcBorrow.
Trait objects (`dyn T`) require trait `T` to be "object safe". One of the requirements for "object safety" is that the receiver have one of the allowed types. This commit adds `Arc<T>` and `ArcBorrow<'_, T>` to the list of allowed types. Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Gary Guo <gary@garyguo.net> Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com> Acked-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
parent
70e42ebbf6
commit
0748424aba
2 changed files with 19 additions and 2 deletions
|
@ -15,6 +15,7 @@
|
||||||
#![feature(allocator_api)]
|
#![feature(allocator_api)]
|
||||||
#![feature(coerce_unsized)]
|
#![feature(coerce_unsized)]
|
||||||
#![feature(core_ffi_c)]
|
#![feature(core_ffi_c)]
|
||||||
|
#![feature(dispatch_from_dyn)]
|
||||||
#![feature(receiver_trait)]
|
#![feature(receiver_trait)]
|
||||||
#![feature(unsize)]
|
#![feature(unsize)]
|
||||||
|
|
||||||
|
|
|
@ -92,9 +92,15 @@ use core::{
|
||||||
/// Coercion from `Arc<Example>` to `Arc<dyn MyTrait>`:
|
/// Coercion from `Arc<Example>` to `Arc<dyn MyTrait>`:
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use kernel::sync::Arc;
|
/// use kernel::sync::{Arc, ArcBorrow};
|
||||||
///
|
///
|
||||||
/// trait MyTrait {}
|
/// trait MyTrait {
|
||||||
|
/// // Trait has a function whose `self` type is `Arc<Self>`.
|
||||||
|
/// fn example1(self: Arc<Self>) {}
|
||||||
|
///
|
||||||
|
/// // Trait has a function whose `self` type is `ArcBorrow<'_, Self>`.
|
||||||
|
/// fn example2(self: ArcBorrow<'_, Self>) {}
|
||||||
|
/// }
|
||||||
///
|
///
|
||||||
/// struct Example;
|
/// struct Example;
|
||||||
/// impl MyTrait for Example {}
|
/// impl MyTrait for Example {}
|
||||||
|
@ -123,6 +129,9 @@ impl<T: ?Sized> core::ops::Receiver for Arc<T> {}
|
||||||
// dynamically-sized type (DST) `U`.
|
// dynamically-sized type (DST) `U`.
|
||||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::CoerceUnsized<Arc<U>> for Arc<T> {}
|
impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::CoerceUnsized<Arc<U>> for Arc<T> {}
|
||||||
|
|
||||||
|
// This is to allow `Arc<U>` to be dispatched on when `Arc<T>` can be coerced into `Arc<U>`.
|
||||||
|
impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<Arc<U>> for Arc<T> {}
|
||||||
|
|
||||||
// SAFETY: It is safe to send `Arc<T>` to another thread when the underlying `T` is `Sync` because
|
// SAFETY: It is safe to send `Arc<T>` 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
|
// 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<T>` may ultimately access `T` directly, for
|
// `T` to be `Send` because any thread that has an `Arc<T>` may ultimately access `T` directly, for
|
||||||
|
@ -297,6 +306,13 @@ pub struct ArcBorrow<'a, T: ?Sized + 'a> {
|
||||||
// This is to allow [`ArcBorrow`] (and variants) to be used as the type of `self`.
|
// This is to allow [`ArcBorrow`] (and variants) to be used as the type of `self`.
|
||||||
impl<T: ?Sized> core::ops::Receiver for ArcBorrow<'_, T> {}
|
impl<T: ?Sized> core::ops::Receiver for ArcBorrow<'_, T> {}
|
||||||
|
|
||||||
|
// This is to allow `ArcBorrow<U>` to be dispatched on when `ArcBorrow<T>` can be coerced into
|
||||||
|
// `ArcBorrow<U>`.
|
||||||
|
impl<T: ?Sized + Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<ArcBorrow<'_, U>>
|
||||||
|
for ArcBorrow<'_, T>
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: ?Sized> Clone for ArcBorrow<'_, T> {
|
impl<T: ?Sized> Clone for ArcBorrow<'_, T> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
*self
|
*self
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue