[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <D88BDOC74W1T.IMRXO8BE868S@proton.me>
Date: Wed, 05 Mar 2025 12:00:06 +0000
From: Benno Lossin <benno.lossin@...ton.me>
To: Andreas Hindborg <a.hindborg@...nel.org>
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
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)>`
---
Cheers,
Benno
>
>
> Best regards,
> Andreas Hindborg
>
>
> [1] https://doc.rust-lang.org/nomicon/subtyping.html#variance
Powered by blists - more mailing lists