@@ -7,11 +7,12 @@ pub mod buffer;
7
7
/// not require allocation and could speed things up.
8
8
pub trait Serialize {
9
9
fn encode < ' buf > ( & self , write_buf : & ' buf mut [ u8 ] ) -> Store < ' buf > ;
10
+ fn decode ( read_buf : & [ u8 ] ) -> ( String , & [ u8 ] ) ;
10
11
fn buffer_size_required ( & self ) -> usize ;
11
12
}
12
13
13
14
/// Function pointer which decodes a byte buffer back into `String` representation
14
- pub type DecodeFn = fn ( & [ u8 ] ) -> String ;
15
+ pub type DecodeFn = fn ( & [ u8 ] ) -> ( String , & [ u8 ] ) ;
15
16
16
17
/// Contains the decode function required to decode `buffer` back into a `String`
17
18
/// representation.
@@ -27,7 +28,8 @@ impl Store<'_> {
27
28
}
28
29
29
30
pub fn as_string ( & self ) -> String {
30
- ( self . decode_fn ) ( self . buffer )
31
+ let ( s, _) = ( self . decode_fn ) ( self . buffer ) ;
32
+ s
31
33
}
32
34
}
33
35
@@ -42,15 +44,18 @@ macro_rules! gen_serialize {
42
44
impl Serialize for $primitive {
43
45
fn encode<' buf>( & self , write_buf: & ' buf mut [ u8 ] ) -> Store <' buf> {
44
46
assert!( std:: mem:: size_of:: <$primitive>( ) == write_buf. len( ) ) ;
45
- fn decode( read_buf: & [ u8 ] ) -> String {
46
- let x = <$primitive>:: from_le_bytes( read_buf. try_into( ) . unwrap( ) ) ;
47
- format!( "{}" , x)
48
- }
49
47
50
48
let size = std:: mem:: size_of:: <$primitive>( ) ;
51
49
let ( x, _) = write_buf. split_at_mut( size) ;
52
50
x. copy_from_slice( & self . to_le_bytes( ) ) ;
53
- Store :: new( decode, & * x)
51
+ Store :: new( Self :: decode, x)
52
+ }
53
+
54
+ fn decode( read_buf: & [ u8 ] ) -> ( String , & [ u8 ] ) {
55
+ let ( chunk, rest) = read_buf. split_at( std:: mem:: size_of:: <$primitive>( ) ) ;
56
+ let x = <$primitive>:: from_le_bytes( chunk. try_into( ) . unwrap( ) ) ;
57
+
58
+ ( format!( "{}" , x) , rest)
54
59
}
55
60
56
61
fn buffer_size_required( & self ) -> usize {
@@ -72,12 +77,13 @@ gen_serialize!(usize);
72
77
impl Serialize for & str {
73
78
fn encode < ' buf > ( & self , write_buf : & ' buf mut [ u8 ] ) -> Store < ' buf > {
74
79
assert ! ( self . len( ) == write_buf. len( ) ) ;
75
- fn decode ( read_buf : & [ u8 ] ) -> String {
76
- let x = from_utf8 ( read_buf) . unwrap ( ) ;
77
- x. to_string ( )
78
- }
79
80
write_buf. copy_from_slice ( self . as_bytes ( ) ) ;
80
- Store :: new ( decode, write_buf)
81
+ Store :: new ( Self :: decode, write_buf)
82
+ }
83
+
84
+ fn decode ( read_buf : & [ u8 ] ) -> ( String , & [ u8 ] ) {
85
+ let x = from_utf8 ( read_buf) . unwrap ( ) ;
86
+ ( x. to_string ( ) , & [ ] )
81
87
}
82
88
83
89
fn buffer_size_required ( & self ) -> usize {
@@ -92,9 +98,9 @@ pub fn encode_debug<T: std::fmt::Debug>(val: T, write_buf: &mut [u8]) -> Store {
92
98
// `buffer_size_required`
93
99
assert ! ( val_string. len( ) <= write_buf. len( ) ) ;
94
100
95
- fn decode ( read_buf : & [ u8 ] ) -> String {
101
+ fn decode ( read_buf : & [ u8 ] ) -> ( String , & [ u8 ] ) {
96
102
let x = from_utf8 ( read_buf) . unwrap ( ) ;
97
- x. to_string ( )
103
+ ( x. to_string ( ) , & [ ] )
98
104
}
99
105
100
106
let ( chunk, _) = write_buf. split_at_mut ( val_string. len ( ) ) ;
@@ -115,7 +121,7 @@ mod tests {
115
121
116
122
let x: $primitive = $val;
117
123
let x_store = x. encode( & mut buf) ;
118
- assert_eq!( format!( "{}" , x) , ( x_store. decode_fn ) ( & buf ) ) ;
124
+ assert_eq!( format!( "{}" , x) , format! ( "{}" , x_store) ) ;
119
125
} } ;
120
126
}
121
127
@@ -146,13 +152,9 @@ mod tests {
146
152
let b_store = b. encode ( b_chunk) ;
147
153
let c_store = c. encode ( c_chunk) ;
148
154
149
- let a_str = ( a_store. decode_fn ) ( a_store. buffer ) ;
150
- let b_str = ( b_store. decode_fn ) ( b_store. buffer ) ;
151
- let c_str = ( c_store. decode_fn ) ( c_store. buffer ) ;
152
-
153
155
assert_eq ! (
154
156
format!( "{} {} {}" , a, b, c) ,
155
- format!( "{} {} {}" , a_str , b_str , c_str )
157
+ format!( "{} {} {}" , a_store , b_store , c_store )
156
158
)
157
159
}
158
160
0 commit comments