-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings_var_len_regular.rs
97 lines (84 loc) · 2.93 KB
/
strings_var_len_regular.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
mod unittest;
use byteserde::prelude::*;
use byteserde_derive::{ByteDeserializeSlice, ByteSerializeHeap, ByteSerializeStack, ByteSerializedLenOf};
use byteserde_types::{const_char_ascii, prelude::*};
use log::info;
use unittest::setup;
const_char_ascii!(Plus, b'+', #[derive(ByteSerializeStack, ByteSerializeHeap, ByteSerializedLenOf, PartialEq)]);
#[derive(ByteDeserializeSlice, ByteSerializeStack, ByteSerializeHeap, Debug, PartialEq)]
#[byteserde(endian = "be")]
struct VariableLenMsg {
#[byteserde(replace( (text.len() + packet_type.byte_len()) as u16 ))]
packet_length: u16,
packet_type: Plus,
#[byteserde(deplete( packet_length as usize - packet_type.byte_len() ))]
text: StringAscii,
}
impl Default for VariableLenMsg {
fn default() -> Self {
Self {
packet_length: Default::default(),
packet_type: Default::default(),
text: b"0123456789".into(),
}
}
}
#[test]
fn test_strings_ascii() {
strings_ascii()
}
fn strings_ascii() {
setup::log::configure();
let inp_debug = VariableLenMsg::default();
info!("inp_debug: {:?}", inp_debug);
let tail = &[0x01, 0x02, 0x3];
// stack
let mut ser_stack: ByteSerializerStack<135> = to_serializer_stack(&inp_debug).unwrap();
ser_stack.serialize_bytes_slice(tail).unwrap();
info!("ser_stack: {ser_stack:#x}");
// heap
let mut ser_heap = to_serializer_heap(&inp_debug).unwrap();
ser_heap.serialize_bytes_slice(tail).unwrap();
info!("ser_heap: {ser_heap:#x}");
assert_eq!(ser_stack.as_slice(), ser_heap.as_slice());
let des = &mut ByteDeserializerSlice::new(ser_stack.as_slice());
let out_debug = VariableLenMsg::byte_deserialize(des).unwrap();
info!("out_debug: {:?}", out_debug);
info!("des: {:#x}", des);
assert_eq!(inp_debug.packet_length + 11, out_debug.packet_length);
assert_eq!(inp_debug.packet_type, out_debug.packet_type);
assert_eq!(inp_debug.text, out_debug.text);
assert_eq!(des.remaining(), tail.len());
}
#[derive(ByteSerializeStack, ByteSerializeHeap, ByteDeserializeSlice, Debug, PartialEq)]
struct Strings {
field_string: String,
field_char: char,
}
#[test]
fn test_strings_utf8() {
strings_utf8()
}
fn strings_utf8() {
setup::log::configure();
let inp_str = Strings {
field_string: "whatever".to_string(),
field_char: '♥', // 3 bytes long
};
// stack
let ser_stack: ByteSerializerStack<128> = to_serializer_stack(&inp_str).unwrap();
info!("ser_stack: {ser_stack:#x}");
// heap
let ser_heap: ByteSerializerHeap = to_serializer_heap(&inp_str).unwrap();
info!("ser_heap: {ser_heap:#x}");
assert_eq!(ser_stack.as_slice(), ser_heap.as_slice());
// deserialize
let out_str: Strings = from_serializer_heap(&ser_heap).unwrap();
info!("inp_str: {:?}", inp_str);
info!("inp_str: {:?}", out_str);
assert_eq!(inp_str, out_str);
}
fn main() {
strings_ascii();
strings_utf8();
}