Re: [PATCH v4 4/7] rust: workqueue: add helper for defining work_struct fields

From: Alice Ryhl
Date: Tue Sep 26 2023 - 06:01:51 EST


>> +impl<T: ?Sized, const ID: u64> Work<T, ID> {
>> + /// Creates a new instance of [`Work`].
>> + #[inline]
>> + #[allow(clippy::new_ret_no_self)]
>> + pub fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self>
>> + where
>> + T: WorkItem<ID>,
>> + {
>> + // SAFETY: The `WorkItemPointer` implementation promises that `run` can be used as the work
>> + // item function.
>> + unsafe {
>> + kernel::init::pin_init_from_closure(move |slot| {
>> + let slot = Self::raw_get(slot);
>> + bindings::init_work_with_key(
>> + slot,
>> + Some(T::Pointer::run),
>> + false,
>> + name.as_char_ptr(),
>> + key.as_ptr(),
>> + );
>> + Ok(())
>> + })
>> + }
>
> I would suggest this instead:
> ```
> pin_init!(Self {
> // SAFETY: The `WorkItemPointer` implementation promises that `run` can be used as the
> // work item function.
> work <- Opaque::ffi_init(|slot| unsafe {
> bindings::init_work_with_key(
> slot,
> Some(T::Pointer::run),
> false,
> name.as_char_ptr(),
> key.as_ptr(),
> )
> }),
> _inner: PhantomData,
> })
> ```

I tried setting up a patch with a few nits fixed, including this one.
However, I ran into an error when adding #[pin_data] to the Work struct:

error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
--> rust/kernel/workqueue.rs:192:28
|
192 | pub struct Work<T: ?Sized, const ID: u64 = 0> {
| ^^^^^^^^^^^^^^^^^

This nit can be fixed when #[pin_data] is updated to support default
values in struct declarations.

Alice