1 // SPDX-License-Identifier: GPL-2.0
3 use crate::helpers::{parse_generics, Generics};
4 use proc_macro::{TokenStream, TokenTree};
6 pub(crate) fn derive(input: TokenStream) -> TokenStream {
13 ) = parse_generics(input);
14 // This should be the body of the struct `{...}`.
15 let last = rest.pop();
16 // Now we insert `Zeroable` as a bound for every generic parameter in `impl_generics`.
17 let mut new_impl_generics = Vec::with_capacity(impl_generics.len());
18 // Are we inside of a generic where we want to add `Zeroable`?
19 let mut in_generic = !impl_generics.is_empty();
20 // Have we already inserted `Zeroable`?
21 let mut inserted = false;
22 // Level of `<>` nestings.
24 for tt in impl_generics {
26 // If we find a `,`, then we have finished a generic/constant/lifetime parameter.
27 TokenTree::Punct(p) if nested == 0 && p.as_char() == ',' => {
28 if in_generic && !inserted {
29 new_impl_generics.extend(quote! { : ::kernel::init::Zeroable });
33 new_impl_generics.push(tt);
35 // If we find `'`, then we are entering a lifetime.
36 TokenTree::Punct(p) if nested == 0 && p.as_char() == '\'' => {
38 new_impl_generics.push(tt);
40 TokenTree::Punct(p) if nested == 0 && p.as_char() == ':' => {
41 new_impl_generics.push(tt);
43 new_impl_generics.extend(quote! { ::kernel::init::Zeroable + });
47 TokenTree::Punct(p) if p.as_char() == '<' => {
49 new_impl_generics.push(tt);
51 TokenTree::Punct(p) if p.as_char() == '>' => {
54 new_impl_generics.push(tt);
56 _ => new_impl_generics.push(tt),
59 assert_eq!(nested, 0);
60 if in_generic && !inserted {
61 new_impl_generics.extend(quote! { : ::kernel::init::Zeroable });
64 ::kernel::__derive_zeroable!(
67 @impl_generics(#(#new_impl_generics)*),
68 @ty_generics(#(#ty_generics)*),