From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4713EC46467 for ; Wed, 28 Dec 2022 10:02:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232633AbiL1KCL (ORCPT ); Wed, 28 Dec 2022 05:02:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232617AbiL1KCK (ORCPT ); Wed, 28 Dec 2022 05:02:10 -0500 Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C095B97; Wed, 28 Dec 2022 02:02:09 -0800 (PST) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 38E795C00C3; Wed, 28 Dec 2022 05:02:09 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Wed, 28 Dec 2022 05:02:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ryhl.io; h=cc:cc :content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; t=1672221729; x= 1672308129; bh=zJ9H10w8qH7GxLiJRMvCYl4baqrj5iP/EW2fYSs9PSs=; b=a KUHWAmr7o2bzQngALX/NHEuntIkv1PPeQDCFhsjfa2t8mBAngQJSzZLFxxSquQSp jnzulQWKjDAMvkpujaqrIR3bFSlHrQzzerIIz3oduqoMxMDC/l1oneqqagVDYJjI dD/zVguOfoYNoJotAGwCWdnLV98JZiZjZraUoUd+dI1DV3HSf4OzSGWhHvpaMBdN TQGlWk5M0Ke49ZaQ/SdrGjJ9t5U9NiMIVPjqyHRjp32w0rZVLmf2ITP4uHNYULU/ 4MdGFBe+XuBB2/8RuMZO9r55MAH8yw5D07g43TIIBz7aZ7lTIiXt32l/qZkWYa8G AzMnf2gV7lkLL05dr3t8g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1672221729; x= 1672308129; bh=zJ9H10w8qH7GxLiJRMvCYl4baqrj5iP/EW2fYSs9PSs=; b=Z 5qw//C08QpwpXq9liGB0BlM3E4+52CPMNN1nbTDwqVevOwJl3Vuk1D5QZ7yhaNRD SzyHMYUJoeGL/9UZQQX7MoIRRvw1or+2LX4xeZP0/YUop/tqbch2/LVh6OsPBD6c 1+HMvQ25nR1oZJncMyXxsC+1wdrnlSFyHxt9WdU7oQPBARfr1LRhENUv2Kk/W+7p SCCfJh/VO+5l/JclVed2f32xe00GZKiZ8T8B9dgO25PU28vt7dyxSGrpmMdH+JC0 +uP7t2zB30T7Mh8NJrTFAkCDnoaVpeBoUBrXCm5SWpcHnt55dH/b/Tw1yd1YXXbQ sodilPgoAzjGcpsAACOBg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedriedvgdduudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefkffggfgfhuffvvehfjggtgfesthejredttdefjeenucfhrhhomheptehlihgt vgcutfihhhhluceorghlihgtvgesrhihhhhlrdhioheqnecuggftrfgrthhtvghrnhephf ehueeileevjeefkeetvdffveffudeuhffgtedvuefgiefgiedvjeegvdejleejnecuvehl uhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprghlihgtvgesrh ihhhhlrdhioh X-ME-Proxy: Feedback-ID: i56684263:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 28 Dec 2022 05:02:07 -0500 (EST) Message-ID: Date: Wed, 28 Dec 2022 11:03:43 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.1 From: Alice Ryhl Subject: Re: [PATCH 2/7] rust: sync: allow type of `self` to be `Arc` or variants To: Wedson Almeida Filho Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=c3=b6rn_Roy_Baron?= , linux-kernel@vger.kernel.org References: <20221228060346.352362-1-wedsonaf@gmail.com> <20221228060346.352362-2-wedsonaf@gmail.com> Content-Language: en-US In-Reply-To: <20221228060346.352362-2-wedsonaf@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: rust-for-linux@vger.kernel.org Reviewed-by: Alice Ryhl On 12/28/22 07:03, Wedson Almeida Filho wrote: > This allows associated functions whose `self` argument has `Arc` or > variants as their type. This, in turn, allows callers to use the dot > syntax to make calls. > > Signed-off-by: Wedson Almeida Filho > --- > rust/kernel/lib.rs | 1 + > rust/kernel/sync/arc.rs | 28 ++++++++++++++++++++++++++++ > 2 files changed, 29 insertions(+) > > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index ace064a3702a..1a10f7c0ddd9 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -14,6 +14,7 @@ > #![no_std] > #![feature(allocator_api)] > #![feature(core_ffi_c)] > +#![feature(receiver_trait)] > > // 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 22290eb5ab9b..e2eb0e67d483 100644 > --- a/rust/kernel/sync/arc.rs > +++ b/rust/kernel/sync/arc.rs > @@ -57,6 +57,31 @@ use core::{marker::PhantomData, ops::Deref, ptr::NonNull}; > /// > /// // The refcount drops to zero when `cloned` goes out of scope, and the memory is freed. > /// ``` > +/// > +/// Using `Arc` as the type of `self`: > +/// > +/// ``` > +/// use kernel::sync::Arc; > +/// > +/// struct Example { > +/// a: u32, > +/// b: u32, > +/// } > +/// > +/// impl Example { > +/// fn take_over(self: Arc) { > +/// // ... > +/// } > +/// > +/// fn use_reference(self: &Arc) { > +/// // ... > +/// } > +/// } > +/// > +/// let obj = Arc::try_new(Example { a: 10, b: 20 })?; > +/// obj.use_reference(); > +/// obj.take_over(); > +/// ``` > pub struct Arc { > ptr: NonNull>, > _p: PhantomData>, > @@ -68,6 +93,9 @@ struct ArcInner { > data: T, > } > > +// This is to allow [`Arc`] (and variants) to be used as the type of `self`. > +impl core::ops::Receiver 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