Re: [PATCH 12/22] rust: pin-init: remove kernel-crate dependency
From: Andreas Hindborg
Date: Wed Mar 05 2025 - 07:34:54 EST
"Benno Lossin" <benno.lossin@xxxxxxxxx> writes:
> On Wed Mar 5, 2025 at 12:49 PM CET, Andreas Hindborg wrote:
>> "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`?
>
> Yes! There is another reason: `Send` and `Sync`, my `Invariant` type
> will always be `Send` and `Sync`, but `PhantomData<*mut T>` is always
> `!Send` and `!Sync`.
> One could argue that an `Invariant<T>` type should impl `Send`/`Sync`
> if and only if `T` does, but for my usage it doesn't matter. If you do
> need to use that, you could use `PhantomData<(fn(*mut T) -> *mut T, T)>`
Awesome, thanks for explaining.
Could you add this info to the docstring for `Invariant<T>`?
Best regards,
Andreas Hindborg