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 D064379C6 for ; Thu, 30 Mar 2023 15:28:24 +0000 (UTC) Date: Thu, 30 Mar 2023 15:28:17 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680190102; x=1680449302; bh=kpaOTyrKqsw1XHAwXmaqpibdDWFN/5HsmHgDh0lZUrg=; 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=odsQp3kuPYH79BQTdgIjYANBF/X5AwTkHo+Rx+bFHO+jN8jDLzyn/3n3aV3s5tX+S bUUbfGNFYEz9U7kyM7IJxHptVFFoh9xJC4OzG2tvcX1aPlBVT/Og0t42oiFq7VX4bg ZNv1QBD/PHk8n6EFgpLYq9lECnxEvb2FZUPbXGr84UXs82YAm34zWk/lDWt2B0CjX5 5bdOAOi5zwpCtvpYrEonpP296gvgJRfTzA5j6qTMwkcV8tsfo5j2vikdhtAGEuEIvb NctaMjaE+N187DL9lX+jQES9OGdzCMkWerxcpRStLoDW5fuvSgc/KxVGiz72rdeSv9 h3hthoAXLcpPA== To: Alice Ryhl From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= Subject: Re: [PATCH v3 06/13] rust: init/sync: add `InPlaceInit` trait to pin-initialize smart pointers Message-ID: <3ea5cd8d-84ca-59bd-de50-fef185233a50@protonmail.com> In-Reply-To: <1f93a045-5bd8-e07f-cf1b-7b1196c8ab54@ryhl.io> References: <20230329223239.138757-1-y86-dev@protonmail.com> <20230329223239.138757-7-y86-dev@protonmail.com> <1f93a045-5bd8-e07f-cf1b-7b1196c8ab54@ryhl.io> 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 On 30.03.23 16:37, Alice Ryhl wrote: > On 3/30/23 00:33, y86-dev@protonmail.com wrote: >> From: Benno Lossin >> >> The `InPlaceInit` trait that provides two functions, for initializing >> using `PinInit` and `Init`. It is implemented by `Arc`, >> `UniqueArc` and `Box`. >> >> Signed-off-by: Benno Lossin > > --- > > >> +/// Smart pointer that can initialize memory in-place. >> +pub trait InPlaceInit: Sized { >> + /// Use the given initializer to in-place initialize a `T`. >> + /// >> + /// If `T: !Unpin` it will not be able to move afterwards. >> + fn pin_init(init: impl PinInit) -> error::Result= > >> + where >> + Error: From; >> + >> + /// Use the given initializer to in-place initialize a `T`. >> + fn init(init: impl Init) -> error::Result >> + where >> + Error: From; >> +} > > This definition is potentially rather limiting, because it can only be > used with error types that can be converted into a `kernel::Error`. What > do you think of this alternative? > > pub trait InPlaceInit: Sized { > fn pin_init(init: impl PinInit) -> Result, E> > where > E: From; > > fn init(init: impl Init) -> Result > where > E: From; > } I initially implemented it like this, but it required almost always that `E` is specified, I will try and see if the situation is any different now, but I do not think so. In the user-space version of this API (see [1]) I have four functions, normal variants that return an `AllocError` and `try` variants that look exactly like what you suggested. In the kernel, we could make the normal variants as they are now and add the `try` variants as you described. [1]: https://docs.rs/pinned-init/0.0.5/pinned_init/trait.InPlaceInit.html -- Cheers, Benno