This repository has been archived by the owner on Sep 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
lib.rs
338 lines (315 loc) · 11 KB
/
lib.rs
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#![recursion_limit="1024"]
extern crate proc_macro;
extern crate proc_macro2;
#[macro_use]
extern crate quote;
extern crate syn;
use proc_macro::TokenStream;
fn impl_struct(name: &syn::Ident, fields: &syn::FieldsNamed) -> proc_macro2::TokenStream {
let items: Vec<_> = fields.named.iter().map(|f| {
let ident = &f.ident;
let ty = &f.ty;
match ty {
&syn::Type::Array(ref array) => {
match array.len {
syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(ref int), ..}) => {
let size = int.value();
quote! {
#ident: { let mut __tmp: #ty = [0; #size as usize]; src.gread_inout_with(offset, &mut __tmp, ctx)?; __tmp }
}
},
_ => panic!("Pread derive with bad array constexpr")
}
},
_ => {
quote! {
#ident: src.gread_with::<#ty>(offset, ctx)?
}
}
}
}).collect();
quote! {
impl<'a> ::scroll::ctx::TryFromCtx<'a, ::scroll::Endian> for #name where #name: 'a {
type Error = ::scroll::Error;
type Size = usize;
#[inline]
fn try_from_ctx(src: &'a [u8], ctx: ::scroll::Endian) -> ::scroll::export::result::Result<(Self, Self::Size), Self::Error> {
use ::scroll::Pread;
let offset = &mut 0;
let data = #name { #(#items,)* };
Ok((data, *offset))
}
}
}
}
fn impl_try_from_ctx(ast: &syn::DeriveInput) -> proc_macro2::TokenStream {
let name = &ast.ident;
match ast.data {
syn::Data::Struct(ref data) => {
match data.fields {
syn::Fields::Named(ref fields) => {
impl_struct(name, fields)
},
_ => {
panic!("Pread can only be derived for a regular struct with public fields")
}
}
},
_ => panic!("Pread can only be derived for structs")
}
}
#[proc_macro_derive(Pread)]
pub fn derive_pread(input: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(input).unwrap();
let gen = impl_try_from_ctx(&ast);
gen.into()
}
fn impl_try_into_ctx(name: &syn::Ident, fields: &syn::FieldsNamed) -> proc_macro2::TokenStream {
let items: Vec<_> = fields.named.iter().map(|f| {
let ident = &f.ident;
let ty = &f.ty;
match ty {
&syn::Type::Array(_) => {
quote! {
for i in 0..self.#ident.len() {
dst.gwrite_with(self.#ident[i], offset, ctx)?;
}
}
},
_ => {
quote! {
dst.gwrite_with(self.#ident, offset, ctx)?
}
}
}
}).collect();
quote! {
impl<'a> ::scroll::ctx::TryIntoCtx<::scroll::Endian> for &'a #name {
type Error = ::scroll::Error;
type Size = usize;
#[inline]
fn try_into_ctx(self, dst: &mut [u8], ctx: ::scroll::Endian) -> ::scroll::export::result::Result<Self::Size, Self::Error> {
use ::scroll::Pwrite;
let offset = &mut 0;
#(#items;)*;
Ok(*offset)
}
}
impl ::scroll::ctx::TryIntoCtx<::scroll::Endian> for #name {
type Error = ::scroll::Error;
type Size = usize;
#[inline]
fn try_into_ctx(self, dst: &mut [u8], ctx: ::scroll::Endian) -> ::scroll::export::result::Result<Self::Size, Self::Error> {
(&self).try_into_ctx(dst, ctx)
}
}
}
}
fn impl_pwrite(ast: &syn::DeriveInput) -> proc_macro2::TokenStream {
let name = &ast.ident;
match ast.data {
syn::Data::Struct(ref data) => {
match data.fields {
syn::Fields::Named(ref fields) => {
impl_try_into_ctx(name, fields)
},
_ => {
panic!("Pwrite can only be derived for a regular struct with public fields")
}
}
},
_ => panic!("Pwrite can only be derived for structs")
}
}
#[proc_macro_derive(Pwrite)]
pub fn derive_pwrite(input: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(input).unwrap();
let gen = impl_pwrite(&ast);
gen.into()
}
fn size_with(name: &syn::Ident, fields: &syn::FieldsNamed) -> proc_macro2::TokenStream {
let items: Vec<_> = fields.named.iter().map(|f| {
let ty = &f.ty;
match ty {
&syn::Type::Array(ref array) => {
let elem = &array.elem;
match array.len {
syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(ref int), ..}) => {
let size = int.value() as usize;
quote! {
(#size * <#elem>::size_with(ctx))
}
},
_ => panic!("Pread derive with bad array constexpr")
}
},
_ => {
quote! {
<#ty>::size_with(ctx)
}
}
}
}).collect();
quote! {
impl ::scroll::ctx::SizeWith<::scroll::Endian> for #name {
type Units = usize;
#[inline]
fn size_with(ctx: &::scroll::Endian) -> Self::Units {
0 #(+ #items)*
}
}
}
}
fn impl_size_with(ast: &syn::DeriveInput) -> proc_macro2::TokenStream {
let name = &ast.ident;
match ast.data {
syn::Data::Struct(ref data) => {
match data.fields {
syn::Fields::Named(ref fields) => {
size_with(name, fields)
},
_ => {
panic!("SizeWith can only be derived for a regular struct with public fields")
}
}
},
_ => panic!("SizeWith can only be derived for structs")
}
}
#[proc_macro_derive(SizeWith)]
pub fn derive_sizewith(input: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(input).unwrap();
let gen = impl_size_with(&ast);
gen.into()
}
fn impl_cread_struct(name: &syn::Ident, fields: &syn::FieldsNamed) -> proc_macro2::TokenStream {
let items: Vec<_> = fields.named.iter().map(|f| {
let ident = &f.ident;
let ty = &f.ty;
match ty {
&syn::Type::Array(ref array) => {
let arrty = &array.elem;
match array.len {
syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(ref int), ..}) => {
let size = int.value();
let incr = quote! { ::scroll::export::mem::size_of::<#arrty>() };
quote! {
#ident: {
let mut __tmp: #ty = [0; #size as usize];
for i in 0..__tmp.len() {
__tmp[i] = src.cread_with(*offset, ctx);
*offset += #incr;
}
__tmp
}
}
},
_ => panic!("IOread derive with bad array constexpr")
}
},
_ => {
let size = quote! { ::scroll::export::mem::size_of::<#ty>() };
quote! {
#ident: { let res = src.cread_with::<#ty>(*offset, ctx); *offset += #size; res }
}
}
}
}).collect();
quote! {
impl ::scroll::ctx::FromCtx<::scroll::Endian> for #name {
#[inline]
fn from_ctx(src: &[u8], ctx: ::scroll::Endian) -> Self {
use ::scroll::Cread;
let offset = &mut 0;
let data = #name { #(#items,)* };
data
}
}
}
}
fn impl_from_ctx(ast: &syn::DeriveInput) -> proc_macro2::TokenStream {
let name = &ast.ident;
match ast.data {
syn::Data::Struct(ref data) => {
match data.fields {
syn::Fields::Named(ref fields) => {
impl_cread_struct(name, fields)
},
_ => {
panic!("IOread can only be derived for a regular struct with public fields")
}
}
},
_ => panic!("IOread can only be derived for structs")
}
}
#[proc_macro_derive(IOread)]
pub fn derive_ioread(input: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(input).unwrap();
let gen = impl_from_ctx(&ast);
gen.into()
}
fn impl_into_ctx(name: &syn::Ident, fields: &syn::FieldsNamed) -> proc_macro2::TokenStream {
let items: Vec<_> = fields.named.iter().map(|f| {
let ident = &f.ident;
let ty = &f.ty;
let size = quote! { ::scroll::export::mem::size_of::<#ty>() };
match ty {
&syn::Type::Array(ref array) => {
let arrty = &array.elem;
quote! {
let size = ::scroll::export::mem::size_of::<#arrty>();
for i in 0..self.#ident.len() {
dst.cwrite_with(self.#ident[i], *offset, ctx);
*offset += size;
}
}
},
_ => {
quote! {
dst.cwrite_with(self.#ident, *offset, ctx);
*offset += #size;
}
}
}
}).collect();
quote! {
impl<'a> ::scroll::ctx::IntoCtx<::scroll::Endian> for &'a #name {
#[inline]
fn into_ctx(self, dst: &mut [u8], ctx: ::scroll::Endian) {
use ::scroll::Cwrite;
let offset = &mut 0;
#(#items;)*;
()
}
}
impl ::scroll::ctx::IntoCtx<::scroll::Endian> for #name {
#[inline]
fn into_ctx(self, dst: &mut [u8], ctx: ::scroll::Endian) {
(&self).into_ctx(dst, ctx)
}
}
}
}
fn impl_iowrite(ast: &syn::DeriveInput) -> proc_macro2::TokenStream {
let name = &ast.ident;
match ast.data {
syn::Data::Struct(ref data) => {
match data.fields {
syn::Fields::Named(ref fields) => {
impl_into_ctx(name, fields)
},
_ => {
panic!("IOwrite can only be derived for a regular struct with public fields")
}
}
},
_ => panic!("IOwrite can only be derived for structs")
}
}
#[proc_macro_derive(IOwrite)]
pub fn derive_iowrite(input: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(input).unwrap();
let gen = impl_iowrite(&ast);
gen.into()
}