Re: [PATCH v2 2/3] rust: macros: allow generic parameter default values in `#[pin_data]`

From: Gary Guo
Date: Sat Dec 23 2023 - 09:42:39 EST


On Wed, 13 Dec 2023 22:08:53 +0000
Benno Lossin <benno.lossin@xxxxxxxxx> wrote:

> Add support for generic parameters defaults in `#[pin_data]` by using
> the newly introduced `decl_generics` instead of the `impl_generics`.
>
> Before this would not compile:
>
> #[pin_data]
> struct Foo<const N: usize = 0> {
> // ...
> }
>
> because it would be expanded to this:
>
> struct Foo<const N: usize = 0> {
> // ...
> }
>
> const _: () = {
> struct __ThePinData<const N: usize = 0> {
> __phantom: ::core::marker::PhantomData<fn(Foo<N>) -> Foo<N>>,
> }
> impl<const N: usize = 0> ::core::clone::Clone for __ThePinData<N> {
> fn clone(&self) -> Self {
> *self
> }
> }
>
> // [...] rest of expansion omitted
> };
>
> The problem is with the `impl<const N: usize = 0>`, since that is
> invalid Rust syntax. It should not mention the default value at all,
> since default values only make sense on type definitions.
>
> The new `impl_generics` do not contain the default values, thus
> generating correct Rust code.
>
> This is used by the next commit that puts `#[pin_data]` on
> `kernel::workqueue::Work`.
>
> Signed-off-by: Benno Lossin <benno.lossin@xxxxxxxxx>

Reviewed-by: Gary Guo <gary@xxxxxxxxxxx>

> ---
> v1 -> v2:
> - clarify the change in the commit message
> - add motivation to the commit message
>
> rust/kernel/init/macros.rs | 19 ++++++++++++++++++-
> rust/macros/pin_data.rs | 3 ++-
> 2 files changed, 20 insertions(+), 2 deletions(-)