From nobody Tue Feb 10 10:54:40 2026 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 383CDC0015E for ; Sat, 29 Jul 2023 09:11:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231621AbjG2JLx (ORCPT ); Sat, 29 Jul 2023 05:11:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231886AbjG2JLW (ORCPT ); Sat, 29 Jul 2023 05:11:22 -0400 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1231249F3; Sat, 29 Jul 2023 02:10:44 -0700 (PDT) Date: Sat, 29 Jul 2023 09:10:22 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1690621836; x=1690881036; bh=AH0HIEGjoQqibf1kDSLrSuvQlmckx0/sN/Y3zMQ9l2E=; 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=e6Ee/Ay28m2aF8g71TkVLHYPgKrHIsI6ZnO0XItdUgFnAlfOZSA0C3ivHqZ7l19nR hbHmpw/U7YDSNwfQChTDKu2zRmAru/SNNYCkExVdJYg91XUqmdTL+Ioi8LoWSjVcLo r7PRiooEkosYRC95ou+HpyIJV+0Bk5JWKIYr2TR4Sv6lrPp851efgebomRNzFnWoEg e9BchDE0QgFYQJewCJ1Go702HyCVikozp1mDXtQ3LhGxmKlc2D22cQHjtjib7jR+Ky 0KetuWGDpxhVa3h6xC0QG9l7I6QmXKayDlv7WOVubrgXIRzgNhrEtHx//WiNnrTikG N+Hff4uUNhzjA== To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor From: Benno Lossin Cc: Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Martin Rodriguez Reboredo Subject: [PATCH v3 11/13] rust: init: make `PinInit` a supertrait of `Init` Message-ID: <20230729090838.225225-12-benno.lossin@proton.me> In-Reply-To: <20230729090838.225225-1-benno.lossin@proton.me> References: <20230729090838.225225-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Remove the blanket implementation of `PinInit for I where I: Init`. This blanket implementation prevented custom types that implement `PinInit`. Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Alice Ryhl Signed-off-by: Benno Lossin Reviewed-by: Gary Guo --- v2 -> v3: - add blank missing line, - added Reviewed-by's from Martin and Alice. rust/kernel/init.rs | 21 ++++++++------------- rust/kernel/init/__internal.rs | 12 ++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index 06ecab4901f2..040dc9a5f9fd 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -807,7 +807,7 @@ pub unsafe trait PinInit: = Sized { /// /// [`Arc`]: crate::sync::Arc #[must_use =3D "An initializer must be used in order to create its value."] -pub unsafe trait Init: Sized { +pub unsafe trait Init: PinInit { /// Initializes `slot`. /// /// # Safety @@ -818,18 +818,6 @@ pub unsafe trait Init: Si= zed { unsafe fn __init(self, slot: *mut T) -> Result<(), E>; } =20 -// SAFETY: Every in-place initializer can also be used as a pin-initialize= r. -unsafe impl PinInit for I -where - I: Init, -{ - unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> { - // SAFETY: `__init` meets the same requirements as `__pinned_init`= , except that it does not - // require `slot` to not move after init. - unsafe { self.__init(slot) } - } -} - /// Creates a new [`PinInit`] from the given closure. /// /// # Safety @@ -971,6 +959,13 @@ unsafe fn __init(self, slot: *mut T) -> Result<(), E> { } } =20 +// SAFETY: Every type can be initialized by-value. `__pinned_init` calls `= __init`. +unsafe impl PinInit for T { + unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> { + unsafe { self.__init(slot) } + } +} + /// 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 diff --git a/rust/kernel/init/__internal.rs b/rust/kernel/init/__internal.rs index 7abd1fb65e41..12e195061525 100644 --- a/rust/kernel/init/__internal.rs +++ b/rust/kernel/init/__internal.rs @@ -32,6 +32,18 @@ unsafe fn __init(self, slot: *mut T) -> Result<(), E> { } } =20 +// SAFETY: While constructing the `InitClosure`, the user promised that it= upholds the +// `__pinned_init` invariants. +unsafe impl PinInit for InitClosure +where + F: FnOnce(*mut T) -> Result<(), E>, +{ + #[inline] + unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> { + (self.0)(slot) + } +} + /// This trait is only implemented via the `#[pin_data]` proc-macro. It is= used to facilitate /// the pin projections within the initializers. /// --=20 2.41.0