From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) (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 DBCFF28DD1 for ; Wed, 17 Jul 2024 19:54:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721246066; cv=none; b=Bjax52BoldK+Gdw8b+RkDGCdO7oB5jlRvBNDC+6tYA33NkQfxSxIcIEsCvBzy5crFkKJgzO/ZLlrQ/6aWNCwAcTO1PTCwC54Rdxm2hpqtcRKiRIJSNwm66nbci8D2nu1YQN+yKg9rTPtBEOkf/Q4otK2B+Kwl5wIEhSGne4fY4g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721246066; c=relaxed/simple; bh=0s0SddSeTuGrJ1laxk/JMS46syAuIUDRNFG+AKO22BA=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=onhB+AMEnZCLfT4iJAsq17XHC3XTwSrQk0Ju5q4qOExbiJiuGEyn0RXyFN2s3rBaz7m74o7hemuo9vAdkQ0dgAMkF9D28jJQkCiPUcZxa3qSo2WmxFjdWzlWFE/9lBBZa7j6umCvRHLseEZcGopnCgNfDuggKVYTnl0WHoViNOA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=h8bCcZk1; arc=none smtp.client-ip=185.70.40.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="h8bCcZk1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1721246062; x=1721505262; bh=e5x3kmpFuQF+/sLbsGqQ7KQCx1e2yyOtsQpsFj0qtZY=; 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=h8bCcZk1DS1/EHyD7JH5n8Av/DHN6OyBiRcuBjmlaL7EiXmyIIKBgYTxXwhjhVbVC 1UUYqHu3gsP9UPu2Zm//HJJgmIGAsU3RmK75+5fgvbiTqMQVDLoFXiGa2+zHjA4P2d FF47mxYYOWgZ14imiTqawgxDDL60RxQMTsOjqtkPCnvYopJJWrm7fAuvgC+eBPgZr5 vBSoZekUmtSKNKRDv9SQWp88YzJ5gxPGZ1KWfZ0v29pRhMJl2t1Ng5cA7ZxiTreuWC X0a1Jzcj2emuTSM68NWkK2ts3BBojST5C+3V0stYlqKNb3/pvgdVTG2xPDYBQv8Zv8 D8k9SZYJf6bgA== Date: Wed, 17 Jul 2024 19:54:14 +0000 To: Boqun Feng From: Benno Lossin Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 1/2] rust: kernel: add `drop_contents` to `BoxExt` Message-ID: <323ad3d0-e478-4ed9-875c-335ced829afd@proton.me> In-Reply-To: References: <20240708205325.1275473-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton X-Pm-Message-ID: 5a16a5dd66736c6e7adbc740079b5efdfd16af58 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 On 08.07.24 23:52, Boqun Feng wrote: > On Mon, Jul 08, 2024 at 02:14:50PM -0700, Boqun Feng wrote: >> On Mon, Jul 08, 2024 at 08:53:38PM +0000, Benno Lossin wrote: >>> Sometimes (see [1]) it is necessary to drop the value inside of a >>> `Box`, but retain the allocation. For example to reuse the allocatio= n >>> in the future. >>> Introduce a new function `drop_contents` that turns a `Box` into >>> `Box>` by dropping the value. >>> >>> Signed-off-by: Benno Lossin >>> Link: https://lore.kernel.org/rust-for-linux/20240418-b4-rbtree-v3-5-32= 3e134390ce@google.com/ [1] >>> --- >>> rust/kernel/alloc/box_ext.rs | 21 +++++++++++++++++++++ >>> 1 file changed, 21 insertions(+) >>> >>> diff --git a/rust/kernel/alloc/box_ext.rs b/rust/kernel/alloc/box_ext.r= s >>> index cdbb5ad166d9..6cf79f96d6c7 100644 >>> --- a/rust/kernel/alloc/box_ext.rs >>> +++ b/rust/kernel/alloc/box_ext.rs >>> @@ -5,6 +5,7 @@ >>> use super::{AllocError, Flags}; >>> use alloc::boxed::Box; >>> use core::mem::MaybeUninit; >>> +use core::ptr; >>> use core::result::Result; >>> >>> /// Extensions to [`Box`]. >>> @@ -18,6 +19,18 @@ pub trait BoxExt: Sized { >>> /// >>> /// The allocation may fail, in which case an error is returned. >>> fn new_uninit(flags: Flags) -> Result>, AllocEr= ror>; >>> + >>> + /// Drops the contents, but keeps the allocation. >>> + /// >=20 > (I spoke too soon ;-)) >=20 >>> + /// # Examples >>> + /// >>> + /// ``` >=20 > need a `use` here: >=20 > =09use kernel::alloc::{flags, box_ext::BoxExt}; Oops, should have checked that. >>> + /// let value =3D Box::new([0; 32], flags::GFP_KERNEL) >=20 > missing a '?' and a ';' at the end of this line. >=20 >>> + /// let value =3D value.drop_contents(); >>> + /// // Now we can re-use `value`: >>> + /// Box::write(value, [1; 32]); >=20 > Need a line: >=20 > =09# Ok::<(), Error>(()) >=20 > here. >>> + /// ``` >>> + fn drop_contents(self) -> Box>; >>> } >>> > [...] >=20 > I queued this patch in rust-dev with these fixes applied, FYI. Thanks! @Miguel, when you take this, then apply the fixes that Boqun suggested. --- Cheers, Benno