Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5970632rwl; Tue, 4 Apr 2023 06:18:15 -0700 (PDT) X-Google-Smtp-Source: AKy350b9IyWePDKHe6HCvDCUvsIlDRhkLatgmKlUy0DgqEt+nsuw3e75Fwzio7oJhbXvTPbeRnGv X-Received: by 2002:a17:906:e28a:b0:93a:219d:b167 with SMTP id gg10-20020a170906e28a00b0093a219db167mr2065466ejb.52.1680614295591; Tue, 04 Apr 2023 06:18:15 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1680614295; cv=pass; d=google.com; s=arc-20160816; b=OyRAe3Ki9jDDDcibTucE6T7q/tQypEQK3W/6kLzTPmVJaLBBV59R7cSQzUQaqdhuJf Cq58R+DAGQIzo2Zfquytz7y99b6dI+3lm7FoC0mysTttw54y4JrWkOYyCH1JEHCtm4Dn 1YgxYHRSZW106DOtfGcOs/YE6/W7aeCW5lzKFqFZiP3Pyhj4MUXWuuXIDDRLUT/ZjGGo IBuUM/t9plk61Ay/HpUcq4Ce2xnUoxeZ7Jq8xGvjgfvnMcN/kJWmCcI30FDr2QMWG0Fl 4Swolk0gP6dz8QeG7m3uP7JsMclfby41Jq8TM9tujvttrQqh+AAlwgBj6D6Jvt4S00dW p5SA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=XHMhtOPBA/eYpcZhMmy+Db7kUrCfb5rfDR3vRAtR+0o=; b=sR0Hf2RZ1tIGkbfUK9jzS7OUf05uO04rtzhbl8/lnpVAFWaLFRt3LEZMg5Ek+UZn7h F6I6hFAT+Z2hw5aT4ui9P5Z+zOjjVFpQb5ttIvywfy48A3qxr08BTcQgbUJZt8zg16GG /vHsDsvoGjguwSNpXJFLmfPB0bFiRl4lfP3c6grvHZVlKs5odxQUUHHjqq5vYwhGY5rq +uLE5rcheOYhdYlHoCYUN0CVDc8b8/0ps5J2LGtkAvVvOKJzxwQzC5cG7kCjLc+f1wIn gffXHG+1/cF2WOh36H3a3HQ43JoWDcuhlomcIHoZOtOVfW1fYsKmfLS1qRlw3NvNbDsl wPwQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@garyguo.net header.s=selector1 header.b=yuqgxAYH; arc=pass (i=1 spf=pass spfdomain=garyguo.net dkim=pass dkdomain=garyguo.net dmarc=pass fromdomain=garyguo.net); 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f25-20020a170906c09900b008ddf3c18301si420868ejz.653.2023.04.04.06.17.50; Tue, 04 Apr 2023 06:18:15 -0700 (PDT) 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=@garyguo.net header.s=selector1 header.b=yuqgxAYH; arc=pass (i=1 spf=pass spfdomain=garyguo.net dkim=pass dkdomain=garyguo.net dmarc=pass fromdomain=garyguo.net); 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235094AbjDDNQa (ORCPT + 99 others); Tue, 4 Apr 2023 09:16:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235097AbjDDNQ0 (ORCPT ); Tue, 4 Apr 2023 09:16:26 -0400 Received: from GBR01-LO2-obe.outbound.protection.outlook.com (mail-lo2gbr01on070c.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe15::70c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9EDB30F5; Tue, 4 Apr 2023 06:16:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TeIQQiOCVU5bwKOEYdGNmj17ofKvIWSB88CER9EtZbSRNr52rNCCtXGM4JZJcsmIHSdGTo9etCDmU+mF4fIGS3rgBL4hUz+1oAeZZV+rV2bqL2GdGVtSDMxuskBXvKpRGPWpf8r/KIfiahJaaLPSVDiGkQda8e/5axWnFtFrPLB4sRQdVUszFU2ievXHIU8Orr8l9Ic2Pr9S4RVA590emXPqdUtCwetP1crCr56OMS/5QX0xGXcTpoa10013bu2qTej1OZcn8qqC0tsj46FJqqYWh10z7i8HL3oxkSBlQ4Jn+S+WdaSok2395DAFRwJxdVLL1yLetVxFSM7fJLKaxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=XHMhtOPBA/eYpcZhMmy+Db7kUrCfb5rfDR3vRAtR+0o=; b=OX0ydf5+f9e+0b/me43hLJMD2kSzcArV3Gkuy8IOJlmNdDEqsJTVnxD8Qo+2uVthH2K02qxe8rfZwq0rehMpx7SJjacww6sUEHE7vy5kkLLJeqz/re8ELjR3CrQdJ8Nywit/RK7bBvUTaJvLsF7V8t95+Bn15uHsKlG9gcP9NIfdKEo0qSmwYbY0wUusWsNXZZby/VjRRjW9ekoCWbxETHisJwQ0MfjGO2TYXbAm4rMpUzhqKUoNI2L3ACUOvt8JOABiBI0AnIdG5t1aSW+A0aN2hpzITe3qroLyuMdzjZla06+7gKwVTueSsYL9t4DUnJaofV+zZ2IlilT04pNg5g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=garyguo.net; dmarc=pass action=none header.from=garyguo.net; dkim=pass header.d=garyguo.net; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=garyguo.net; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=XHMhtOPBA/eYpcZhMmy+Db7kUrCfb5rfDR3vRAtR+0o=; b=yuqgxAYH7WP45QtH8pNNwT99BFaRRah7wd+mDvOmrR15eYednqll5LJ0YLZvOWCEWXfSaQoxurIt6/BA2b815EPxOQfkq3wk+uJvAv37732tcYooCEUF2JjWtefKPoxJp9O3A8a3NTmGleD4ihO8yXoFhZAn+s+vhyqoN2XBjQk= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=garyguo.net; Received: from LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:253::10) by LO2P265MB6309.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:254::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.35; Tue, 4 Apr 2023 13:15:51 +0000 Received: from LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM ([fe80::e1a3:5e38:b483:8161]) by LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM ([fe80::e1a3:5e38:b483:8161%5]) with mapi id 15.20.6254.035; Tue, 4 Apr 2023 13:15:51 +0000 Date: Tue, 4 Apr 2023 14:15:49 +0100 From: Gary Guo To: Benno Lossin Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , =?UTF-8?B?QmrDtnJu?= Roy Baron , Alice Ryhl , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Andreas Hindborg , Alice Ryhl Subject: Re: [PATCH v5 08/15] rust: init/sync: add `InPlaceInit` trait to pin-initialize smart pointers Message-ID: <20230404141549.48230994.gary@garyguo.net> In-Reply-To: <20230403160511.174894-1-y86-dev@protonmail.com> References: <20230403154422.168633-1-y86-dev@protonmail.com> <20230403160511.174894-1-y86-dev@protonmail.com> X-Mailer: Claws Mail 4.0.0 (GTK+ 3.24.33; x86_64-pc-linux-gnu) Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-ClientProxiedBy: LO4P123CA0305.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:196::22) To LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:253::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LO2P265MB5183:EE_|LO2P265MB6309:EE_ X-MS-Office365-Filtering-Correlation-Id: 61f50f6a-6e15-4727-7b62-08db350eb6af X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yipFxJbAp/yQ/1UvKyGoq+9HOsoLI6IGs4geWEgflnSkgy/ogbm4Ds8yvhOy0tknaTcna/+vcvdTGAOBWdDWW7r0l2h5QHk6RBIt8CKdXV9jr1Ovjjc6DJ2eDk59LUohPbQFjX3Dh48lb4aVDXJvsfd76mR5Ndn666ZSf+QO+CjGTTck38fTFten5/ZP//U96iSpG7sQs8wgHpWWVS7CNhLP5ZjrPe5FyAGI8UpEEf86Y5KQwiTencJHfDrIwEghbQAgPAaCmkLTLBpPFDvGHRpZYw6P8Xo3BklxBhgoUCgUJWESuE+0yohcFfVqyVJwHJnFWmQHCXopnWjiIWVKGiRkKFCMMoF9wOSDurGmf2XyJa4aQftSWKlTKAD/xYdU/RNTRnEG3OMl+tf79OI5GwNtgy7Rshi/0FkRTX//h0RiVAjFqk/WE9ke7iSIuz0zYZOLYvHhatng+TfHWOQq4c0c1G1EuSsmH7ElVDk7gap0+B6sdxurRL//QfwM2FxYQP70pYdgdnulCXsGTCFmrjE5Jm4TcPWMsaTKO/SOqmd+lpovgyj8+lc/M1OKI/LgvewbPPRZr8596LCwpGU3vMam1c8UY1g+IgSilqiyIdU= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230028)(6029001)(39830400003)(396003)(366004)(346002)(376002)(136003)(451199021)(86362001)(36756003)(2906002)(66899021)(2616005)(6486002)(186003)(83380400001)(6512007)(26005)(1076003)(6506007)(4326008)(8676002)(66946007)(66476007)(66556008)(478600001)(7416002)(41300700001)(38100700002)(316002)(6916009)(54906003)(5660300002)(8936002)(81973001);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?Cc4e37kMYo6YMdOKY7iBE+21dQ5G2sFy/27E9xSUEnACQbvYHLHWsdPm/43/?= =?us-ascii?Q?NAqMScAIXcfoQt+aplS7mmO68x6cUar0KNhD3p0up9bCH1ym0sQBtNUj+Whp?= =?us-ascii?Q?3tJfFo09as6Yp1Jx5mrQVnUycdhxj24ls/8Jhh2sx/vb4m3OjRiTXAXkwcq2?= =?us-ascii?Q?GQIRxo7jzPVGgquwTnV0XUCw/9aEpOo2R3N4OjxcTjH1DTE9+NCZwtAWh60u?= =?us-ascii?Q?p93JmHAcjkBR5LF1+cU4/ET7FyBD7jx4X0y381j9owXF/ULSvZTvPhRvsOKU?= =?us-ascii?Q?o8lNzIx+uHgEGkM60ULiahyDXY6+G4aahq29vovrZX7ui+5q5rdA0Kc5THpd?= =?us-ascii?Q?wiZtBnYEc5tOfPciK0EYCab37jBac4Fd+ZUIBwY1Yq2SV5DaEH305WugdRjp?= =?us-ascii?Q?kiSwWehflVpdjt9TtPDoXmAHHNmWewSnIuz1S7D/fcD/rX3tTdY4ZP/4W5e+?= =?us-ascii?Q?bp8IAAWkY4Tzz3/aDOXwiokbfk6RHkgxhjz7XqMcMgpYb+oInbsEtNIfucZh?= =?us-ascii?Q?FTEtR544BhlNJrnQVzMVEkGbib99teI6vZKnLwiu9RO5GrKVSP2SKhWkGo70?= =?us-ascii?Q?NxH25wZr5TAMIZmdy0S5DZyxFOVABO7o2oSAleeYQYzgYPtZHUy2NhLqS5oj?= =?us-ascii?Q?G3x6Q8Vt/JdhZYKJQLd3Iap+Y9O0Y50PSTSwSo2gL2PTj2ry7oFHUfH1yOC7?= =?us-ascii?Q?82Ug0wD5roSbAUCBjTUdQdXmtNb+7zbwv1IBCPWavxhnB8e1rmNgOH4cBDX9?= =?us-ascii?Q?A1vYkCVoKmVMuuqBPGJ9rdcaiwk5skLeurgAEgdbe7eYq4i7KOUCX0qXsiV4?= =?us-ascii?Q?pSGWK1bCqF5vJ2Z2I6OQG8kpbZpi9mgulSNJfTycTs/lFG3oo4l9lDyFGJut?= =?us-ascii?Q?34Qn3W7h+rYW5wWtLtb3wOwSwM6ckCUkuwdtTCioEWAdokDdCSlIMShH80gO?= =?us-ascii?Q?w1vMlzLS5Kpyv+ibwoRhTbfaEF9tWOePadpk/0F6XQWeeBx4xR5iy9dcGuil?= =?us-ascii?Q?RQDfnesWD+nZDsZ1Z6BSTvXgXpAfmY/uhwT92bE/0ge3SsUgthK3l29Mdx3X?= =?us-ascii?Q?UQdp9vAA5HNq8edJZwpk5fRI27hqvTws67bsvQo5UQbwE1CAozuJl9P8wdzx?= =?us-ascii?Q?UIoIDzgaWGT0n6o9Rt/kB6DAQ6+ktVLdUyHZMqxN7+MV0M3ZO+wfoMvLF/+C?= =?us-ascii?Q?wWVt2jS2cL1EQ9puKhqz28j7O+wBREMEDby1MPqruRZ1+Q5WQSKIP31Mjrqa?= =?us-ascii?Q?v9rRySMqeDRs+WKnIYRQl3xwVYeBR2eDrP7FsfY3OiZdQSHIw9Y+5iWVFNok?= =?us-ascii?Q?ePBlRaHaUzDfx4YLwSpym9E6KmX+Sk1UBhOATOv8hUDwD+/f4Jvnv2qrpvew?= =?us-ascii?Q?Luqnuvj4wFxCTljaTxup5bnRrXe3fFt4jFg6CxevYHFy5Yy76nbZ4Ga5slN5?= =?us-ascii?Q?kyo3P+31qMPDaHGqORpAWurojYtQn+9OrG1fVJp856nEL+FmaoYm+ZaI5a79?= =?us-ascii?Q?qbCCTPOviiNO1XdF1ZknhTUfwvaaQlrzN8ounNPxHygtZ9cmCgG9kaKJeiB1?= =?us-ascii?Q?UJapJaHUwqAco2th9QRi4ejzbv3EWt7AAkOXuEIZ?= X-OriginatorOrg: garyguo.net X-MS-Exchange-CrossTenant-Network-Message-Id: 61f50f6a-6e15-4727-7b62-08db350eb6af X-MS-Exchange-CrossTenant-AuthSource: LO2P265MB5183.GBRP265.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Apr 2023 13:15:51.5466 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bbc898ad-b10f-4e10-8552-d9377b823d45 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: vOmB6mJuVP+P35mDNv7kNUpWpnB0nfbFZNh0YC+MDwJE3BogyZs5jp+1WKMUSjyHSqZcrh0fRUOCiiTNuxkQzA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: LO2P265MB6309 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS autolearn=unavailable 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: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 03 Apr 2023 16:05:22 +0000 Benno Lossin wrote: > The `InPlaceInit` trait that provides two functions, for initializing > using `PinInit` and `Init`. It is implemented by `Arc`, > `UniqueArc` and `Box`. > > Signed-off-by: Benno Lossin > Cc: Andreas Hindborg > Cc: Alice Ryhl > Cc: Gary Guo Reviewed-by: Gary Guo > --- > rust/kernel/init.rs | 128 ++++++++++++++++++++++++++++++++++++---- > rust/kernel/sync/arc.rs | 24 ++++++++ > 2 files changed, 139 insertions(+), 13 deletions(-) > > diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs > index ecef0376d726..6499cf5c9c20 100644 > --- a/rust/kernel/init.rs > +++ b/rust/kernel/init.rs > @@ -114,10 +114,16 @@ > //! [`impl Init`]: Init > //! [`Opaque`]: kernel::types::Opaque > //! [`pin_data`]: ::macros::pin_data > -//! [`UniqueArc`]: kernel::sync::UniqueArc > > +use crate::{ > + error::{self, Error}, > + sync::UniqueArc, > +}; > use alloc::boxed::Box; > -use core::{cell::Cell, convert::Infallible, marker::PhantomData, mem::MaybeUninit, ptr}; > +use core::{ > + alloc::AllocError, cell::Cell, convert::Infallible, marker::PhantomData, mem::MaybeUninit, > + pin::Pin, ptr, > +}; > > #[doc(hidden)] > pub mod __internal; > @@ -309,7 +315,6 @@ pub mod macros; > /// > /// [`try_pin_init!`]: kernel::try_pin_init > /// [`NonNull`]: core::ptr::NonNull > -/// [`Error`]: kernel::error::Error > // For a detailed example of how this macro works, see the module documentation of the hidden > // module `__internal` inside of `init/__internal.rs`. > #[macro_export] > @@ -363,8 +368,6 @@ macro_rules! pin_init { > /// } > /// } > /// ``` > -/// > -/// [`Error`]: kernel::error::Error > // For a detailed example of how this macro works, see the module documentation of the hidden > // module `__internal` inside of `init/__internal.rs`. > #[macro_export] > @@ -586,8 +589,6 @@ macro_rules! try_pin_init { > /// > /// This initializer is for initializing data in-place that might later be moved. If you want to > /// pin-initialize, use [`pin_init!`]. > -/// > -/// [`Error`]: kernel::error::Error > // For a detailed example of how this macro works, see the module documentation of the hidden > // module `__internal` inside of `init/__internal.rs`. > #[macro_export] > @@ -635,8 +636,6 @@ macro_rules! init { > /// } > /// } > /// ``` > -/// > -/// [`Error`]: kernel::error::Error > // For a detailed example of how this macro works, see the module documentation of the hidden > // module `__internal` inside of `init/__internal.rs`. > #[macro_export] > @@ -842,7 +841,8 @@ macro_rules! try_init { > /// A pin-initializer for the type `T`. > /// > /// To use this initializer, you will need a suitable memory location that can hold a `T`. This can > -/// be [`Box`], [`Arc`], [`UniqueArc`]. > +/// be [`Box`], [`Arc`], [`UniqueArc`]. Use the [`InPlaceInit::pin_init`] function of a > +/// smart pointer like [`Arc`] on this. > /// > /// Also see the [module description](self). > /// > @@ -861,7 +861,6 @@ macro_rules! try_init { > /// > /// [`Arc`]: crate::sync::Arc > /// [`Arc::pin_init`]: crate::sync::Arc::pin_init > -/// [`UniqueArc`]: kernel::sync::UniqueArc > #[must_use = "An initializer must be used in order to create its value."] > pub unsafe trait PinInit: Sized { > /// Initializes `slot`. > @@ -878,7 +877,8 @@ pub unsafe trait PinInit: Sized { > /// An initializer for `T`. > /// > /// To use this initializer, you will need a suitable memory location that can hold a `T`. This can > -/// be [`Box`], [`Arc`], [`UniqueArc`]. Because [`PinInit`] is a super trait, you can > +/// be [`Box`], [`Arc`], [`UniqueArc`]. Use the [`InPlaceInit::init`] function of a smart > +/// pointer like [`Arc`] on this. Because [`PinInit`] is a super trait, you can > /// use every function that takes it as well. > /// > /// Also see the [module description](self). > @@ -903,7 +903,6 @@ pub unsafe trait PinInit: Sized { > /// move the pointee after initialization. > /// > /// [`Arc`]: crate::sync::Arc > -/// [`UniqueArc`]: kernel::sync::UniqueArc > #[must_use = "An initializer must be used in order to create its value."] > pub unsafe trait Init: Sized { > /// Initializes `slot`. > @@ -982,3 +981,106 @@ unsafe impl Init for T { > Ok(()) > } > } > + > +/// Smart pointer that can initialize memory in-place. > +pub trait InPlaceInit: Sized { > + /// Use the given pin-initializer to pin-initialize a `T` inside of a new smart pointer of this > + /// type. > + /// > + /// If `T: !Unpin` it will not be able to move afterwards. > + fn try_pin_init(init: impl PinInit) -> Result, E> > + where > + E: From; > + > + /// Use the given pin-initializer to pin-initialize a `T` inside of a new smart pointer of this > + /// type. > + /// > + /// If `T: !Unpin` it will not be able to move afterwards. > + fn pin_init(init: impl PinInit) -> error::Result> > + where > + Error: From, > + { > + // SAFETY: We delegate to `init` and only change the error type. > + let init = unsafe { > + pin_init_from_closure(|slot| init.__pinned_init(slot).map_err(|e| Error::from(e))) > + }; > + Self::try_pin_init(init) > + } > + > + /// Use the given initializer to in-place initialize a `T`. > + fn try_init(init: impl Init) -> Result > + where > + E: From; > + > + /// Use the given initializer to in-place initialize a `T`. > + fn init(init: impl Init) -> error::Result > + where > + Error: From, > + { > + // SAFETY: We delegate to `init` and only change the error type. > + let init = unsafe { > + init_from_closure(|slot| init.__pinned_init(slot).map_err(|e| Error::from(e))) > + }; > + Self::try_init(init) > + } > +} > + > +impl InPlaceInit for Box { > + #[inline] > + fn try_pin_init(init: impl PinInit) -> Result, E> > + where > + E: From, > + { > + let mut this = Box::try_new_uninit()?; > + let slot = this.as_mut_ptr(); > + // SAFETY: When init errors/panics, slot will get deallocated but not dropped, > + // slot is valid and will not be moved, because we pin it later. > + unsafe { init.__pinned_init(slot)? }; > + // SAFETY: All fields have been initialized. > + Ok(unsafe { this.assume_init() }.into()) > + } > + > + #[inline] > + fn try_init(init: impl Init) -> Result > + where > + E: From, > + { > + let mut this = Box::try_new_uninit()?; > + let slot = this.as_mut_ptr(); > + // SAFETY: When init errors/panics, slot will get deallocated but not dropped, > + // slot is valid. > + unsafe { init.__init(slot)? }; > + // SAFETY: All fields have been initialized. > + Ok(unsafe { this.assume_init() }) > + } > +} > + > +impl InPlaceInit for UniqueArc { > + #[inline] > + fn try_pin_init(init: impl PinInit) -> Result, E> > + where > + E: From, > + { > + let mut this = UniqueArc::try_new_uninit()?; > + let slot = this.as_mut_ptr(); > + // SAFETY: When init errors/panics, slot will get deallocated but not dropped, > + // slot is valid and will not be moved, because we pin it later. > + unsafe { init.__pinned_init(slot)? }; > + // SAFETY: All fields have been initialized. > + Ok(unsafe { this.assume_init() }.into()) > + } > + > + #[inline] > + fn try_init(init: impl Init) -> Result > + where > + E: From, > + { > + let mut this = UniqueArc::try_new_uninit()?; > + let slot = this.as_mut_ptr(); > + // SAFETY: When init errors/panics, slot will get deallocated but not dropped, > + // slot is valid. > + unsafe { init.__init(slot)? }; > + // SAFETY: All fields have been initialized. > + Ok(unsafe { this.assume_init() }) > + } > +} > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs > index eee7008e5e3e..43a53fbe175d 100644 > --- a/rust/kernel/sync/arc.rs > +++ b/rust/kernel/sync/arc.rs > @@ -17,6 +17,8 @@ > > use crate::{ > bindings, > + error::{self, Error}, > + init::{InPlaceInit, Init, PinInit}, > types::{ForeignOwnable, Opaque}, > }; > use alloc::boxed::Box; > @@ -163,6 +165,28 @@ impl Arc { > // `Arc` object. > Ok(unsafe { Self::from_inner(Box::leak(inner).into()) }) > } > + > + /// Use the given initializer to in-place initialize a `T`. > + /// > + /// If `T: !Unpin` it will not be able to move afterwards. > + #[inline] > + pub fn pin_init(init: impl PinInit) -> error::Result > + where > + Error: From, > + { > + UniqueArc::pin_init(init).map(|u| u.into()) > + } > + > + /// Use the given initializer to in-place initialize a `T`. > + /// > + /// This is equivalent to [`pin_init`], since an [`Arc`] is always pinned. > + #[inline] > + pub fn init(init: impl Init) -> error::Result > + where > + Error: From, > + { > + UniqueArc::init(init).map(|u| u.into()) > + } > } > > impl Arc { > -- > 2.39.2 > >