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 BA986C6FD1D for ; Tue, 21 Mar 2023 19:50:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229906AbjCUTuR (ORCPT ); Tue, 21 Mar 2023 15:50:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229841AbjCUTuG (ORCPT ); Tue, 21 Mar 2023 15:50:06 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 594A69EF2 for ; Tue, 21 Mar 2023 12:50:02 -0700 (PDT) Date: Tue, 21 Mar 2023 19:49:55 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1679428200; x=1679687400; bh=5wFTfQvkkunlIrsARX4Vq7hYHVmiJWCfu28xcLcdviI=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=yo3gFfxMfaxuil6kpYNQ51W526MtY1XzriLAqtUMsU0wfTlOKpcjd6bWdEB4zJvCK EO9rf+vJFjiQCEsarmSXWn1yMJLfbMI+/d+ufWy9ZgpjQRMnVu0eymjMAT6UJ9kPmF LiNAF8i9KPRDecEJlTM5LEaW0nlUmq78H/IFkQXYSRGamiWBT448a9HLgwC2QZZoLu sIE/ivUqLaPUY5pKp8CL1RGEPoH72x0VjHLExdvu/7Ga+LX1U8sbDGK5L4MdDzZ2ed xZzPxbWPYTiTZP9KSs6dNPwJqlalOo1Zoysefsd0mVxf45dCAA4LI6KH0ahkrPnTJX MVu5aeUxjc+aA== To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v2 2/5] rust: sync: add `assume_init` to `UniqueArc` Message-ID: <20230321194934.908891-3-y86-dev@protonmail.com> In-Reply-To: <20230321194934.908891-1-y86-dev@protonmail.com> References: <20230321194934.908891-1-y86-dev@protonmail.com> Feedback-ID: 40624463:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Adds the `assume_init` function to `UniqueArc>` that unsafely assumes the value to be initialized and yields a value of type `UniqueArc`. This function is used when manually initializing the pointee of an `UniqueArc`. Signed-off-by: Benno Lossin --- rust/kernel/sync/arc.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index f2f1c83d72ba..16ec174637b2 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -489,6 +489,17 @@ impl UniqueArc> { /// Converts a `UniqueArc>` into a `UniqueArc` by wr= iting a value into it. pub fn write(mut self, value: T) -> UniqueArc { self.deref_mut().write(value); + // SAFETY: We just wrote the value to be initialized. + unsafe { self.assume_init() } + } + + /// Unsafely assume that `self` is initialized. + /// + /// # Safety + /// + /// The caller guarantees that the value behind this pointer has been = initialized. It is + /// *immediate* UB to call this when the value is not initialized. + pub unsafe fn assume_init(self) -> UniqueArc { let inner =3D ManuallyDrop::new(self).inner.ptr; UniqueArc { // SAFETY: The new `Arc` is taking over `ptr` from `self.inner= ` (which won't be -- 2.39.2