@@ -10,7 +10,8 @@ use proc_macro2::{Span, TokenStream};
1010use quote:: { quote, ToTokens } ;
1111use syn:: {
1212 parse_quote, Data , DataEnum , DataStruct , DataUnion , DeriveInput , Error , Expr , ExprLit , Field ,
13- Ident , Index , Lit , Meta , Path , Type , Variant , Visibility ,
13+ Ident , ImplGenerics , Index , Lit , Meta , Path , Type , TypeGenerics , Variant , Visibility ,
14+ WhereClause ,
1415} ;
1516
1617pub ( crate ) struct Ctx {
@@ -68,6 +69,60 @@ impl Ctx {
6869 }
6970}
7071
72+ pub ( crate ) struct ImplBlockBuilder < ' a > {
73+ impl_generics : ImplGenerics < ' a > ,
74+
75+ trait_name : Path ,
76+ trait_generics : Option < TypeGenerics < ' a > > ,
77+
78+ self_name : Ident ,
79+ self_generics : TypeGenerics < ' a > ,
80+
81+ where_clause : Option < & ' a WhereClause > ,
82+
83+ ctx : & ' a Ctx ,
84+ }
85+
86+ impl < ' a > ImplBlockBuilder < ' a > {
87+ pub ( crate ) fn from_derive_input (
88+ ctx : & ' a Ctx ,
89+ input : & ' a DeriveInput ,
90+ trait_name : Path ,
91+ ) -> ImplBlockBuilder < ' a > {
92+ let ( impl_generics, self_generics, where_clause) = input. generics . split_for_impl ( ) ;
93+
94+ Self {
95+ impl_generics,
96+ trait_name,
97+ trait_generics : None ,
98+ self_name : input. ident . clone ( ) ,
99+ self_generics,
100+ where_clause,
101+ ctx,
102+ }
103+ }
104+
105+ pub ( crate ) fn build ( self ) -> TokenStream {
106+ let Self {
107+ impl_generics,
108+ trait_name,
109+ trait_generics,
110+ self_name,
111+ self_generics,
112+ where_clause,
113+ ctx,
114+ } = self ;
115+
116+ let zerocopy_crate = & ctx. zerocopy_crate ;
117+
118+ quote ! {
119+ unsafe impl #impl_generics #trait_name #trait_generics for #self_name #self_generics #where_clause {
120+ fn only_derive_is_allowed_to_implement_this_trait( ) where Self : #zerocopy_crate:: util:: macro_util:: core_reexport:: marker:: Sized { }
121+ }
122+ }
123+ }
124+ }
125+
71126pub ( crate ) trait DataExt {
72127 /// Extracts the names and types of all fields. For enums, extracts the
73128 /// names and types of fields from each variant. For tuple structs, the
0 commit comments