diff options
Diffstat (limited to 'azalea-buf')
| -rw-r--r-- | azalea-buf/azalea-buf-macros/src/lib.rs | 29 | ||||
| -rw-r--r-- | azalea-buf/azalea-buf-macros/src/read.rs | 20 | ||||
| -rw-r--r-- | azalea-buf/azalea-buf-macros/src/write.rs | 22 |
3 files changed, 49 insertions, 22 deletions
diff --git a/azalea-buf/azalea-buf-macros/src/lib.rs b/azalea-buf/azalea-buf-macros/src/lib.rs index c4938f89..965e19c9 100644 --- a/azalea-buf/azalea-buf-macros/src/lib.rs +++ b/azalea-buf/azalea-buf-macros/src/lib.rs @@ -7,24 +7,39 @@ use syn::{DeriveInput, parse_macro_input}; #[proc_macro_derive(AzaleaRead, attributes(var))] pub fn derive_azalearead(input: TokenStream) -> TokenStream { - let DeriveInput { ident, data, .. } = parse_macro_input!(input); + let DeriveInput { + ident, + generics, + data, + .. + } = parse_macro_input!(input); - read::create_impl_azalearead(&ident, &data).into() + read::create_impl_azalearead(&ident, &generics, &data).into() } #[proc_macro_derive(AzaleaWrite, attributes(var))] pub fn derive_azaleawrite(input: TokenStream) -> TokenStream { - let DeriveInput { ident, data, .. } = parse_macro_input!(input); + let DeriveInput { + ident, + generics, + data, + .. + } = parse_macro_input!(input); - write::create_impl_azaleawrite(&ident, &data).into() + write::create_impl_azaleawrite(&ident, &generics, &data).into() } #[proc_macro_derive(AzBuf, attributes(var, limit))] pub fn derive_azbuf(input: TokenStream) -> TokenStream { - let DeriveInput { ident, data, .. } = parse_macro_input!(input); + let DeriveInput { + ident, + generics, + data, + .. + } = parse_macro_input!(input); - let writable = write::create_impl_azaleawrite(&ident, &data); - let readable = read::create_impl_azalearead(&ident, &data); + let writable = write::create_impl_azaleawrite(&ident, &generics, &data); + let readable = read::create_impl_azalearead(&ident, &generics, &data); quote! { #writable #readable diff --git a/azalea-buf/azalea-buf-macros/src/read.rs b/azalea-buf/azalea-buf-macros/src/read.rs index 3ec6133e..9f4fa78a 100644 --- a/azalea-buf/azalea-buf-macros/src/read.rs +++ b/azalea-buf/azalea-buf-macros/src/read.rs @@ -1,14 +1,20 @@ use quote::{ToTokens, quote}; -use syn::{Data, Field, FieldsNamed, Ident, punctuated::Punctuated, token::Comma}; +use syn::{Data, Field, FieldsNamed, Generics, Ident, punctuated::Punctuated, token::Comma}; + +pub fn create_impl_azalearead( + ident: &Ident, + generics: &Generics, + data: &Data, +) -> proc_macro2::TokenStream { + let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); -pub fn create_impl_azalearead(ident: &Ident, data: &Data) -> proc_macro2::TokenStream { match data { syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields { syn::Fields::Named(FieldsNamed { named, .. }) => { let (read_fields, read_field_names) = read_named_fields(named); quote! { - impl azalea_buf::AzaleaRead for #ident { + impl #impl_generics azalea_buf::AzaleaRead for #ident #ty_generics #where_clause { fn azalea_read(buf: &mut std::io::Cursor<&[u8]>) -> std::result::Result<Self, azalea_buf::BufReadError> { #(#read_fields)* Ok(Self { @@ -20,7 +26,7 @@ pub fn create_impl_azalearead(ident: &Ident, data: &Data) -> proc_macro2::TokenS } syn::Fields::Unit => { quote! { - impl azalea_buf::AzaleaRead for #ident { + impl #impl_generics azalea_buf::AzaleaRead for #ident #ty_generics #where_clause { fn azalea_read(buf: &mut std::io::Cursor<&[u8]>) -> std::result::Result<Self, azalea_buf::BufReadError> { Ok(Self) } @@ -31,7 +37,7 @@ pub fn create_impl_azalearead(ident: &Ident, data: &Data) -> proc_macro2::TokenS let read_fields = read_unnamed_fields(&fields.unnamed); quote! { - impl azalea_buf::AzaleaRead for #ident { + impl #impl_generics azalea_buf::AzaleaRead for #ident #ty_generics #where_clause { fn azalea_read(buf: &mut std::io::Cursor<&[u8]>) -> std::result::Result<Self, azalea_buf::BufReadError> { Ok(Self( #(#read_fields),* @@ -135,14 +141,14 @@ pub fn create_impl_azalearead(ident: &Ident, data: &Data) -> proc_macro2::TokenS let first_reader = first_reader.expect("There should be at least one variant"); quote! { - impl azalea_buf::AzaleaRead for #ident { + impl #impl_generics azalea_buf::AzaleaRead for #ident #ty_generics #where_clause { fn azalea_read(buf: &mut std::io::Cursor<&[u8]>) -> std::result::Result<Self, azalea_buf::BufReadError> { let id = azalea_buf::AzaleaReadVar::azalea_read_var(buf)?; Self::azalea_read_id(buf, id) } } - impl #ident { + impl #impl_generics #ident #ty_generics #where_clause { pub fn azalea_read_id(buf: &mut std::io::Cursor<&[u8]>, id: u32) -> std::result::Result<Self, azalea_buf::BufReadError> { match id { #match_contents diff --git a/azalea-buf/azalea-buf-macros/src/write.rs b/azalea-buf/azalea-buf-macros/src/write.rs index 12739eb5..7bd58d29 100644 --- a/azalea-buf/azalea-buf-macros/src/write.rs +++ b/azalea-buf/azalea-buf-macros/src/write.rs @@ -1,8 +1,14 @@ use proc_macro2::Span; use quote::{ToTokens, quote}; -use syn::{Data, Field, FieldsNamed, Ident, punctuated::Punctuated, token::Comma}; +use syn::{Data, Field, FieldsNamed, Generics, Ident, punctuated::Punctuated, token::Comma}; + +pub fn create_impl_azaleawrite( + ident: &Ident, + generics: &Generics, + data: &Data, +) -> proc_macro2::TokenStream { + let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); -pub fn create_impl_azaleawrite(ident: &Ident, data: &Data) -> proc_macro2::TokenStream { match data { syn::Data::Struct(syn::DataStruct { fields, .. }) => match fields { syn::Fields::Named(FieldsNamed { named, .. }) => { @@ -10,7 +16,7 @@ pub fn create_impl_azaleawrite(ident: &Ident, data: &Data) -> proc_macro2::Token write_named_fields(named, Some(&Ident::new("self", Span::call_site()))); quote! { - impl azalea_buf::AzaleaWrite for #ident { + impl #impl_generics azalea_buf::AzaleaWrite for #ident #ty_generics #where_clause { fn azalea_write(&self, buf: &mut impl std::io::Write) -> std::result::Result<(), std::io::Error> { #write_fields Ok(()) @@ -20,7 +26,7 @@ pub fn create_impl_azaleawrite(ident: &Ident, data: &Data) -> proc_macro2::Token } syn::Fields::Unit => { quote! { - impl azalea_buf::AzaleaWrite for #ident { + impl #impl_generics azalea_buf::AzaleaWrite for #ident #ty_generics #where_clause { fn azalea_write(&self, buf: &mut impl std::io::Write) -> std::result::Result<(), std::io::Error> { Ok(()) } @@ -31,7 +37,7 @@ pub fn create_impl_azaleawrite(ident: &Ident, data: &Data) -> proc_macro2::Token let write_fields = write_unnamed_fields(&fields.unnamed); quote! { - impl azalea_buf::AzaleaWrite for #ident { + impl #impl_generics azalea_buf::AzaleaWrite for #ident #ty_generics #where_clause { fn azalea_write(&self, buf: &mut impl std::io::Write) -> std::result::Result<(), std::io::Error> { #write_fields Ok(()) @@ -143,7 +149,7 @@ pub fn create_impl_azaleawrite(ident: &Ident, data: &Data) -> proc_macro2::Token } if is_data_enum { quote! { - impl azalea_buf::AzaleaWrite for #ident { + impl #impl_generics azalea_buf::AzaleaWrite for #ident #ty_generics #where_clause { fn azalea_write(&self, buf: &mut impl std::io::Write) -> std::result::Result<(), std::io::Error> { match self { #match_arms @@ -151,7 +157,7 @@ pub fn create_impl_azaleawrite(ident: &Ident, data: &Data) -> proc_macro2::Token Ok(()) } } - impl #ident { + impl #impl_generics #ident #ty_generics #where_clause { pub fn write_without_id(&self, buf: &mut impl std::io::Write) -> std::result::Result<(), std::io::Error> { match self { #match_arms_without_id @@ -163,7 +169,7 @@ pub fn create_impl_azaleawrite(ident: &Ident, data: &Data) -> proc_macro2::Token } else { // optimization: if it doesn't have data we can just do `as u32` quote! { - impl azalea_buf::AzaleaWrite for #ident { + impl #impl_generics azalea_buf::AzaleaWrite for #ident #ty_generics #where_clause { fn azalea_write(&self, buf: &mut impl std::io::Write) -> std::result::Result<(), std::io::Error> { azalea_buf::AzaleaWriteVar::azalea_write_var(&(*self as u32), buf) } |
