This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Derivable Encode & Decode #509
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a44fd32
Derive macro for simple structs.
tomusdrw 66f6424
Derive Encode/Decode wherever we can.
tomusdrw 0aecc83
Derive for enums.
tomusdrw d21d09d
Support discriminant.
tomusdrw ac85a7f
Get rid of some repetition.
tomusdrw 0729be8
Support custom indices.
tomusdrw 252302c
Derive codec for enums wherever possible.
tomusdrw a558a62
Merge branch 'master' into td-codecderive
tomusdrw 7f0c8e9
Fix no_std
tomusdrw 68c93b3
WASM rebuilt
tomusdrw d3a5ccd
Avoid excessive import.
tomusdrw 1588c86
Fix compilation.
tomusdrw cf6b46c
Address review grumbles.
tomusdrw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Derive for enums.
- Loading branch information
commit 0aecc833c607168a0cd01281863410666fa9eafc
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -16,11 +16,11 @@ | |
|
|
||
| use proc_macro2::{Span, TokenStream}; | ||
| use syn::{ | ||
| Data, Fields, Index, | ||
| Data, Fields, Ident, Index, | ||
| spanned::Spanned, | ||
| }; | ||
|
|
||
| pub fn quote(data: &Data, self_: &TokenStream, dest_: &TokenStream) -> TokenStream { | ||
| pub fn quote(data: &Data, type_name: &Ident, self_: &TokenStream, dest_: &TokenStream) -> TokenStream { | ||
| let call_site = Span::call_site(); | ||
| match *data { | ||
| Data::Struct(ref data) => match data.fields { | ||
|
|
@@ -34,7 +34,7 @@ pub fn quote(data: &Data, self_: &TokenStream, dest_: &TokenStream) -> TokenStre | |
| } | ||
| }); | ||
|
|
||
| quote! { | ||
| quote_spanned! { call_site => | ||
| #( #recurse )* | ||
| } | ||
| }, | ||
|
|
@@ -51,13 +51,73 @@ pub fn quote(data: &Data, self_: &TokenStream, dest_: &TokenStream) -> TokenStre | |
| #( #recurse )* | ||
| } | ||
| }, | ||
| // Do nothing, we don't encode unit types? | ||
| // Do nothing, we don't encode unit type and assume it's always decodable. | ||
| Fields::Unit => quote! { | ||
| drop(#dest_); | ||
| }, | ||
| }, | ||
| // TODO [ToDr] Encode enums and unions | ||
| Data::Enum(_) | Data::Union(_) => unimplemented!(), | ||
| Data::Enum(ref data) => { | ||
| assert!(data.variants.len() < 256, "Currently only enums with less than 255 variants are encodable."); | ||
|
|
||
| let recurse = data.variants.iter().enumerate().map(|(i, f)| { | ||
| let name = &f.ident; | ||
|
|
||
| match f.fields { | ||
| Fields::Named(ref fields) => { | ||
| let names = fields.named.iter().map(|f| { | ||
| let field_name = &f.ident; | ||
| quote_spanned!(call_site => #field_name) | ||
| }).collect::<Vec<_>>(); | ||
|
|
||
| let fields = names.iter(); | ||
| let encode_fields = names.iter().map(|f| { | ||
| quote_spanned! { call_site => #dest_.push(#f); } | ||
| }); | ||
|
|
||
| quote_spanned! { f.span() => | ||
| #type_name :: #name { #( ref #fields, )* } => { | ||
| #dest_.push_byte(#i as u8); | ||
| #( #encode_fields )* | ||
| } | ||
| } | ||
| }, | ||
| Fields::Unnamed(ref fields) => { | ||
| let names = fields.unnamed.iter().enumerate().map(|(i, _f)| { | ||
| let ident = Ident::new(&format!("f_{}", i), call_site); | ||
| quote_spanned!(call_site => #ident) | ||
| }).collect::<Vec<_>>(); | ||
|
|
||
| let fields = names.iter(); | ||
| let encode_fields = names.iter().map(|f| { | ||
| quote_spanned! { call_site => #dest_.push(#f); } | ||
| }); | ||
|
|
||
| quote_spanned! { f.span() => | ||
| #type_name :: #name ( #( ref #fields, )* ) => { | ||
| #dest_.push_byte(#i as u8); | ||
| #( #encode_fields )* | ||
| } | ||
| } | ||
| }, | ||
| Fields::Unit => { | ||
| quote_spanned! { f.span() => | ||
| #type_name :: #name => { | ||
| #dest_.push_byte(#i as u8); | ||
| } | ||
| } | ||
| }, | ||
| } | ||
| }); | ||
|
|
||
| quote! { | ||
| match *#self_ { | ||
| #( #recurse )*, | ||
| _ => unreachable!(), | ||
| } | ||
| } | ||
| }, | ||
| // NOTE [ToDr] we currently don't use unions at all. | ||
| Data::Union(_) => unimplemented!(), | ||
|
||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
less than 256 / less than or equal 255?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to
at most 256:)