-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
ToStatic
custom derive attribute
- Loading branch information
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use proc_macro2::Span; | ||
use quote::quote; | ||
use syn::{Data, DeriveInput, FieldsNamed, FieldsUnnamed, Ident, LitInt}; | ||
|
||
pub fn derive_tostatic(s: synstructure::Structure) -> proc_macro2::TokenStream { | ||
let ast = s.ast(); | ||
|
||
let debug_derive = ast.attrs.iter().any(|attr| { | ||
attr.meta | ||
.path() | ||
.is_ident(&Ident::new("debug_derive", Span::call_site())) | ||
}); | ||
|
||
let ds = match &ast.data { | ||
Data::Struct(ds) => ds, | ||
_ => panic!("Unsupported type, cannot derive"), | ||
}; | ||
|
||
let ts = match &ds.fields { | ||
syn::Fields::Unit => derive_unit_struct(ast), | ||
syn::Fields::Named(fields) => derive_named_struct(fields, ast), | ||
syn::Fields::Unnamed(fields) => derive_unnamed_struct(fields, ast), | ||
}; | ||
|
||
let ts = s.gen_impl(ts); | ||
|
||
if debug_derive { | ||
eprintln!("{ts}"); | ||
} | ||
ts | ||
} | ||
|
||
fn derive_unit_struct(ast: &DeriveInput) -> proc_macro2::TokenStream { | ||
let struct_ident = &ast.ident; | ||
|
||
quote! { | ||
gen impl asn1_rs::ToStatic for @Self { | ||
type Owned = #struct_ident; | ||
|
||
fn to_static(&self) -> Self::Owned { | ||
#struct_ident | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn derive_named_struct(fields: &FieldsNamed, ast: &DeriveInput) -> proc_macro2::TokenStream { | ||
let fields: Vec<_> = fields.named.iter().collect(); | ||
|
||
let field_idents: Vec<_> = fields.iter().map(|f| f.ident.clone()).collect(); | ||
|
||
let field_instrs = field_idents.iter().map(|ident| { | ||
quote! { let #ident = self.#ident.to_static(); } | ||
}); | ||
|
||
let struct_ident = &ast.ident; | ||
|
||
quote! { | ||
gen impl asn1_rs::ToStatic for @Self { | ||
type Owned = #struct_ident<'static>; | ||
|
||
fn to_static(&self) -> Self::Owned { | ||
#(#field_instrs)* | ||
#struct_ident{ | ||
#(#field_idents,)* | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn derive_unnamed_struct(fields: &FieldsUnnamed, ast: &DeriveInput) -> proc_macro2::TokenStream { | ||
// TODO: if unnamed, check that there is only one lifetime in ast.generics | ||
let fields: Vec<_> = fields.unnamed.iter().collect(); | ||
|
||
let field_idents: Vec<_> = (0..fields.len()) | ||
.map(|idx| Ident::new(&format!("_{idx}"), Span::call_site())) | ||
.collect(); | ||
|
||
let field_instrs = field_idents.iter().enumerate().map(|(idx, ident)| { | ||
let idx = LitInt::new(&format!("{idx}"), Span::call_site()); | ||
quote! { let #ident = self.#idx.to_static(); } | ||
}); | ||
|
||
let struct_ident = &ast.ident; | ||
|
||
quote! { | ||
gen impl asn1_rs::ToStatic for @Self { | ||
type Owned = #struct_ident<'static>; | ||
|
||
fn to_static(&self) -> Self::Owned { | ||
#(#field_instrs)* | ||
#struct_ident( | ||
#(#field_idents,)* | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters