1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::DeriveInput;
#[proc_macro_derive(Bytes)]
pub fn derive_bytes(input: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(input as DeriveInput);
let ident = input.ident;
match input.data {
syn::Data::Struct(s) => {
let mut names = Vec::with_capacity(s.fields.len());
let mut types = Vec::with_capacity(s.fields.len());
for field in s.fields {
names.push(field.ident);
types.push(field.ty);
}
quote! {
impl TBytes for #ident{
fn size(&self) -> usize{
#(self.#names.size())+*
}
fn to_bytes(&self) -> Vec<u8>{
let mut buffer = Vec::with_capacity(self.size());
#(buffer.append(&mut self.#names.to_bytes());)*
buffer
}
fn from_bytes(bytes: &mut Vec<u8>) -> Option<Self>{
#(let #names = <#types>::from_bytes(bytes)?;)*
Some(Self{
#(#names),*
})
}
}
}
.into()
}
syn::Data::Enum(e) => {
let mut idxs = Vec::new();
let mut idents = Vec::new();
let mut fields = Vec::new();
for (i, variant) in e.variants.iter().enumerate() {
idxs.push(i);
idents.push(variant.ident.clone());
fields.push(variant.fields.clone());
}
let mut froms_fb = Vec::new();
let mut froms_tb = Vec::new();
let mut matchs_tb = Vec::new();
for i in 0..idents.len() {
let ident = idents[i].clone();
let fields = fields[i].clone();
let mut f = Vec::new();
let mut t = Vec::new();
let mut tt = Vec::new();
let mut braket = false;
for (ii, field) in fields.iter().enumerate() {
let ty = field.ty.clone();
if let Some(ident) = field.ident.clone() {
braket = true;
f.push(quote!(
#ident : <#ty>::from_bytes(bytes)?
));
t.push(quote!(
#ident.to_bytes()
));
tt.push(quote!(#ident))
} else {
f.push(quote!(
<#ty>::from_bytes(bytes)?
));
let vii = format_ident!("v{}", ii);
t.push(quote!(#vii.to_bytes()));
tt.push(quote!(#vii))
}
}
if t.len() > 0 {
if braket {
matchs_tb.push(quote!(#ident{#(#tt),*}));
} else {
matchs_tb.push(quote!(#ident(#(#tt),*)));
}
froms_tb.push(quote! {
#(buffer.append(&mut #t);)*
});
} else {
matchs_tb.push(quote! {
#ident
});
froms_tb.push(quote! {});
}
if f.len() > 0 {
if braket {
froms_fb.push(quote! {
#ident{#(#f),*}
});
} else {
froms_fb.push(quote! {
#ident(#(#f),*)
});
}
} else {
froms_fb.push(quote! {
#ident
});
}
}
quote! {
impl TBytes for #ident{
fn size(&self) -> usize{
match self{
#(#idents => 0),*
}
}
fn to_bytes(&self) -> Vec<u8>{
let mut buffer = Vec::new();
match self{
#(Self::#matchs_tb => {buffer.append(&mut #idxs.to_bytes()); #froms_tb}),*
};
buffer
}
fn from_bytes(bytes: &mut Vec<u8>) -> Option<Self>{
let id = usize::from_bytes(bytes)?;
match id{
#(#idxs => Some(Self::#froms_fb),)*
_=> None
}
}
}
}
.into()
}
syn::Data::Union(_) => todo!(),
}
}