<feed xmlns='http://www.w3.org/2005/Atom'>
<title>kernel/linux.git/rust/macros/zeroable.rs, branch v6.12.80</title>
<subtitle>Linux kernel stable tree (mirror)</subtitle>
<id>https://git.radix-linux.su/kernel/linux.git/atom?h=v6.12.80</id>
<link rel='self' href='https://git.radix-linux.su/kernel/linux.git/atom?h=v6.12.80'/>
<link rel='alternate' type='text/html' href='https://git.radix-linux.su/kernel/linux.git/'/>
<updated>2024-04-07T20:03:42+00:00</updated>
<entry>
<title>rust: macros: add `decl_generics` to `parse_generics()`</title>
<updated>2024-04-07T20:03:42+00:00</updated>
<author>
<name>Benno Lossin</name>
<email>benno.lossin@proton.me</email>
</author>
<published>2024-03-09T15:53:25+00:00</published>
<link rel='alternate' type='text/html' href='https://git.radix-linux.su/kernel/linux.git/commit/?id=9762dca54a4fec433b50eb83fdd8ff0a876cccf2'/>
<id>urn:sha1:9762dca54a4fec433b50eb83fdd8ff0a876cccf2</id>
<content type='text'>
The generic parameters on a type definition can specify default values.
Currently `parse_generics()` cannot handle this though. For example when
parsing the following generics:

    &lt;T: Clone, const N: usize = 0&gt;

The `impl_generics` will be set to `T: Clone, const N: usize = 0` and
`ty_generics` will be set to `T, N`. Now using the `impl_generics` on an
impl block:

    impl&lt;$($impl_generics)*&gt; Foo {}

will result in invalid Rust code, because default values are only
available on type definitions.

Therefore add parsing support for generic parameter default values using
a new kind of generics called `decl_generics` and change the old
behavior of `impl_generics` to not contain the generic parameter default
values.

Now `Generics` has three fields:
- `impl_generics`: the generics with bounds
  (e.g. `T: Clone, const N: usize`)
- `decl_generics`: the generics with bounds and default values
  (e.g. `T: Clone, const N: usize = 0`)
- `ty_generics`:  contains the generics without bounds and without
  default values (e.g. `T, N`)

`impl_generics` is designed to be used on `impl&lt;$impl_generics&gt;`,
`decl_generics` for the type definition, so `struct Foo&lt;$decl_generics&gt;`
and `ty_generics` whenever you use the type, so `Foo&lt;$ty_generics&gt;`.

Here is an example that uses all three different types of generics:

    let (Generics { decl_generics, impl_generics, ty_generics }, rest) = parse_generics(input);
    quote! {
        struct Foo&lt;$($decl_generics)*&gt; {
            // ...
        }

        impl&lt;$impl_generics&gt; Foo&lt;$ty_generics&gt; {
            fn foo() {
                // ...
            }
        }
    }

The next commit contains a fix to the `#[pin_data]` macro making it
compatible with generic parameter default values by relying on this new
behavior.

Signed-off-by: Benno Lossin &lt;benno.lossin@proton.me&gt;
Reviewed-by: Alice Ryhl &lt;aliceryhl@google.com&gt;
Link: https://lore.kernel.org/r/20240309155243.482334-1-benno.lossin@proton.me
Signed-off-by: Miguel Ojeda &lt;ojeda@kernel.org&gt;
</content>
</entry>
<entry>
<title>rust: add derive macro for `Zeroable`</title>
<updated>2023-08-21T12:31:48+00:00</updated>
<author>
<name>Benno Lossin</name>
<email>benno.lossin@proton.me</email>
</author>
<published>2023-08-14T08:46:41+00:00</published>
<link rel='alternate' type='text/html' href='https://git.radix-linux.su/kernel/linux.git/commit/?id=071cedc84e907f6984b3de3285ec2b077d3c3cdb'/>
<id>urn:sha1:071cedc84e907f6984b3de3285ec2b077d3c3cdb</id>
<content type='text'>
Add a derive proc-macro for the `Zeroable` trait. The macro supports
structs where every field implements the `Zeroable` trait. This way
`unsafe` implementations can be avoided.

The macro is split into two parts:
- a proc-macro to parse generics into impl and ty generics,
- a declarative macro that expands to the impl block.

Suggested-by: Asahi Lina &lt;lina@asahilina.net&gt;
Signed-off-by: Benno Lossin &lt;benno.lossin@proton.me&gt;
Reviewed-by: Gary Guo &lt;gary@garyguo.net&gt;
Reviewed-by: Martin Rodriguez Reboredo &lt;yakoyoku@gmail.com&gt;
Link: https://lore.kernel.org/r/20230814084602.25699-4-benno.lossin@proton.me
[ Added `ignore` to the `lib.rs` example and cleaned trivial nit. ]
Signed-off-by: Miguel Ojeda &lt;ojeda@kernel.org&gt;
</content>
</entry>
</feed>
