@@ -46,9 +46,7 @@ impl Packable for u32 {
46
46
buf. extend ( iter:: once ( Format :: UINT8 ) . chain ( iter:: once ( * self as u8 ) ) ) ;
47
47
2
48
48
} else if * self <= u16:: MAX as u32 {
49
- buf. extend (
50
- iter:: once ( Format :: UINT16 ) . chain ( self . to_be_bytes ( ) . iter ( ) . skip ( 2 ) . copied ( ) ) ,
51
- ) ;
49
+ buf. extend ( iter:: once ( Format :: UINT16 ) . chain ( ( * self as u16 ) . to_be_bytes ( ) ) ) ;
52
50
3
53
51
} else {
54
52
buf. extend ( iter:: once ( Format :: UINT32 ) . chain ( self . to_be_bytes ( ) ) ) ;
@@ -69,14 +67,10 @@ impl Packable for u64 {
69
67
buf. extend ( iter:: once ( Format :: UINT8 ) . chain ( iter:: once ( * self as u8 ) ) ) ;
70
68
2
71
69
} else if * self <= u16:: MAX as u64 {
72
- buf. extend (
73
- iter:: once ( Format :: UINT16 ) . chain ( self . to_be_bytes ( ) . iter ( ) . skip ( 6 ) . copied ( ) ) ,
74
- ) ;
70
+ buf. extend ( iter:: once ( Format :: UINT16 ) . chain ( ( * self as u16 ) . to_be_bytes ( ) ) ) ;
75
71
3
76
72
} else if * self <= u32:: MAX as u64 {
77
- buf. extend (
78
- iter:: once ( Format :: UINT32 ) . chain ( self . to_be_bytes ( ) . iter ( ) . skip ( 4 ) . copied ( ) ) ,
79
- ) ;
73
+ buf. extend ( iter:: once ( Format :: UINT32 ) . chain ( ( * self as u32 ) . to_be_bytes ( ) ) ) ;
80
74
5
81
75
} else {
82
76
buf. extend ( iter:: once ( Format :: UINT64 ) . chain ( self . to_be_bytes ( ) ) ) ;
@@ -85,6 +79,37 @@ impl Packable for u64 {
85
79
}
86
80
}
87
81
82
+ impl Packable for u128 {
83
+ fn pack < T > ( & self , buf : & mut T ) -> usize
84
+ where
85
+ T : Extend < u8 > ,
86
+ {
87
+ if * self <= 127 {
88
+ buf. extend ( iter:: once ( * self as u8 & Format :: POSITIVE_FIXINT ) ) ;
89
+ 1
90
+ } else if * self <= u8:: MAX as u128 {
91
+ buf. extend ( iter:: once ( Format :: UINT8 ) . chain ( iter:: once ( * self as u8 ) ) ) ;
92
+ 2
93
+ } else if * self <= u16:: MAX as u128 {
94
+ buf. extend ( iter:: once ( Format :: UINT16 ) . chain ( ( * self as u16 ) . to_be_bytes ( ) ) ) ;
95
+ 3
96
+ } else if * self <= u32:: MAX as u128 {
97
+ buf. extend ( iter:: once ( Format :: UINT32 ) . chain ( ( * self as u32 ) . to_be_bytes ( ) ) ) ;
98
+ 5
99
+ } else if * self <= u64:: MAX as u128 {
100
+ buf. extend ( iter:: once ( Format :: UINT64 ) . chain ( ( * self as u64 ) . to_be_bytes ( ) ) ) ;
101
+ 9
102
+ } else {
103
+ buf. extend (
104
+ iter:: once ( Format :: BIN8 )
105
+ . chain ( iter:: once ( 16 ) )
106
+ . chain ( self . to_be_bytes ( ) ) ,
107
+ ) ;
108
+ 18
109
+ }
110
+ }
111
+ }
112
+
88
113
impl Packable for usize {
89
114
fn pack < T > ( & self , buf : & mut T ) -> usize
90
115
where
@@ -97,14 +122,10 @@ impl Packable for usize {
97
122
buf. extend ( iter:: once ( Format :: UINT8 ) . chain ( iter:: once ( * self as u8 ) ) ) ;
98
123
2
99
124
} else if * self <= u16:: MAX as usize {
100
- buf. extend (
101
- iter:: once ( Format :: UINT16 ) . chain ( self . to_be_bytes ( ) . iter ( ) . skip ( 6 ) . copied ( ) ) ,
102
- ) ;
125
+ buf. extend ( iter:: once ( Format :: UINT16 ) . chain ( ( * self as u16 ) . to_be_bytes ( ) ) ) ;
103
126
3
104
127
} else if * self <= u32:: MAX as usize {
105
- buf. extend (
106
- iter:: once ( Format :: UINT32 ) . chain ( self . to_be_bytes ( ) . iter ( ) . skip ( 4 ) . copied ( ) ) ,
107
- ) ;
128
+ buf. extend ( iter:: once ( Format :: UINT32 ) . chain ( ( * self as u32 ) . to_be_bytes ( ) ) ) ;
108
129
5
109
130
} else {
110
131
buf. extend ( iter:: once ( Format :: UINT64 ) . chain ( self . to_be_bytes ( ) ) ) ;
@@ -138,7 +159,7 @@ impl Packable for i16 {
138
159
{
139
160
if * self < i8:: MIN as i16 {
140
161
buf. extend ( iter:: once ( Format :: INT16 ) . chain ( self . to_be_bytes ( ) ) ) ;
141
- 2
162
+ 3
142
163
} else if * self <= -33 {
143
164
buf. extend ( iter:: once ( Format :: INT8 ) . chain ( iter:: once ( ( * self as i8 ) as u8 ) ) ) ;
144
165
2
@@ -150,7 +171,7 @@ impl Packable for i16 {
150
171
1
151
172
} else {
152
173
buf. extend ( iter:: once ( Format :: INT16 ) . chain ( self . to_be_bytes ( ) ) ) ;
153
- 2
174
+ 3
154
175
}
155
176
}
156
177
}
@@ -165,7 +186,7 @@ impl Packable for i32 {
165
186
5
166
187
} else if * self < i8:: MIN as i32 {
167
188
buf. extend ( iter:: once ( Format :: INT16 ) . chain ( ( * self as i16 ) . to_be_bytes ( ) ) ) ;
168
- 2
189
+ 3
169
190
} else if * self <= -33 {
170
191
buf. extend ( iter:: once ( Format :: INT8 ) . chain ( iter:: once ( ( * self as i8 ) as u8 ) ) ) ;
171
192
2
@@ -177,7 +198,7 @@ impl Packable for i32 {
177
198
1
178
199
} else if * self <= i16:: MAX as i32 {
179
200
buf. extend ( iter:: once ( Format :: INT16 ) . chain ( ( * self as i16 ) . to_be_bytes ( ) ) ) ;
180
- 2
201
+ 3
181
202
} else {
182
203
buf. extend ( iter:: once ( Format :: INT32 ) . chain ( self . to_be_bytes ( ) ) ) ;
183
204
5
@@ -198,7 +219,7 @@ impl Packable for i64 {
198
219
5
199
220
} else if * self < i8:: MIN as i64 {
200
221
buf. extend ( iter:: once ( Format :: INT16 ) . chain ( ( * self as i16 ) . to_be_bytes ( ) ) ) ;
201
- 2
222
+ 3
202
223
} else if * self <= -33 {
203
224
buf. extend ( iter:: once ( Format :: INT8 ) . chain ( iter:: once ( ( * self as i8 ) as u8 ) ) ) ;
204
225
2
@@ -210,7 +231,7 @@ impl Packable for i64 {
210
231
1
211
232
} else if * self <= i16:: MAX as i64 {
212
233
buf. extend ( iter:: once ( Format :: INT16 ) . chain ( ( * self as i16 ) . to_be_bytes ( ) ) ) ;
213
- 2
234
+ 3
214
235
} else if * self <= i32:: MAX as i64 {
215
236
buf. extend ( iter:: once ( Format :: INT32 ) . chain ( ( * self as i32 ) . to_be_bytes ( ) ) ) ;
216
237
5
@@ -221,6 +242,56 @@ impl Packable for i64 {
221
242
}
222
243
}
223
244
245
+ impl Packable for i128 {
246
+ fn pack < T > ( & self , buf : & mut T ) -> usize
247
+ where
248
+ T : Extend < u8 > ,
249
+ {
250
+ if * self < i64:: MIN as i128 {
251
+ buf. extend (
252
+ iter:: once ( Format :: BIN8 )
253
+ . chain ( iter:: once ( 16 ) )
254
+ . chain ( self . to_be_bytes ( ) ) ,
255
+ ) ;
256
+ 18
257
+ } else if * self < i32:: MIN as i128 {
258
+ buf. extend ( iter:: once ( Format :: INT64 ) . chain ( ( * self as i64 ) . to_be_bytes ( ) ) ) ;
259
+ 9
260
+ } else if * self < i16:: MIN as i128 {
261
+ buf. extend ( iter:: once ( Format :: INT32 ) . chain ( ( * self as i32 ) . to_be_bytes ( ) ) ) ;
262
+ 5
263
+ } else if * self < i8:: MIN as i128 {
264
+ buf. extend ( iter:: once ( Format :: INT16 ) . chain ( ( * self as i16 ) . to_be_bytes ( ) ) ) ;
265
+ 3
266
+ } else if * self <= -33 {
267
+ buf. extend ( iter:: once ( Format :: INT8 ) . chain ( iter:: once ( ( * self as i8 ) as u8 ) ) ) ;
268
+ 2
269
+ } else if * self <= -1 {
270
+ buf. extend ( iter:: once ( ( * self | -32i128 ) as u8 ) ) ;
271
+ 1
272
+ } else if * self <= i8:: MAX as i128 {
273
+ buf. extend ( iter:: once ( * self as u8 & Format :: POSITIVE_FIXINT ) ) ;
274
+ 1
275
+ } else if * self <= i16:: MAX as i128 {
276
+ buf. extend ( iter:: once ( Format :: INT16 ) . chain ( ( * self as i16 ) . to_be_bytes ( ) ) ) ;
277
+ 3
278
+ } else if * self <= i32:: MAX as i128 {
279
+ buf. extend ( iter:: once ( Format :: INT32 ) . chain ( ( * self as i32 ) . to_be_bytes ( ) ) ) ;
280
+ 5
281
+ } else if * self <= i64:: MAX as i128 {
282
+ buf. extend ( iter:: once ( Format :: INT64 ) . chain ( ( * self as i64 ) . to_be_bytes ( ) ) ) ;
283
+ 9
284
+ } else {
285
+ buf. extend (
286
+ iter:: once ( Format :: BIN8 )
287
+ . chain ( iter:: once ( 16 ) )
288
+ . chain ( self . to_be_bytes ( ) ) ,
289
+ ) ;
290
+ 18
291
+ }
292
+ }
293
+ }
294
+
224
295
impl Packable for isize {
225
296
fn pack < T > ( & self , buf : & mut T ) -> usize
226
297
where
@@ -234,7 +305,7 @@ impl Packable for isize {
234
305
5
235
306
} else if * self < i8:: MIN as isize {
236
307
buf. extend ( iter:: once ( Format :: INT16 ) . chain ( ( * self as i16 ) . to_be_bytes ( ) ) ) ;
237
- 2
308
+ 3
238
309
} else if * self <= -33 {
239
310
buf. extend ( iter:: once ( Format :: INT8 ) . chain ( iter:: once ( ( * self as i8 ) as u8 ) ) ) ;
240
311
2
@@ -246,7 +317,7 @@ impl Packable for isize {
246
317
1
247
318
} else if * self <= i16:: MAX as isize {
248
319
buf. extend ( iter:: once ( Format :: INT16 ) . chain ( ( * self as i16 ) . to_be_bytes ( ) ) ) ;
249
- 2
320
+ 3
250
321
} else if * self <= i32:: MAX as isize {
251
322
buf. extend ( iter:: once ( Format :: INT32 ) . chain ( ( * self as i32 ) . to_be_bytes ( ) ) ) ;
252
323
5
0 commit comments