From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CFDD4156D2 for ; Wed, 19 Jul 2023 14:22:13 +0000 (UTC) Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E01B5198D; Wed, 19 Jul 2023 07:21:54 -0700 (PDT) Date: Wed, 19 Jul 2023 14:21:19 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1689776494; x=1690035694; bh=PY3Ej/DIQyjr1nZd2KtYmX46DbaLv5+ISgZDgCfGk58=; 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=JbZ3uC2P97beRJVTZB8zzWyQWqTrD2X3DqO614uvYpMuDtjxTr8BcSXwvtmuhFBe6 C7yftw8MTtWqDeWbJA+4c5RRhAxTYBJBRKv8bo8z05YKe/gqdOKV+TKqA4duvrxZIn wcC9LKyrbxSxp8GlSIHztsAgFe51qHjkWNc51k1AS9SUQduqUH4bDCEyqvX0r0qHn3 BpTfDawsATp14Zoz5DH5WH9XkeUOTkkZdyXX59ofNUgelkkhVs9nen57LuxEfgYHx/ 9N0PZbP76v0NSO8drATOMc1SxQnzzngSkeYBtqoEWsV3DZlgaoqkXiEMWEwFgJLER2 6vJwO4xk9yv0A== 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 Subject: [PATCH v2 10/12] rust: init: make `PinInit` a supertrait of `Init` Message-ID: <20230719141918.543938-11-benno.lossin@proton.me> In-Reply-To: <20230719141918.543938-1-benno.lossin@proton.me> References: <20230719141918.543938-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Remove the blanket implementation of `PinInit for I where I: Init`. This blanket implementation prevented custom types that implement `PinInit`. Signed-off-by: Benno Lossin --- rust/kernel/init.rs | 20 +++++++------------- rust/kernel/init/__internal.rs | 12 ++++++++++++ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index fa1ebdbf5f4b..3c7cd36a424b 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -805,7 +805,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 @@ -816,18 +816,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 @@ -968,6 +956,12 @@ unsafe fn __init(self, slot: *mut T) -> Result<(), E> = { Ok(()) } } +// 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) } + } +} =20 /// Smart pointer that can initialize memory in-place. pub trait InPlaceInit: Sized { diff --git a/rust/kernel/init/__internal.rs b/rust/kernel/init/__internal.r= s 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