flat_message_proc_macro 0.2.1

Procedural macros for the FlatMessage serialization library.
Documentation
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use proc_macro2::TokenStream;
use quote::{format_ident, quote, ToTokens};
use syn::DeriveInput;

pub struct Flags {
    name: syn::Ident,
    sealed: bool,
    flags: Vec<String>,
    repr_size: u8,
}
impl Flags {
    fn compute_hash(&self) -> u32 {
        if self.sealed {
            let mut name = self.name.to_string();
            for flag in self.flags.iter() {
                name.push_str(flag);
                name.push(',');
            }
            crate::common::hashes::crc32(name.as_bytes())
        } else {
            crate::common::hashes::crc32(self.name.to_string().as_bytes())
        }
    }
    fn data_format(&self) -> proc_macro2::TokenStream {
        match self.repr_size {
            1 => quote! {DataFormat::Flags8},
            2 => quote! {DataFormat::Flags16},
            4 => quote! {DataFormat::Flags32},
            8 => quote! {DataFormat::Flags64},
            16 => quote! {DataFormat::Flags128},
            _ => quote! {},
        }
    }
    fn repr_type(&self) -> proc_macro2::TokenStream {
        match self.repr_size {
            1 => quote! {u8},
            2 => quote! {u16},
            4 => quote! {u32},
            8 => quote! {u64},
            16 => quote! {u128},
            _ => quote! {},
        }
    }
    fn generate_const_assertion_functions(&self) -> proc_macro2::TokenStream {
        // let name = &self.name;
        // //let align_size = self.mem_alignment.align_size();
        // let const_ident = format_ident!("_CONST_ASSERT_ALIGN_{}", name.to_string().to_uppercase());
        // quote! {
        //     const #const_ident: () = {
        //         if std::mem::align_of::<#name>() != #align_size {
        //             panic!(concat!(
        //                 "Incorrect representation for struct `",
        //                 stringify!(#name),
        //                 "`! Please check the #[repr(C, align(...))] attribute and make sure it matches std::mem::align_of::<",
        //                 stringify!(#name),
        //                 ">()"
        //             ));
        //         }
        //     };
        // }
        quote! {}
    }

    fn generate_flags_support_implementation(&self) -> TokenStream {
        let name = &self.name;
        let repr_type = self.repr_type();
        let flags = self
            .flags
            .iter()
            .map(|f| {
                let flag = format_ident!("{}", f);
                quote!(#name::#flag.0)
            }).collect::<Vec<_>>();
        let mask = quote! {
            const MASK: #repr_type = #(#flags |)* 0;
        };
        quote! {
            impl FlagsSupport<#repr_type> for   #name {
                fn from_value(value: #repr_type) -> Option<Self> {
                    #mask
                    if value | MASK == MASK {
                        Some(Self(value))
                    } else {
                        None
                    }
                }
                fn to_value(&self) -> #repr_type {
                    self.0
                }
                fn any_set(&self, flag: Self) -> bool {
                    self.0 & flag.0 != 0
                }
                fn all_set(&self, flag: Self) -> bool {
                    self.0 & flag.0 == flag.0
                }
                fn is_empty(&self) -> bool {
                    self.0 == 0
                }
                fn set(&mut self, flag: Self) {
                    self.0 |= flag.0;
                }
                fn unset(&mut self, flag: Self) {
                    self.0 &= !flag.0;
                }
                fn toggle(&mut self, flag: Self) {
                    self.0 ^= flag.0;
                }
                fn clear(&mut self) {
                    self.0 = 0;
                }
            }
            impl std::ops::BitOr for #name {
                type Output = Self;
                fn bitor(self, rhs: Self) -> Self::Output {
                    Self(self.0 | rhs.0)
                }
            }
            impl std::ops::BitAnd for #name {
                type Output = Self;
                fn bitand(self, rhs: Self) -> Self::Output {
                    Self(self.0 & rhs.0)
                }
            }
            impl std::ops::BitXor for #name {
                type Output = Self;
                fn bitxor(self, rhs: Self) -> Self::Output {
                    Self(self.0 ^ rhs.0)
                }
            }
            impl std::ops::BitAndAssign for #name {
                fn bitand_assign(&mut self, rhs: Self) {
                    self.0 &= rhs.0;
                }
            }
            impl std::ops::BitOrAssign for #name {
                fn bitor_assign(&mut self, rhs: Self) {
                    self.0 |= rhs.0;
                }
            }
            impl std::ops::BitXorAssign for #name {
                fn bitxor_assign(&mut self, rhs: Self) {
                    self.0 ^= rhs.0;
                }
            }
        }
    }

    fn generate_serde_implementation(&self) -> TokenStream {
        let name = &self.name;
        let name_hash = self.compute_hash();
        let data_format = self.data_format();
        let repr_type = self.repr_type();

        quote! {
            unsafe impl<'a> SerDe<'a> for #name {
                const DATA_FORMAT: flat_message::DataFormat = #data_format;
                #[inline(always)]
                unsafe fn from_buffer_unchecked(buf: &[u8], pos: usize) -> Self {
                    unsafe {
                        let ptr = buf.as_ptr().add(pos+4) as *const Self;
                        std::ptr::read_unaligned(ptr)
                    }
                }
                #[inline(always)]
                fn from_buffer(buf: &[u8], pos: usize) -> Option<Self> {
                    if pos + std::mem::size_of::<#repr_type>() + 4 > buf.len() {
                        None
                    } else {
                        unsafe {
                            let hash = (buf.as_ptr().add(pos) as *const u32).read_unaligned();
                            if hash != #name_hash {
                                return None;
                            }
                            let value = ((buf.as_ptr().add(pos+4) as *const #repr_type)).read_unaligned();
                            Self::from_value(value)
                        }
                    }
                }
                #[inline(always)]
                unsafe fn write(obj: &Self, p: *mut u8, pos: usize) -> usize {
                    unsafe {
                        std::ptr::write_unaligned(p.add(pos) as *mut u32, #name_hash);
                        std::ptr::write_unaligned(p.add(pos+4) as *mut #repr_type, obj.0);
                        pos + std::mem::size_of::<#repr_type>()+4
                    }
                }
                #[inline(always)]
                fn size(_: &Self) -> usize {
                    std::mem::size_of::<#repr_type>()+4 /* name hash + value */
                }
            }
        }
    }

    fn generate_slice_serde_implementation(&self) -> TokenStream {
        let name = &self.name;
        let data_format = self.data_format();
        let name_hash = self.compute_hash();
        let repr_type = self.repr_type();
        let (size_format, multiplier, slice) = match self.repr_size {
            1 => (
                quote! { U8withExtension },
                quote! {},
                quote! {&buf[pos + size_len..end];},
            ),
            2 => (
                quote! { U16withExtension },
                quote! { * 2 },
                quote! { unsafe { std::slice::from_raw_parts(buf.as_ptr().add(pos+size_len) as *const #repr_type, count) }; },
            ),
            4 => (
                quote! { U32 },
                quote! { *4 },
                quote! { unsafe { std::slice::from_raw_parts(buf.as_ptr().add(pos+size_len) as *const #repr_type, count) }; },
            ),
            8 => {
                // since we have the hash (4 bytes) we don't need to use U32onu64 as we are already aligned to 8 bytes
                (
                    quote! { U32 },
                    quote! { *8 },
                    quote! { unsafe { std::slice::from_raw_parts(buf.as_ptr().add(pos+size_len) as *const #repr_type, count) }; },
                )
            },
            16 => {
                // since we have the hash (4 bytes) we need aditional 12 bits
                (
                    quote! { U32on96bits },
                    quote! { *16 },
                    quote! { unsafe { std::slice::from_raw_parts(buf.as_ptr().add(pos+size_len) as *const #repr_type, count) }; },
                )
            }
            _ => panic!("Not defined enum representation type"),
        };

        quote! {
            unsafe impl<'a> SerDeSlice<'a> for #name {
                const DATA_FORMAT: flat_message::DataFormat = #data_format;
                #[inline(always)]
                unsafe fn from_buffer_unchecked(buf: &[u8], pos: usize) -> &'a [Self] {
                    let p = buf.as_ptr();
                    let pos = pos + 4; // skip the name hash
                    let (count, size_len) =
                        unsafe { flat_message::size::read_unchecked(p, pos, flat_message::size::Format::#size_format) };
                    std::slice::from_raw_parts(p.add(pos + size_len) as *const #name, count)
                }
                #[inline(always)]
                fn from_buffer(buf: &[u8], pos: usize) -> Option<&'a [Self]> {
                    if pos + 4 > buf.len() {
                        return None;
                    }
                    unsafe {
                        let hash = (buf.as_ptr().add(pos) as *const u32).read_unaligned();
                        if hash != #name_hash {
                            return None;
                        }
                    }
                    let pos = pos + 4;
                    let (count, size_len) =  unsafe { flat_message::size::read(
                        buf.as_ptr(),
                        pos,
                        buf.len(),
                        flat_message::size::Format::#size_format,
                    )? };
                    let end = pos + size_len + count #multiplier;
                    if end > buf.len() {
                        None
                    } else {
                        let slice = #slice
                        // check each value
                        for value in slice.iter() {
                            let _ = #name::from_value(*value as #repr_type)?;
                        }
                        Some(unsafe {
                            std::slice::from_raw_parts(
                                buf.as_ptr().add(pos + size_len) as *const #name,
                                count,
                            )
                        })
                    }
                }
                #[inline(always)]
                unsafe fn write(obj: &[Self], p: *mut u8, pos: usize) -> usize {
                    let len = obj.len() as u32;
                    unsafe {
                        std::ptr::write_unaligned(p.add(pos) as *mut u32, #name_hash);
                        let size_len =
                        flat_message::size::write(p, pos+4, len, flat_message::size::Format::#size_format);
                        std::ptr::copy_nonoverlapping(
                            obj.as_ptr() as *mut u8,
                            p.add(pos + size_len + 4),
                            obj.len() #multiplier,
                        );
                        pos + size_len + (len as usize) #multiplier  + 4usize
                    }
                }
                #[inline(always)]
                fn size(obj: &[Self]) -> usize {
                    flat_message::size::len(obj.len() as u32, flat_message::size::Format::#size_format)
                    + obj.len() #multiplier + 4usize /* name hash */
                }
            }
        }
    }

    fn generate_vector_serde_implementation(&self) -> TokenStream {
        let data_format = self.data_format();
        let name = &self.name;

        quote! {
            unsafe impl<TVecType: SerDeVecType<#name>> SerDeVec<'_, TVecType> for #name {
                const DATA_FORMAT: flat_message::DataFormat = #data_format;

                #[inline(always)]
                unsafe fn from_buffer_unchecked(buf: &[u8], pos: usize) -> TVecType {
                    let res: &[#name] = SerDeSlice::from_buffer_unchecked(buf, pos);
                    TVecType::from_slice(res)
                }
                #[inline(always)]
                fn from_buffer(buf: &[u8], pos: usize) -> Option<TVecType> {
                    let res: &[#name] = SerDeSlice::from_buffer(buf, pos)?;
                    Some(TVecType::from_slice(res))
                }
                #[inline(always)]
                unsafe fn write(obj: &TVecType, p: *mut u8, pos: usize) -> usize {
                    SerDeSlice::write(obj.as_slice(), p, pos)
                }
                #[inline(always)]
                fn size(obj: &TVecType) -> usize {
                    SerDeSlice::size(obj.as_slice())
                }
            }
        }
    }    


    pub fn generate_code(&self) -> TokenStream {
        let serde_code = self.generate_serde_implementation();
        let const_assertion_code = self.generate_const_assertion_functions();
        let flags_support_code = self.generate_flags_support_implementation();
        let name = &self.name;
        let slice_code = self.generate_slice_serde_implementation();
        let vec_code = self.generate_vector_serde_implementation();
        quote! {
            impl flat_message::FlatMessageCopy for #name {}
            #flags_support_code
            #const_assertion_code
            #serde_code
            // for slices
            #slice_code
            // for vectors
            #vec_code
        }
    }
}

impl TryFrom<syn::DeriveInput> for Flags {
    type Error = String;

    fn try_from(input: DeriveInput) -> Result<Self, Self::Error> {
        let mut repr = false;
        let mut sealed = false;
        let mut flags = Vec::<String>::new();
        for attr in input.attrs.iter() {
            if attr.path().is_ident("repr") {
                let s = attr.to_token_stream().to_string().replace(" ", "");
                if s != "#[repr(transparent)]" {
                    return Err("You can only use the `repr(transparent)` attribute for the struct to be serializable/deserializable as a flags object. ".to_string());
                }
                repr = true;
            }
            if attr.path().is_ident("sealed") {
                sealed = true;
            }
            if attr.path().is_ident("flags") {
                let s = attr.to_token_stream().to_string().replace(" ", "");
                if s.starts_with("#[flags(") && s.ends_with(")]") {
                    let flags_str = s.replace("#[flags(", "").replace(")]", "");
                    flags = flags_str.split(",").map(|f| f.trim().to_string()).collect();
                }
            }
        }
        // Extract the inner type from the struct's generic parameter
        let type_name = if let syn::Data::Struct(data_struct) = input.data {
            if let syn::Fields::Unnamed(fields) = data_struct.fields {
                if fields.unnamed.len() == 1 {
                    if let syn::Type::Path(type_path) = &fields.unnamed[0].ty {
                        if let Some(segment) = type_path.path.segments.last() {
                            segment.ident.to_string()
                        } else {
                            return Err("Invalid type parameter".to_string());
                        }
                    } else {
                        return Err("Invalid type parameter".to_string());
                    }
                } else {
                    return Err("Struct must have exactly one unnamed field".to_string());
                }
            } else {
                return Err("Struct must have unnamed fields".to_string());
            }
        } else {
            return Err("Only structs are supported".to_string());
        };
        let repr_size = match type_name.as_str() {
            "u8" => 1,
            "u16" => 2,
            "u32" => 4,
            "u64" => 8,
            "u128" => 16,
            _ => return Err("You need to add a type parameter to the struct to be serializable/deserializable as a flags object. ".to_string()),
        };
        if !repr {
            return Err("You need to add #[repr(transparent)] attribute to the struct to be serializable/deserializable as a flags object. ".to_string());
        }
        if flags.is_empty() {
            return Err("You need to add at least one flag in the #[flags(...)] attribute to the struct to be serializable/deserializable as a flags object. ".to_string());
        }
        flags.sort();
        Ok(Self {
            name: input.ident,
            sealed,
            flags,
            repr_size,
        })
    }
}