From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) (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 8487FBA20 for ; Wed, 29 Mar 2023 22:33:59 +0000 (UTC) Date: Wed, 29 Mar 2023 22:33:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680129237; x=1680388437; bh=YF0KQdg01n5qzHolAR22LM31sPRu0U9EL2Fgy7BvhWY=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=K9Mcc2lgyAmLApQ1FKC5qh6AYqyIwn0z69T0ipfKpv9PzSGlJ6nZCEm9X8+lCGn+5 ThpO2yyOmgEQmeLNXPSMIvWa0B7ZjDqiVcH6AK8Uvxjvo2hzHpzeL94RkO1dfQ5/oC M5teL0DzerQKN4/EXPZWJm9VyfhbrfSQjdfPBtoHWEw7Uv9+VRgxnbgMhONhPTuARe PKmjwAfroRqXsSzwqLMTr3sTESGEUwacs6jny3JlQkdgKR3IP9ifxXrRQoMj3xVpbW Tw/DVhXp51T0Kr41EfIK8addT277VjnnnH9JAFjFgfA7JAxdWD14CsB2P7edEUDRLJ vQ4MIN7biMu4w== To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl From: y86-dev@protonmail.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v3 13/13] rust: sync: add functions for initializing `UniqueArc>` Message-ID: <20230329223239.138757-14-y86-dev@protonmail.com> Feedback-ID: 40624463:user:proton Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Benno Lossin Add two functions `init_with` and `pin_init_with` to `UniqueArc>` to initialize the memory of already allocated `UniqueArc`s. This is useful when you want to allocate memory check some condition inside of a context where allocation is forbidden and then conditionally initialize an object. Signed-off-by: Benno Lossin --- rust/kernel/sync/arc.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index 4ed6329a5e5f..64b0e9d67025 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -540,6 +540,30 @@ impl UniqueArc> { inner: unsafe { Arc::from_inner(inner.cast()) }, } } + + /// Initialize `self` using the given initializer. + pub fn init_with(mut self, init: impl Init) -> core::result::= Result, E> { + // SAFETY: The supplied pointer is valid for initialization. + match unsafe { init.__init(self.as_mut_ptr()) } { + // SAFETY: Initialization completed successfully. + Ok(()) =3D> Ok(unsafe { self.assume_init() }), + Err(err) =3D> Err(err), + } + } + + /// Pin-initialize `self` using the given pin-initializer. + pub fn pin_init_with( + mut self, + init: impl PinInit, + ) -> core::result::Result>, E> { + // SAFETY: The supplied pointer is valid for initialization and we= will later pin the value + // to ensure it does not move. + match unsafe { init.__pinned_init(self.as_mut_ptr()) } { + // SAFETY: Initialization completed successfully. + Ok(()) =3D> Ok(unsafe { self.assume_init() }.into()), + Err(err) =3D> Err(err), + } + } } impl From> for Pin> { -- 2.39.2