Message ID | 20221228060346.352362-7-wedsonaf@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp1739280wrt; Tue, 27 Dec 2022 22:08:12 -0800 (PST) X-Google-Smtp-Source: AMrXdXsAcc1K9tYKum2W9H9wz0ahjeMB063/4HhcxJG6H75XFSKUCfFuWyQHxxjclUv0kVOtJz3Z X-Received: by 2002:a17:902:7805:b0:191:bb:c207 with SMTP id p5-20020a170902780500b0019100bbc207mr23813791pll.37.1672207692083; Tue, 27 Dec 2022 22:08:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672207692; cv=none; d=google.com; s=arc-20160816; b=eam42zGW5IN2MJ6IyeFg8+XuFBYKL4lMtxHcRc0n0Eki0wuC9PGPIEaRZeHmK/pd23 Kkt2AgdZNsDvxxPd8x6pzTjZ6nuYlpHqJ1dvrGDuxax9HVrxmEEgL1B38P7cebIA/Hlz g61mzBTGtIqwW95SUuQPA2I0AA9uurOCIXb4Q/qP0G3mzRSLp6uf984BhXfBe5mP9Gk2 tT8HQmt5zXrX/0J/jPqT9BJyscVCBZhsPmOCAdPboG3I64gmdcPijzvGMjQjzU1biJaq YRKCd8Da5JEdmqV+KSh84LGWea15VP7zMksOa5aR0l0NttIzSYLgs5s8C2BHk6e0YMT1 1DCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Nyqdaq2S7tC0O5h+EPr9voswYlNOiiQ62YOgK8u+XEY=; b=GZbeLd3QpCCx5cLA8NUQrhaR6F8zHtX+wWkAvJbQEcwbrctSQiRscmTfviyOlnmO+u uUwK8zUjUZAn/jZVGmZfijHw8jg+kgxDSK2uIZEayeDAa4MFtCf6uMST2rzJoSfan9bc gGvwZA5K9BolrZ53BUvtakLGgYLyTVtQO9Q48HPx6ygFGoDC4oPspcVHYcvnqXy6dVbz HddD8yuwYFP5w1rMVfbaSinHTyrxwex5IaM+iZ9i/DdHEAb796PtWq3BxzMDfycVmrqT OmkSoTY9+Xo7CacvPdT96EYbYduvqZlTVVe+dlrBBQK1sWuBCACjFda+OTr5vYpiravm l9gQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=PkjOU0Ow; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o17-20020a170902d4d100b0018701f083b1si17381623plg.619.2022.12.27.22.07.59; Tue, 27 Dec 2022 22:08:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=PkjOU0Ow; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232272AbiL1GFJ (ORCPT <rfc822;eddaouddi.ayoub@gmail.com> + 99 others); Wed, 28 Dec 2022 01:05:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229666AbiL1GEn (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 28 Dec 2022 01:04:43 -0500 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 054C7DF5E; Tue, 27 Dec 2022 22:04:42 -0800 (PST) Received: by mail-wr1-x42a.google.com with SMTP id t15so5238847wro.9; Tue, 27 Dec 2022 22:04:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Nyqdaq2S7tC0O5h+EPr9voswYlNOiiQ62YOgK8u+XEY=; b=PkjOU0OwjgOja8a24OYSeB2xiCXsUcXR2DuCf4HwLICZZ6x5sUmJcWD+n6ZDvBAkTG Q+yDtygd9skAsH7t4D3HxYgI4yhnyQ4mSJt1uW+qYKr5q7Jka2QoKZll1mkxK5Ug149F fO1lUz8ciMe3jG1z4R/He3IGT7X3d09qLVq8L5fdAc+sQv1qBijTYeekmQUnBnE0KJ6y ZxPStzKKAtcEnK1FxUL6vlmqE8oH/vjbQ/iiV0qSrloeoFwlXy3KIVD5QWoGxMAmG4OW y+g3SztzbfqSiPuTK8NIqTg6b99rR+6POIH9XoXMkEZ0Bwj5wbv5fnr8W/bbql43LwpJ +Rug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Nyqdaq2S7tC0O5h+EPr9voswYlNOiiQ62YOgK8u+XEY=; b=onRpjMBBF4HRqEBlmSaNibzSz7COsuAPjn+8KM/7duv2V2aavRDQECpwzr6uPAzMNQ m3OZyoq7nycNz/UiFdYeowxLhvfSb1Wd8qqdhCXPKmKqjFzf685KfVbFIqgyPDRM6utX R4pHfEZn0GPaB8grdVV5/uPklAjPU10qmJj9XCW22Y4liAdHprwfqCuWwmwoSgdURuCB FlII50Eo2L3nOHTDdygMz8S7rkIq2yAd5+cV2tc7zmpOq3cb9IdzSy4j3HdqJgq65OAA oz7heN+OuIBa4utdlA5mkR83P3ezlnJkM0f5tfqOGjUUbdYtyRYae/oHglQfZID29nXu 4L9Q== X-Gm-Message-State: AFqh2kqRX257t2wXcArj2++vzM0+giWLTY7WdBn7Yvbfd+iJdT/eBr7a ItkRxr2I9glTjWUeo2iDBku8dZ86FpwEHA== X-Received: by 2002:a5d:494d:0:b0:268:5557:98c9 with SMTP id r13-20020a5d494d000000b00268555798c9mr14607418wrs.58.1672207480519; Tue, 27 Dec 2022 22:04:40 -0800 (PST) Received: from wedsonaf-dev.. ([81.2.152.129]) by smtp.googlemail.com with ESMTPSA id x16-20020a5d6510000000b002755e301eeasm12128867wru.100.2022.12.27.22.04.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Dec 2022 22:04:40 -0800 (PST) From: Wedson Almeida Filho <wedsonaf@gmail.com> To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda <ojeda@kernel.org>, Alex Gaynor <alex.gaynor@gmail.com>, Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>, =?utf-8?q?B?= =?utf-8?q?j=C3=B6rn_Roy_Baron?= <bjorn3_gh@protonmail.com>, linux-kernel@vger.kernel.org, Wedson Almeida Filho <wedsonaf@gmail.com> Subject: [PATCH 7/7] rust: sync: add support for dispatching on Arc and ArcBorrow. Date: Wed, 28 Dec 2022 06:03:46 +0000 Message-Id: <20221228060346.352362-7-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221228060346.352362-1-wedsonaf@gmail.com> References: <20221228060346.352362-1-wedsonaf@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753436852714896144?= X-GMAIL-MSGID: =?utf-8?q?1753436852714896144?= |
Series |
[1/7] rust: sync: add `Arc` for ref-counted allocations
|
|
Commit Message
Wedson Almeida Filho
Dec. 28, 2022, 6:03 a.m. UTC
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>
---
rust/kernel/lib.rs | 1 +
rust/kernel/sync/arc.rs | 20 ++++++++++++++++++--
2 files changed, 19 insertions(+), 2 deletions(-)
Comments
On Dec 27, 2022, at 10:03 PM, Wedson Almeida Filho <wedsonaf@gmail.com> wrote: > > 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> > --- > rust/kernel/lib.rs | 1 + > rust/kernel/sync/arc.rs | 20 ++++++++++++++++++-- > 2 files changed, 19 insertions(+), 2 deletions(-) > > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index 4bde65e7b06b..e0b0e953907d 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -15,6 +15,7 @@ > #![feature(allocator_api)] > #![feature(coerce_unsized)] > #![feature(core_ffi_c)] > +#![feature(dispatch_from_dyn)] > #![feature(receiver_trait)] > #![feature(unsize)] > > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs > index 832bafc74a90..ff73f9240ca1 100644 > --- a/rust/kernel/sync/arc.rs > +++ b/rust/kernel/sync/arc.rs > @@ -92,9 +92,15 @@ use core::{ > /// Coercion from `Arc<Example>` to `Arc<dyn MyTrait>`: > /// > /// ``` > -/// use kernel::sync::Arc; > +/// use kernel::sync::{Arc, ArcBorrow}; > +/// > +/// trait MyTrait { > +/// // Trait has a function whose `self` type is `Arc<Self>`. > +/// fn example1(self: Arc<Self>) {} > /// > -/// trait MyTrait {} > +/// // Trait has a function whose `self` type is `ArcBorrow<'_, Self>`. > +/// fn example2(self: ArcBorrow<'_, Self>) {} > +/// } > /// > /// struct Example; > /// impl MyTrait for Example {} > @@ -123,6 +129,9 @@ impl<T: ?Sized> core::ops::Receiver for Arc<T> {} > // dynamically-sized type (DST) `U`. > 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 > // 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 > @@ -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`. > 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> These lifetimes need to be tied together, I think. `impl<'a, T: ?Sized + Unsize<U> + 'a, U: ?Sized + 'a> core::ops::DispatchFromDyn<ArcBorrow<'a, U>> for ArcBorrow<'a, T>` > +{ > +} > + > impl<T: ?Sized> Clone for ArcBorrow<'_, T> { > fn clone(&self) -> Self { > *self > -- > 2.34.1 > > — Laine Taffin Altman
Reviewed-by: Alice Ryhl <aliceryhl@google.com> On 12/28/22 07:03, Wedson Almeida Filho wrote: > 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> > --- > rust/kernel/lib.rs | 1 + > rust/kernel/sync/arc.rs | 20 ++++++++++++++++++-- > 2 files changed, 19 insertions(+), 2 deletions(-) > > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index 4bde65e7b06b..e0b0e953907d 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -15,6 +15,7 @@ > #![feature(allocator_api)] > #![feature(coerce_unsized)] > #![feature(core_ffi_c)] > +#![feature(dispatch_from_dyn)] > #![feature(receiver_trait)] > #![feature(unsize)] > > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs > index 832bafc74a90..ff73f9240ca1 100644 > --- a/rust/kernel/sync/arc.rs > +++ b/rust/kernel/sync/arc.rs > @@ -92,9 +92,15 @@ use core::{ > /// Coercion from `Arc<Example>` to `Arc<dyn MyTrait>`: > /// > /// ``` > -/// use kernel::sync::Arc; > +/// use kernel::sync::{Arc, ArcBorrow}; > +/// > +/// trait MyTrait { > +/// // Trait has a function whose `self` type is `Arc<Self>`. > +/// fn example1(self: Arc<Self>) {} > /// > -/// trait MyTrait {} > +/// // Trait has a function whose `self` type is `ArcBorrow<'_, Self>`. > +/// fn example2(self: ArcBorrow<'_, Self>) {} > +/// } > /// > /// struct Example; > /// impl MyTrait for Example {} > @@ -123,6 +129,9 @@ impl<T: ?Sized> core::ops::Receiver for Arc<T> {} > // dynamically-sized type (DST) `U`. > 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 > // 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 > @@ -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`. > 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> { > fn clone(&self) -> Self { > *self
On Tue, 27 Dec 2022 23:24:37 -0800 Laine Taffin Altman <alexanderaltman@me.com> wrote: > > +// 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> > > These lifetimes need to be tied together, I think. > `impl<'a, T: ?Sized + Unsize<U> + 'a, U: ?Sized + 'a> core::ops::DispatchFromDyn<ArcBorrow<'a, U>> for ArcBorrow<'a, T>` I don't think this is necessary, libcore has the following code (https://doc.rust-lang.org/stable/src/core/ops/unsize.rs.html#123): impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> core::ops::DispatchFromDyn<&'a U> for &'a T {} which should be equivalent to Wedson's lifetime elided version. Best, Gary
On Wed, 28 Dec 2022 06:03:46 +0000 Wedson Almeida Filho <wedsonaf@gmail.com> wrote: > 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: Gary Guo <gary@garyguo.net> > --- > rust/kernel/lib.rs | 1 + > rust/kernel/sync/arc.rs | 20 ++++++++++++++++++-- > 2 files changed, 19 insertions(+), 2 deletions(-) > > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index 4bde65e7b06b..e0b0e953907d 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -15,6 +15,7 @@ > #![feature(allocator_api)] > #![feature(coerce_unsized)] > #![feature(core_ffi_c)] > +#![feature(dispatch_from_dyn)] > #![feature(receiver_trait)] > #![feature(unsize)] > > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs > index 832bafc74a90..ff73f9240ca1 100644 > --- a/rust/kernel/sync/arc.rs > +++ b/rust/kernel/sync/arc.rs > @@ -92,9 +92,15 @@ use core::{ > /// Coercion from `Arc<Example>` to `Arc<dyn MyTrait>`: > /// > /// ``` > -/// use kernel::sync::Arc; > +/// use kernel::sync::{Arc, ArcBorrow}; > +/// > +/// trait MyTrait { > +/// // Trait has a function whose `self` type is `Arc<Self>`. > +/// fn example1(self: Arc<Self>) {} > /// > -/// trait MyTrait {} > +/// // Trait has a function whose `self` type is `ArcBorrow<'_, Self>`. > +/// fn example2(self: ArcBorrow<'_, Self>) {} > +/// } > /// > /// struct Example; > /// impl MyTrait for Example {} > @@ -123,6 +129,9 @@ impl<T: ?Sized> core::ops::Receiver for Arc<T> {} > // dynamically-sized type (DST) `U`. > 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 > // 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 > @@ -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`. > 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> { > fn clone(&self) -> Self { > *self
On Wed Dec 28, 2022 at 7:03 AM CET, Wedson Almeida Filho wrote: > 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: Vincenzo Palazzo <vincenzopalazzodev@gmail.com> > --- > rust/kernel/lib.rs | 1 + > rust/kernel/sync/arc.rs | 20 ++++++++++++++++++-- > 2 files changed, 19 insertions(+), 2 deletions(-) > > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index 4bde65e7b06b..e0b0e953907d 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -15,6 +15,7 @@ > #![feature(allocator_api)] > #![feature(coerce_unsized)] > #![feature(core_ffi_c)] > +#![feature(dispatch_from_dyn)] > #![feature(receiver_trait)] > #![feature(unsize)] > > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs > index 832bafc74a90..ff73f9240ca1 100644 > --- a/rust/kernel/sync/arc.rs > +++ b/rust/kernel/sync/arc.rs > @@ -92,9 +92,15 @@ use core::{ > /// Coercion from `Arc<Example>` to `Arc<dyn MyTrait>`: > /// > /// ``` > -/// use kernel::sync::Arc; > +/// use kernel::sync::{Arc, ArcBorrow}; > +/// > +/// trait MyTrait { > +/// // Trait has a function whose `self` type is `Arc<Self>`. > +/// fn example1(self: Arc<Self>) {} > /// > -/// trait MyTrait {} > +/// // Trait has a function whose `self` type is `ArcBorrow<'_, Self>`. > +/// fn example2(self: ArcBorrow<'_, Self>) {} > +/// } > /// > /// struct Example; > /// impl MyTrait for Example {} > @@ -123,6 +129,9 @@ impl<T: ?Sized> core::ops::Receiver for Arc<T> {} > // dynamically-sized type (DST) `U`. > 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 > // 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 > @@ -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`. > 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> { > fn clone(&self) -> Self { > *self > -- > 2.34.1
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 4bde65e7b06b..e0b0e953907d 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -15,6 +15,7 @@ #![feature(allocator_api)] #![feature(coerce_unsized)] #![feature(core_ffi_c)] +#![feature(dispatch_from_dyn)] #![feature(receiver_trait)] #![feature(unsize)] diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index 832bafc74a90..ff73f9240ca1 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -92,9 +92,15 @@ use core::{ /// Coercion from `Arc<Example>` to `Arc<dyn MyTrait>`: /// /// ``` -/// use kernel::sync::Arc; +/// use kernel::sync::{Arc, ArcBorrow}; +/// +/// trait MyTrait { +/// // Trait has a function whose `self` type is `Arc<Self>`. +/// fn example1(self: Arc<Self>) {} /// -/// trait MyTrait {} +/// // Trait has a function whose `self` type is `ArcBorrow<'_, Self>`. +/// fn example2(self: ArcBorrow<'_, Self>) {} +/// } /// /// struct Example; /// impl MyTrait for Example {} @@ -123,6 +129,9 @@ impl<T: ?Sized> core::ops::Receiver for Arc<T> {} // dynamically-sized type (DST) `U`. 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 // 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 @@ -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`. 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> { fn clone(&self) -> Self { *self