-
Notifications
You must be signed in to change notification settings - Fork 0
/
generics_tuple.rs
78 lines (63 loc) · 2.99 KB
/
generics_tuple.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
mod unittest;
use byteserde::prelude::*;
use byteserde_derive::{ByteDeserializeSlice, ByteSerializeHeap, ByteSerializeStack};
use log::info;
use unittest::setup;
#[derive(ByteSerializeStack, ByteSerializeHeap, ByteDeserializeSlice, Debug, PartialEq, Clone)]
#[byteserde(endian = "le")]
pub struct NumbersStructRegular<const L: usize, const M: usize>(#[byteserde(endian = "be")] [u16; L], [u16; M]);
#[derive(ByteSerializeStack, ByteSerializeHeap, ByteDeserializeSlice, Debug, PartialEq, Clone)]
pub struct StringsStructRegular<S: ByteSerializeStack+ByteSerializeHeap+ByteDeserializeSlice<S>, C: ByteSerializeStack+ByteSerializeHeap+ByteDeserializeSlice<C>>(S, C);
#[derive(ByteSerializeStack, ByteSerializeHeap, ByteDeserializeSlice, Debug, PartialEq)]
pub struct NestedStructRegular<const L: usize, const M: usize, S: ByteSerializeStack+ByteSerializeHeap+ByteDeserializeSlice<S>, C: ByteSerializeStack+ByteSerializeHeap+ByteDeserializeSlice<C>>(
NumbersStructRegular<L, M>,
StringsStructRegular<S, C>,
);
#[test]
fn test_all() {
all()
}
fn all() {
setup::log::configure();
// **************** NUMERICS ****************
let inp_num = NumbersStructRegular::<2, 3>([0x0001_u16, 0x0002_u16], [0x0001_u16, 0x0002_u16, 0x0003_u16]);
// stack
let ser_stack: ByteSerializerStack<128> = to_serializer_stack(&inp_num).unwrap();
info!("ser_stack: {ser_stack:#x}");
// heap
let ser_heap: ByteSerializerHeap = to_serializer_heap(&inp_num).unwrap();
info!("ser_heap: {ser_heap:#x}");
assert_eq!(ser_stack.as_slice(), ser_heap.as_slice());
// deserialize
let out_num: NumbersStructRegular<2, 3> = from_serializer_stack(&ser_stack).unwrap();
info!("inp_num: {inp_num:?}");
info!("out_num: {out_num:?}");
assert_eq!(inp_num, out_num);
// **************** STRINGS ****************
let inp_str = StringsStructRegular::<String, char>("Hello".to_string(), 'a');
let ser_stack: ByteSerializerStack<128> = to_serializer_stack(&inp_str).unwrap();
info!("ser_stack: {ser_stack:#x}");
let ser_heap: ByteSerializerHeap = to_serializer_heap(&inp_str).unwrap();
info!("ser_heap: {ser_heap:#x}");
let out_str = from_serializer_stack(&ser_stack).unwrap();
info!("inp_str: {inp_str:?}");
info!("out_str: {out_str:?}");
assert_eq!(inp_str, out_str);
// **************** NESTED ****************
let inp_nes = NestedStructRegular::<2, 3, String, char>(inp_num.clone(), inp_str.clone());
// stack
let ser_stack: ByteSerializerStack<128> = to_serializer_stack(&inp_nes).unwrap();
info!("ser_stack: {ser_stack:#x}");
// heap
let ser_heap: ByteSerializerHeap = to_serializer_heap(&inp_nes).unwrap();
info!("ser_heap: {ser_heap:#x}");
assert_eq!(ser_stack.as_slice(), ser_heap.as_slice());
// deserialize
let out_nes: NestedStructRegular<2, 3, String, char> = from_serializer_stack(&ser_stack).unwrap();
info!("inp_nes: {inp_nes:?}");
info!("out_nes: {out_nes:?}");
assert_eq!(inp_nes, out_nes);
}
fn main() {
all()
}