Re: [PATCH 12/22] rust: pin-init: remove kernel-crate dependency

From: Andreas Hindborg
Date: Wed Mar 05 2025 - 06:49:54 EST


"Benno Lossin" <benno.lossin@xxxxxxxxx> writes:

> In order to make pin-init a standalone crate, remove dependencies on
> kernel-specific code such as `ScopeGuard` and `KBox`.
>
> `ScopeGuard` is only used in the `[pin_]init_array_from_fn` functions
> and can easily be replaced by a primitive construct.
>
> `KBox` is only used for type variance of unsized types and can also
> easily be replaced.
>
> Signed-off-by: Benno Lossin <benno.lossin@xxxxxxxxx>

Reviewed-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>

> ---
> rust/pin-init/src/__internal.rs | 2 +-
> rust/pin-init/src/lib.rs | 41 +++++++++++----------------------
> 2 files changed, 15 insertions(+), 28 deletions(-)
>
> diff --git a/rust/pin-init/src/__internal.rs b/rust/pin-init/src/__internal.rs
> index 0db800819681..74086365a18a 100644
> --- a/rust/pin-init/src/__internal.rs
> +++ b/rust/pin-init/src/__internal.rs
> @@ -105,7 +105,7 @@ fn make_closure<F, O, E>(self, f: F) -> F
> }
> }
>
> -pub struct AllData<T: ?Sized>(PhantomData<fn(KBox<T>) -> KBox<T>>);
> +pub struct AllData<T: ?Sized>(Invariant<T>);

Off topic, trying to learn something: You define `Invariant<T>` like so:

pub(super) type Invariant<T> = PhantomData<fn(*mut T) -> *mut T>;

Consulting the variance table at [1], could you define it as

pub(super) type Invariant<T> = PhantomData<*mut T>;

or is there another reason for using `fn`?


Best regards,
Andreas Hindborg


[1] https://doc.rust-lang.org/nomicon/subtyping.html#variance