xref: /DragonOS/kernel/crates/intertrait/macros/src/item_type.rs (revision bd70d2d1f490aabd570a5301b858bd5eb04149fa)
1 use hashbrown::HashSet;
2 use proc_macro2::TokenStream;
3 use syn::spanned::Spanned;
4 use syn::{DeriveInput, Path};
5 
6 use quote::{quote, quote_spanned};
7 
8 use crate::args::Flag;
9 use crate::gen_caster::generate_caster;
10 
process(flags: &HashSet<Flag>, paths: Vec<Path>, input: DeriveInput) -> TokenStream11 pub fn process(flags: &HashSet<Flag>, paths: Vec<Path>, input: DeriveInput) -> TokenStream {
12     let DeriveInput {
13         ref ident,
14         ref generics,
15         ..
16     } = input;
17     let generated = if generics.lt_token.is_some() {
18         quote_spanned! {
19             generics.span() => compile_error!("#[cast_to(..)] can't be used on a generic type definition");
20         }
21     } else {
22         paths
23             .into_iter()
24             .flat_map(|t| generate_caster(ident, &t, flags.contains(&Flag::Sync)))
25             .collect()
26     };
27     quote! {
28         #input
29         #generated
30     }
31 }
32