Re: [PATCH v3 6/9] rust: workqueue: add helper for defining work_struct fields

From: Andreas Hindborg (Samsung)
Date: Wed Aug 23 2023 - 05:29:13 EST



"Andreas Hindborg (Samsung)" <nmi@xxxxxxxxxxxx> writes:

> Hi Benno,
>
> Benno Lossin <benno.lossin@xxxxxxxxx> writes:
>
> ...
>
>>> +/// Links for a work item.
>>> +///
>>> +/// This struct contains a function pointer to the `run` function from the [`WorkItemPointer`]
>>> +/// trait, and defines the linked list pointers necessary to enqueue a work item in a workqueue.
>>> +///
>>> +/// Wraps the kernel's C `struct work_struct`.
>>> +///
>>> +/// This is a helper type used to associate a `work_struct` with the [`WorkItem`] that uses it.
>>> +#[repr(transparent)]
>>> +pub struct Work<T: ?Sized, const ID: u64 = 0> {
>>> + work: Opaque<bindings::work_struct>,
>>> + _inner: PhantomData<T>,
>>
>> Should this really be `PhantomData<T>`? Are you dropping `T`s in the
>> destructor of `Work<T>`? I do not think so `PhantomData<fn() -> Box<T>>`
>> should do the trick.
>>
>
> Could you elaborate what is the issue in having `PhantomData<T>`?

I played around with this and as far as I can tell, using
`PhantomData<fn() -> Box<T>>` does not disable dropck for T. Thus,
`PhantomData<T>` has the same effect as `PhantomData<fn() -> Box<T>`,
which is covariance over T and dropck:

```rust
use std::marker::PhantomData;

struct A<T> {
_marker: PhantomData<fn() -> Box<T>>,
}

//#[cfg(disable)]
impl<T> Drop for A<T> {
fn drop(&mut self) {
todo!()
}
}

struct B {}

fn main() {
let (_a, b);
b = B {};
_a = foo(&b);
}

fn foo<'a>(_b: &'a B) -> A<&'a B> {
let a: A<&'a B> = A {
_marker: PhantomData,
};
a
}
```

This is a little surprising to me since nomicon [1] explicitly marks
`PhantomData<T>` as "covariant (with drop check)" but only "covariant"
for `PhantomData<fn() -> T>`.

Not sure why you wanted the box?

Best regards,
Andreas


[1] https://doc.rust-lang.org/nomicon/phantom-data.html#table-of-phantomdata-patterns