[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <87wmd3br2d.fsf@kernel.org>
Date: Wed, 05 Mar 2025 13:27:38 +0100
From: Andreas Hindborg <a.hindborg@...nel.org>
To: "Benno Lossin" <benno.lossin@...ton.me>
Cc: "Miguel Ojeda" <ojeda@...nel.org>, "Alex Gaynor"
<alex.gaynor@...il.com>, "Boqun Feng" <boqun.feng@...il.com>, "Gary Guo"
<gary@...yguo.net>, Björn Roy Baron
<bjorn3_gh@...tonmail.com>, "Alice
Ryhl" <aliceryhl@...gle.com>, "Trevor Gross" <tmgross@...ch.edu>,
"Danilo Krummrich" <dakr@...nel.org>, <rust-for-linux@...r.kernel.org>,
<linux-kernel@...r.kernel.org>
Subject: Re: [PATCH 12/22] rust: pin-init: remove kernel-crate dependency
"Benno Lossin" <benno.lossin@...ton.me> writes:
> On Wed Mar 5, 2025 at 12:49 PM CET, Andreas Hindborg wrote:
>> "Benno Lossin" <benno.lossin@...ton.me> 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@...ton.me>
>>
>> Reviewed-by: Andreas Hindborg <a.hindborg@...nel.org>
>>
>>> ---
>>> 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
Powered by blists - more mailing lists