Re: [PATCH v4 08/15] rust: init/sync: add `InPlaceInit` trait to pin-initialize smart pointers

From: Boqun Feng
Date: Fri Mar 31 2023 - 18:43:52 EST


On Fri, Mar 31, 2023 at 09:53:25PM +0000, y86-dev@xxxxxxxxxxxxxx wrote:
[...]
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index eee7008e5e3e..24dc96603090 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -17,6 +17,7 @@
>
> use crate::{
> bindings,
> + init::{InPlaceInit, Init, PinInit},
> types::{ForeignOwnable, Opaque},
> };
> use alloc::boxed::Box;
> @@ -163,6 +164,28 @@ impl<T> Arc<T> {
> // `Arc` object.
> Ok(unsafe { Self::from_inner(Box::leak(inner).into()) })
> }
> +
> + /// Use the given initializer to in-place initialize a `T`.
> + ///
> + /// If `T: !Unpin` it will not be able to move afterwards.
> + #[inline]
> + pub fn pin_init<E>(init: impl PinInit<T, E>) -> Result<Self>

`Result` became `core::result::Result` after patch #3, so you will need
to refer to `crate::error::Result` here. Alternatively you can change
patch #3 to use the full path i.e. `core::result::Result`.

> + where
> + Error: From<E>,

In my env, looks like the compiler doesn't know which `Error` it
is after removing use of `crate::error::Result` in patch #3, you
probably need to deal with this as well.

Regards,
Boqun

> + {
> + UniqueArc::pin_init(init).map(|u| u.into())
> + }
> +
> + /// Use the given initializer to in-place initialize a `T`.
> + ///
> + /// This is equivalent to [`pin_init`], since an [`Arc`] is always pinned.
> + #[inline]
> + pub fn init<E>(init: impl Init<T, E>) -> Result<Self>
> + where
> + Error: From<E>,
> + {
> + UniqueArc::init(init).map(|u| u.into())
> + }
> }
>
> impl<T: ?Sized> Arc<T> {
> --
> 2.39.2
>
>