desert/
lib.rs

1#![cfg_attr(feature="nightly-features", feature(backtrace))]
2#![doc=include_str!("../readme.md")]
3
4pub mod varint;
5pub mod error;
6pub use error::*;
7
8/// Serialize a type into a sequence of bytes with unspecified endianness.
9/// The implementations for the built-in types are in big endian for this trait.
10pub trait ToBytes {
11  /// Serialize into a newly-allocated byte vector.
12  fn to_bytes(&self) -> Result<Vec<u8>,Error>;
13  /// Serialize into an existing mutable byte slice.
14  /// The usize Result contains how many bytes were written to `dst`.
15  fn write_bytes(&self, dst: &mut [u8]) -> Result<usize,Error> {
16    let bytes = self.to_bytes()?;
17    if dst.len() < bytes.len() {
18      return DesertErrorKind::DstInsufficient { provided: dst.len(), required: bytes.len() }.raise();
19    }
20    dst[0..bytes.len()].copy_from_slice(&bytes);
21    Ok(bytes.len())
22  }
23}
24/// Serialize a type into a sequence of bytes in big endian.
25pub trait ToBytesBE {
26  /// Serialize into a newly-allocated byte vector in big endian.
27  fn to_bytes_be(&self) -> Result<Vec<u8>,Error>;
28  /// Serialize into an existing mutable byte slice in big endian.
29  /// The usize Result contains how many bytes were written to `dst`.
30  fn write_bytes_be(&self, dst: &mut [u8]) -> Result<usize,Error> {
31    let bytes = self.to_bytes_be()?;
32    if dst.len() < bytes.len() {
33      return DesertErrorKind::DstInsufficient { provided: dst.len(), required: bytes.len() }.raise();
34    }
35    dst[0..bytes.len()].copy_from_slice(&bytes);
36    Ok(bytes.len())
37  }
38}
39/// Serialize a type into a sequence of bytes in little endian.
40pub trait ToBytesLE {
41  /// Serialize into a newly-allocated byte vector in little endian.
42  fn to_bytes_le(&self) -> Result<Vec<u8>,Error>;
43  /// Serialize into an existing mutable byte slice in little endian.
44  /// The usize Result contains how many bytes were written to `dst`.
45  fn write_bytes_le(&self, dst: &mut [u8]) -> Result<usize,Error> {
46    let bytes = self.to_bytes_le()?;
47    if dst.len() < bytes.len() {
48      return DesertErrorKind::DstInsufficient { provided: dst.len(), required: bytes.len() }.raise();
49    }
50    dst[0..bytes.len()].copy_from_slice(&bytes);
51    Ok(bytes.len())
52  }
53}
54
55/// Deserialize a sequence of bytes into a type.
56/// The implementations for the built-in types are in big endian for this trait.
57pub trait FromBytes: Sized {
58  /// Read data from `src` in order to create an instance `Self`.
59  /// The `usize` in the `Result` is the number of bytes read from `src`.
60  fn from_bytes(src: &[u8]) -> Result<(usize,Self),Error>;
61}
62/// Deserialize a sequence of bytes into a type in big endian.
63pub trait FromBytesBE: Sized {
64  /// Read data from `src` in order to create an instance `Self` in big endian.
65  /// The `usize` in the `Result` is the number of bytes read from `src`.
66  fn from_bytes_be(src: &[u8]) -> Result<(usize,Self),Error>;
67}
68/// Deserialize a sequence of bytes into a type in little endian.
69pub trait FromBytesLE: Sized {
70  /// Read data from `src` in order to create an instance `Self` in little
71  /// endian.
72  /// The `usize` in the `Result` is the number of bytes read from `src`.
73  fn from_bytes_le(src: &[u8]) -> Result<(usize,Self),Error>;
74}
75
76/// Count how many bytes to read from a byte slice for a type and calculate how
77/// many bytes the serialization would contain.
78/// The implementations for the built-in types are in big endian for this trait.
79pub trait CountBytes {
80  /// Return how many bytes from `buf` would be required to deserialize Self.
81  fn count_from_bytes(buf: &[u8]) -> Result<usize,Error>;
82  /// Return how many bytes from `buf` would be required to deserialize Self,
83  /// where if there are not enough bytes in `buf` to know how many bytes would
84  /// be required, you will receive `None` or otherwise `Some(nbytes)`.
85  fn count_from_bytes_more(buf: &[u8]) -> Result<Option<usize>,Error> {
86    Ok(Some(Self::count_from_bytes(buf)?))
87  }
88  /// Return the number of bytes that the serialization would require.
89  fn count_bytes(&self) -> usize;
90}
91/// Count how many bytes to read from a byte slice for a type and calculate how
92/// many bytes the serialization would contain. In big endian.
93pub trait CountBytesBE {
94  /// Return how many bytes from `buf` would be required to deserialize Self
95  /// in big endian.
96  fn count_from_bytes_be(buf: &[u8]) -> Result<usize,Error>;
97  /// Return how many bytes from `buf` would be required to deserialize Self
98  /// in big endian, where if there are not enough bytes in `buf` to know how
99  /// many bytes would be required, you will receive `None` or otherwise
100  /// `Some(nbytes)`.
101  fn count_from_bytes_be_more(buf: &[u8]) -> Result<Option<usize>,Error> {
102    Ok(Some(Self::count_from_bytes_be(buf)?))
103  }
104  /// Return the number of bytes that the serialization would require.
105  fn count_bytes_be(&self) -> usize;
106}
107/// Count how many bytes to read from a byte slice for a type and calculate how
108/// many bytes the serialization would contain. In little endian.
109pub trait CountBytesLE {
110  /// Return how many bytes from `buf` would be required to deserialize Self
111  /// in little endian.
112  fn count_from_bytes_le(buf: &[u8]) -> Result<usize,Error>;
113  /// Return how many bytes from `buf` would be required to deserialize Self
114  /// in little endian, where if there are not enough bytes in `buf` to know how
115  /// many bytes would be required, you will receive `None` or otherwise
116  /// `Some(nbytes)`.
117  fn count_from_bytes_le_more(buf: &[u8]) -> Result<Option<usize>,Error> {
118    Ok(Some(Self::count_from_bytes_le(buf)?))
119  }
120  /// Return the number of bytes that the serialization would require.
121  fn count_bytes_le(&self) -> usize;
122}
123
124macro_rules! buf_array {
125  ($b:tt,1) => {[$b[0]]};
126  ($b:tt,2) => {[$b[0],$b[1]]};
127  ($b:tt,4) => {[$b[0],$b[1],$b[2],$b[3]]};
128  ($b:tt,8) => {[$b[0],$b[1],$b[2],$b[3],$b[4],$b[5],$b[6],$b[7]]};
129  ($b:tt,16) => {[$b[0],$b[1],$b[2],$b[3],$b[4],$b[5],$b[6],$b[7],
130    $b[8],$b[9],$b[10],$b[11],$b[12],$b[13],$b[14],$b[15]]};
131}
132
133macro_rules! define_static_builtins {
134  ($(($T:tt,$n:tt)),+) => {$(
135    impl CountBytes for $T {
136      fn count_from_bytes(_buf: &[u8]) -> Result<usize,Error> {
137        Ok($n)
138      }
139      fn count_bytes(&self) -> usize { $n }
140    }
141    impl CountBytesBE for $T {
142      fn count_from_bytes_be(_buf: &[u8]) -> Result<usize,Error> {
143        Ok($n)
144      }
145      fn count_bytes_be(&self) -> usize { $n }
146    }
147    impl CountBytesLE for $T {
148      fn count_from_bytes_le(_buf: &[u8]) -> Result<usize,Error> {
149        Ok($n)
150      }
151      fn count_bytes_le(&self) -> usize { $n }
152    }
153    impl ToBytes for $T {
154      fn to_bytes(&self) -> Result<Vec<u8>,Error> {
155        self.to_bytes_be()
156      }
157      fn write_bytes(&self, dst: &mut [u8]) -> Result<usize,Error> {
158        self.write_bytes_be(dst)
159      }
160    }
161    impl ToBytesBE for $T {
162      fn to_bytes_be(&self) -> Result<Vec<u8>,Error> {
163        Ok(self.to_be_bytes().to_vec())
164      }
165      fn write_bytes_be(&self, dst: &mut [u8]) -> Result<usize,Error> {
166        let bytes = self.to_be_bytes();
167        if dst.len() < bytes.len() {
168          return DesertErrorKind::DstInsufficient { provided: dst.len(), required: bytes.len() }.raise();
169        }
170        dst[0..bytes.len()].copy_from_slice(&bytes);
171        Ok(bytes.len())
172      }
173    }
174    impl ToBytesLE for $T {
175      fn to_bytes_le(&self) -> Result<Vec<u8>,Error> {
176        Ok(self.to_le_bytes().to_vec())
177      }
178      fn write_bytes_le(&self, dst: &mut [u8]) -> Result<usize,Error> {
179        let bytes = self.to_le_bytes();
180        if dst.len() < bytes.len() {
181          return DesertErrorKind::DstInsufficient { provided: dst.len(), required: bytes.len() }.raise();
182        }
183        dst[0..bytes.len()].copy_from_slice(&bytes);
184        Ok(bytes.len())
185      }
186    }
187    impl FromBytes for $T {
188      fn from_bytes(src: &[u8]) -> Result<(usize,Self),Error> {
189        Self::from_bytes_be(src)
190      }
191    }
192    impl FromBytesBE for $T {
193      fn from_bytes_be(src: &[u8]) -> Result<(usize,Self),Error> {
194        if src.len() < $n {
195          return DesertErrorKind::SrcInsufficient { provided: src.len(), required: $n }.raise();
196        } else {
197          Ok(($n,$T::from_be_bytes(buf_array![src,$n])))
198        }
199      }
200    }
201    impl FromBytesLE for $T {
202      fn from_bytes_le(src: &[u8]) -> Result<(usize,Self),Error> {
203        if src.len() < $n {
204          return DesertErrorKind::SrcInsufficient { provided: src.len(), required: $n }.raise();
205        } else {
206          Ok(($n,$T::from_le_bytes(buf_array![src,$n])))
207        }
208      }
209    }
210  )*};
211}
212
213define_static_builtins![
214  (u8,1),(u16,2),(u32,4),(u64,8),(u128,16),
215  (i8,1),(i16,2),(i32,4),(i64,8),(i128,16),
216  (f32,4),(f64,8)
217];
218
219impl CountBytes for bool {
220  fn count_from_bytes(_buf: &[u8]) -> Result<usize,Error> { Ok(1) }
221  fn count_bytes(&self) -> usize { 1 }
222}
223impl CountBytesBE for bool {
224  fn count_from_bytes_be(_buf: &[u8]) -> Result<usize,Error> { Ok(1) }
225  fn count_bytes_be(&self) -> usize { 1 }
226}
227impl CountBytesLE for bool {
228  fn count_from_bytes_le(_buf: &[u8]) -> Result<usize,Error> { Ok(1) }
229  fn count_bytes_le(&self) -> usize { 1 }
230}
231
232impl ToBytes for bool {
233  fn to_bytes(&self) -> Result<Vec<u8>,Error> {
234    Ok(vec![if *self { 1 } else { 0 }])
235  }
236  fn write_bytes(&self, dst: &mut [u8]) -> Result<usize,Error> {
237    if dst.is_empty() {
238      return DesertErrorKind::DstInsufficient { provided: dst.len(), required: 1 }.raise();
239    }
240    dst[0] = *self as u8;
241    Ok(1)
242  }
243}
244impl ToBytesBE for bool {
245  fn to_bytes_be(&self) -> Result<Vec<u8>,Error> {
246    self.to_bytes()
247  }
248  fn write_bytes_be(&self, dst: &mut [u8]) -> Result<usize,Error> {
249    self.write_bytes(dst)
250  }
251}
252impl ToBytesLE for bool {
253  fn to_bytes_le(&self) -> Result<Vec<u8>,Error> {
254    self.to_bytes()
255  }
256  fn write_bytes_le(&self, dst: &mut [u8]) -> Result<usize,Error> {
257    self.write_bytes(dst)
258  }
259}
260
261impl FromBytes for bool {
262  fn from_bytes(src: &[u8]) -> Result<(usize,Self),Error> {
263    if src.is_empty() {
264      return DesertErrorKind::SrcInsufficient { provided: src.len(), required: 1 }.raise();
265    } else {
266      Ok((1,src[0] != 0))
267    }
268  }
269}
270impl FromBytesBE for bool {
271  fn from_bytes_be(src: &[u8]) -> Result<(usize,Self),Error> {
272    Self::from_bytes(src)
273  }
274}
275impl FromBytesLE for bool {
276  fn from_bytes_le(src: &[u8]) -> Result<(usize,Self),Error> {
277    Self::from_bytes(src)
278  }
279}
280
281macro_rules! define_tuple {
282  ($(($T:tt,$i:tt)),+) => {
283    impl<$($T),+> CountBytes for ($($T),+) where $($T: CountBytes),+ {
284      fn count_from_bytes(buf: &[u8]) -> Result<usize,Error> {
285        let mut offset = 0;
286        $(
287          offset += $T::count_from_bytes(&buf[offset..])?;
288        )+
289        Ok(offset)
290      }
291      fn count_bytes(&self) -> usize {
292        $(self.$i.count_bytes() +)+ 0
293      }
294    }
295    impl<$($T),+> CountBytesBE for ($($T),+) where $($T: CountBytesBE),+ {
296      fn count_from_bytes_be(buf: &[u8]) -> Result<usize,Error> {
297        let mut offset = 0;
298        $(
299          offset += $T::count_from_bytes_be(&buf[offset..])?;
300        )+
301        Ok(offset)
302      }
303      fn count_bytes_be(&self) -> usize {
304        $(self.$i.count_bytes_be() +)+ 0
305      }
306    }
307    impl<$($T),+> CountBytesLE for ($($T),+) where $($T: CountBytesLE),+ {
308      fn count_from_bytes_le(buf: &[u8]) -> Result<usize,Error> {
309        let mut offset = 0;
310        $(
311          offset += $T::count_from_bytes_le(&buf[offset..])?;
312        )+
313        Ok(offset)
314      }
315      fn count_bytes_le(&self) -> usize {
316        $(self.$i.count_bytes_le() +)+ 0
317      }
318    }
319
320    impl<$($T),+> ToBytes for ($($T),+) where $($T: ToBytes+CountBytes),+ {
321      fn to_bytes(&self) -> Result<Vec<u8>,Error> {
322        let mut buf = vec![0u8;$(self.$i.count_bytes() +)+ 0];
323        self.write_bytes(&mut buf)?;
324        Ok(buf)
325      }
326      fn write_bytes(&self, dst: &mut [u8]) -> Result<usize,Error> {
327        let len = $(self.$i.count_bytes() +)+ 0;
328        if dst.len() < len {
329          return DesertErrorKind::DstInsufficient { provided: dst.len(), required: len }.raise();
330        }
331        let mut offset = 0;
332        $(
333          offset += self.$i.write_bytes(&mut dst[offset..])?;
334        )+
335        Ok(offset)
336      }
337    }
338    impl<$($T),+> ToBytesBE for ($($T),+) where $($T: ToBytesBE+CountBytesBE),+ {
339      fn to_bytes_be(&self) -> Result<Vec<u8>,Error> {
340        let mut buf = vec![0u8;$(self.$i.count_bytes_be() +)+ 0];
341        self.write_bytes_be(&mut buf)?;
342        Ok(buf)
343      }
344      fn write_bytes_be(&self, dst: &mut [u8]) -> Result<usize,Error> {
345        let len = $(self.$i.count_bytes_be() +)+ 0;
346        if dst.len() < len {
347          return DesertErrorKind::DstInsufficient { provided: dst.len(), required: len }.raise();
348        }
349        let mut offset = 0;
350        $(
351          offset += self.$i.write_bytes_be(&mut dst[offset..])?;
352        )+
353        Ok(offset)
354      }
355    }
356    impl<$($T),+> ToBytesLE for ($($T),+) where $($T: ToBytesLE+CountBytesLE),+ {
357      fn to_bytes_le(&self) -> Result<Vec<u8>,Error> {
358        let mut buf = vec![0u8;$(self.$i.count_bytes_le() +)+ 0];
359        self.write_bytes_le(&mut buf)?;
360        Ok(buf)
361      }
362      fn write_bytes_le(&self, dst: &mut [u8]) -> Result<usize,Error> {
363        let len = $(self.$i.count_bytes_le() +)+ 0;
364        if dst.len() < len {
365          return DesertErrorKind::DstInsufficient { provided: dst.len(), required: len }.raise();
366        }
367        let mut offset = 0;
368        $(
369          offset += self.$i.write_bytes_le(&mut dst[offset..])?;
370        )+
371        Ok(offset)
372      }
373    }
374
375    impl<$($T),+> FromBytes for ($($T),+) where $($T: FromBytes),+ {
376      fn from_bytes(src: &[u8]) -> Result<(usize,Self),Error> {
377        let mut offset = 0;
378        let result = ($({
379          let (size,x) = $T::from_bytes(&src[offset..])?;
380          offset += size;
381          x
382        }),+);
383        Ok((offset,result))
384      }
385    }
386    impl<$($T),+> FromBytesBE for ($($T),+) where $($T: FromBytesBE),+ {
387      fn from_bytes_be(src: &[u8]) -> Result<(usize,Self),Error> {
388        let mut offset = 0;
389        let result = ($({
390          let (size,x) = $T::from_bytes_be(&src[offset..])?;
391          offset += size;
392          x
393        }),+);
394        Ok((offset,result))
395      }
396    }
397    impl<$($T),+> FromBytesLE for ($($T),+) where $($T: FromBytesLE),+ {
398      fn from_bytes_le(src: &[u8]) -> Result<(usize,Self),Error> {
399        let mut offset = 0;
400        let result = ($({
401          let (size,x) = $T::from_bytes_le(&src[offset..])?;
402          offset += size;
403          x
404        }),+);
405        Ok((offset,result))
406      }
407    }
408  }
409}
410
411define_tuple![(A,0),(B,1)];
412define_tuple![(A,0),(B,1),(C,2)];
413define_tuple![(A,0),(B,1),(C,2),(D,3)];
414define_tuple![(A,0),(B,1),(C,2),(D,3),(E,4)];
415define_tuple![(A,0),(B,1),(C,2),(D,3),(E,4),(F,5)];
416define_tuple![(A,0),(B,1),(C,2),(D,3),(E,4),(F,5),(G,6)];
417define_tuple![(A,0),(B,1),(C,2),(D,3),(E,4),(F,5),(G,6),(H,7)];
418define_tuple![(A,0),(B,1),(C,2),(D,3),(E,4),(F,5),(G,6),(H,7),(I,8)];
419define_tuple![(A,0),(B,1),(C,2),(D,3),(E,4),(F,5),(G,6),(H,7),(I,8),(J,9)];
420define_tuple![(A,0),(B,1),(C,2),(D,3),(E,4),(F,5),(G,6),(H,7),(I,8),(J,9),(K,10)];
421define_tuple![(A,0),(B,1),(C,2),(D,3),(E,4),(F,5),(G,6),(H,7),(I,8),(J,9),(K,10),(L,11)];
422
423impl<T, const N: usize> CountBytes for [T;N] where T: CountBytes {
424  fn count_from_bytes(buf: &[u8]) -> Result<usize,Error> {
425    let mut offset = 0;
426    for _i in 0..N {
427      offset += T::count_from_bytes(&buf[offset..])?;
428    }
429    Ok(offset)
430  }
431  fn count_bytes(&self) -> usize {
432    let mut size = 0;
433    for i in 0..N {
434      size += self[i].count_bytes();
435    }
436    size
437  }
438}
439impl<T, const N: usize> CountBytesBE for [T;N] where T: CountBytesBE {
440  fn count_from_bytes_be(buf: &[u8]) -> Result<usize,Error> {
441    let mut offset = 0;
442    for _i in 0..N {
443      offset += T::count_from_bytes_be(&buf[offset..])?;
444    }
445    Ok(offset)
446  }
447  fn count_bytes_be(&self) -> usize {
448    let mut size = 0;
449    for i in 0..N {
450      size += self[i].count_bytes_be();
451    }
452    size
453  }
454}
455impl<T, const N: usize> CountBytesLE for [T;N] where T: CountBytesLE {
456  fn count_from_bytes_le(buf: &[u8]) -> Result<usize,Error> {
457    let mut offset = 0;
458    for _i in 0..N {
459      offset += T::count_from_bytes_le(&buf[offset..])?;
460    }
461    Ok(offset)
462  }
463  fn count_bytes_le(&self) -> usize {
464    let mut size = 0;
465    for i in 0..N {
466      size += self[i].count_bytes_le();
467    }
468    size
469  }
470}
471
472impl<T, const N: usize> ToBytes for [T;N] where T: ToBytes+CountBytes {
473  fn to_bytes(&self) -> Result<Vec<u8>,Error> {
474    let mut buf = vec![0u8;self.count_bytes()];
475    self.write_bytes(&mut buf)?;
476    Ok(buf)
477  }
478  fn write_bytes(&self, dst: &mut [u8]) -> Result<usize,Error> {
479    let mut len = 0;
480    for i in 0..N {
481      len += self[i].count_bytes();
482    }
483    if dst.len() < len {
484      return DesertErrorKind::DstInsufficient { provided: dst.len(), required: len }.raise();
485    }
486    let mut offset = 0;
487    for i in 0..N {
488      offset += self[i].write_bytes(&mut dst[offset..])?;
489    }
490    Ok(offset)
491  }
492}
493impl<T, const N: usize> ToBytesBE for [T;N] where T: ToBytesBE+CountBytesBE {
494  fn to_bytes_be(&self) -> Result<Vec<u8>,Error> {
495    let mut buf = vec![0u8;self.count_bytes_be()];
496    self.write_bytes_be(&mut buf)?;
497    Ok(buf)
498  }
499  fn write_bytes_be(&self, dst: &mut [u8]) -> Result<usize,Error> {
500    let mut len = 0;
501    for i in 0..N {
502      len += self[i].count_bytes_be();
503    }
504    if dst.len() < len {
505      return DesertErrorKind::DstInsufficient { provided: dst.len(), required: len }.raise();
506    }
507    let mut offset = 0;
508    for i in 0..N {
509      offset += self[i].write_bytes_be(&mut dst[offset..])?;
510    }
511    Ok(offset)
512  }
513}
514impl<T, const N: usize> ToBytesLE for [T;N] where T: ToBytesLE+CountBytesLE {
515  fn to_bytes_le(&self) -> Result<Vec<u8>,Error> {
516    let mut buf = vec![0u8;self.count_bytes_le()];
517    self.write_bytes_le(&mut buf)?;
518    Ok(buf)
519  }
520  fn write_bytes_le(&self, dst: &mut [u8]) -> Result<usize,Error> {
521    let mut len = 0;
522    for i in 0..N {
523      len += self[i].count_bytes_le();
524    }
525    if dst.len() < len {
526      return DesertErrorKind::DstInsufficient { provided: dst.len(), required: len }.raise();
527    }
528    let mut offset = 0;
529    for i in 0..N {
530      offset += self[i].write_bytes_le(&mut dst[offset..])?;
531    }
532    Ok(offset)
533  }
534}
535
536impl<T, const N: usize> FromBytes for [T;N] where T: FromBytes+Default+Copy {
537  fn from_bytes(src: &[u8]) -> Result<(usize,Self),Error> {
538    let mut res = [T::default();N];
539    let mut offset = 0;
540    for i in 0..N {
541      let (size,x) = T::from_bytes(&src[offset..])?;
542      offset += size;
543      res[i] = x;
544    }
545    Ok((offset,res))
546  }
547}
548impl<T, const N: usize> FromBytesBE for [T;N] where T: FromBytesBE+Default+Copy {
549  fn from_bytes_be(src: &[u8]) -> Result<(usize,Self),Error> {
550    let mut res = [T::default();N];
551    let mut offset = 0;
552    for i in 0..N {
553      let (size,x) = T::from_bytes_be(&src[offset..])?;
554      offset += size;
555      res[i] = x;
556    }
557    Ok((offset,res))
558  }
559}
560impl<T, const N: usize> FromBytesLE for [T;N] where T: FromBytesLE+Default+Copy {
561  fn from_bytes_le(src: &[u8]) -> Result<(usize,Self),Error> {
562    let mut res = [T::default();N];
563    let mut offset = 0;
564    for i in 0..N {
565      let (size,x) = T::from_bytes_le(&src[offset..])?;
566      offset += size;
567      res[i] = x;
568    }
569    Ok((offset,res))
570  }
571}
572
573impl<T> ToBytes for [T] where T: ToBytes+CountBytes {
574  fn to_bytes(&self) -> Result<Vec<u8>,Error> {
575    let mut buf = vec![0u8;self.count_bytes()];
576    self.write_bytes(&mut buf)?;
577    Ok(buf)
578  }
579  fn write_bytes(&self, dst: &mut [u8]) -> Result<usize,Error> {
580    let mut len = 0;
581    for x in self.iter() {
582      len += x.count_bytes();
583    }
584    let hlen = varint::length(len as u64);
585    if dst.len() < len+hlen {
586      return DesertErrorKind::DstInsufficient { provided: dst.len(), required: len+hlen }.raise();
587    }
588    let mut offset = varint::encode(len as u64, dst)?;
589    for x in self.iter() {
590      offset += x.write_bytes(&mut dst[offset..])?;
591    }
592    Ok(offset)
593  }
594}
595impl<T> ToBytesBE for [T] where T: ToBytesBE+CountBytesBE {
596  fn to_bytes_be(&self) -> Result<Vec<u8>,Error> {
597    let mut buf = vec![0u8;self.count_bytes_be()];
598    self.write_bytes_be(&mut buf)?;
599    Ok(buf)
600  }
601  fn write_bytes_be(&self, dst: &mut [u8]) -> Result<usize,Error> {
602    let mut len = 0;
603    for x in self.iter() {
604      len += x.count_bytes_be();
605    }
606    let hlen = varint::length(len as u64);
607    if dst.len() < len+hlen {
608      return DesertErrorKind::DstInsufficient { provided: dst.len(), required: len+hlen }.raise();
609    }
610    let mut offset = varint::encode(len as u64, dst)?;
611    for x in self.iter() {
612      offset += x.write_bytes_be(&mut dst[offset..])?;
613    }
614    Ok(offset)
615  }
616}
617impl<T> ToBytesLE for [T] where T: ToBytesLE+CountBytesLE {
618  fn to_bytes_le(&self) -> Result<Vec<u8>,Error> {
619    let mut buf = vec![0u8;self.count_bytes_le()];
620    self.write_bytes_le(&mut buf)?;
621    Ok(buf)
622  }
623  fn write_bytes_le(&self, dst: &mut [u8]) -> Result<usize,Error> {
624    let mut len = 0;
625    for x in self.iter() {
626      len += x.count_bytes_le();
627    }
628    let hlen = varint::length(len as u64);
629    if dst.len() < len+hlen {
630      return DesertErrorKind::DstInsufficient { provided: dst.len(), required: len+hlen }.raise();
631    }
632    let mut offset = varint::encode(len as u64, dst)?;
633    for x in self.iter() {
634      offset += x.write_bytes_le(&mut dst[offset..])?;
635    }
636    Ok(offset)
637  }
638}
639
640impl<T> CountBytes for [T] where T: CountBytes {
641  fn count_from_bytes(buf: &[u8]) -> Result<usize,Error> {
642    let (mut offset,len) = varint::decode(buf)?;
643    let end = (offset as u64) + len;
644    while (offset as u64) < end {
645      offset += T::count_from_bytes(&buf[offset..])?;
646    }
647    Ok(offset)
648  }
649  fn count_bytes(&self) -> usize {
650    let mut len = 0;
651    for x in self.iter() {
652      len += x.count_bytes();
653    }
654    return len + varint::length(len as u64);
655  }
656}
657impl<T> CountBytesBE for [T] where T: CountBytesBE {
658  fn count_from_bytes_be(buf: &[u8]) -> Result<usize,Error> {
659    let (mut offset,len) = varint::decode(buf)?;
660    let end = (offset as u64) + len;
661    while (offset as u64) < end {
662      offset += T::count_from_bytes_be(&buf[offset..])?;
663    }
664    Ok(offset)
665  }
666  fn count_bytes_be(&self) -> usize {
667    let mut len = 0;
668    for x in self.iter() {
669      len += x.count_bytes_be();
670    }
671    return len + varint::length(len as u64);
672  }
673}
674impl<T> CountBytesLE for [T] where T: CountBytesLE {
675  fn count_from_bytes_le(buf: &[u8]) -> Result<usize,Error> {
676    let (mut offset,len) = varint::decode(buf)?;
677    let end = (offset as u64) + len;
678    while (offset as u64) < end {
679      offset += T::count_from_bytes_le(&buf[offset..])?;
680    }
681    Ok(offset)
682  }
683  fn count_bytes_le(&self) -> usize {
684    let mut len = 0;
685    for x in self.iter() {
686      len += x.count_bytes_le();
687    }
688    return len + varint::length(len as u64);
689  }
690}
691
692impl<T> ToBytes for Vec<T> where T: ToBytes+CountBytes {
693  fn to_bytes(&self) -> Result<Vec<u8>,Error> {
694    let mut buf = vec![0u8;self.count_bytes()];
695    self.write_bytes(&mut buf)?;
696    Ok(buf)
697  }
698  fn write_bytes(&self, dst: &mut [u8]) -> Result<usize,Error> {
699    let mut len = 0;
700    for x in self.iter() {
701      len += x.count_bytes();
702    }
703    let hlen = varint::length(len as u64);
704    if dst.len() < len+hlen {
705      return DesertErrorKind::DstInsufficient { provided: dst.len(), required: len+hlen }.raise();
706    }
707    let mut offset = varint::encode(len as u64, dst)?;
708    for x in self.iter() {
709      offset += x.write_bytes(&mut dst[offset..])?;
710    }
711    Ok(offset)
712  }
713}
714impl<T> ToBytesBE for Vec<T> where T: ToBytesBE+CountBytesBE {
715  fn to_bytes_be(&self) -> Result<Vec<u8>,Error> {
716    let mut buf = vec![0u8;self.count_bytes_be()];
717    self.write_bytes_be(&mut buf)?;
718    Ok(buf)
719  }
720  fn write_bytes_be(&self, dst: &mut [u8]) -> Result<usize,Error> {
721    let mut len = 0;
722    for x in self.iter() {
723      len += x.count_bytes_be();
724    }
725    let hlen = varint::length(len as u64);
726    if dst.len() < len+hlen {
727      return DesertErrorKind::DstInsufficient { provided: dst.len(), required: len+hlen }.raise();
728    }
729    let mut offset = varint::encode(len as u64, dst)?;
730    for x in self.iter() {
731      offset += x.write_bytes_be(&mut dst[offset..])?;
732    }
733    Ok(offset)
734  }
735}
736impl<T> ToBytesLE for Vec<T> where T: ToBytesLE+CountBytesLE {
737  fn to_bytes_le(&self) -> Result<Vec<u8>,Error> {
738    let mut buf = vec![0u8;self.count_bytes_le()];
739    self.write_bytes_le(&mut buf)?;
740    Ok(buf)
741  }
742  fn write_bytes_le(&self, dst: &mut [u8]) -> Result<usize,Error> {
743    let mut len = 0;
744    for x in self.iter() {
745      len += x.count_bytes_le();
746    }
747    let hlen = varint::length(len as u64);
748    if dst.len() < len+hlen {
749      return DesertErrorKind::DstInsufficient { provided: dst.len(), required: len+hlen }.raise();
750    }
751    let mut offset = varint::encode(len as u64, dst)?;
752    for x in self.iter() {
753      offset += x.write_bytes_le(&mut dst[offset..])?;
754    }
755    Ok(offset)
756  }
757}
758
759impl<T> FromBytes for Vec<T> where T: FromBytes {
760  fn from_bytes(src: &[u8]) -> Result<(usize,Self),Error> {
761    let (mut offset,len) = varint::decode(src)?;
762    let end = offset + (len as usize);
763    let mut v = vec![];
764    while offset < end {
765      let (size,x) = T::from_bytes(&src[offset..])?;
766      v.push(x);
767      offset += size;
768    }
769    Ok((end,v))
770  }
771}
772impl<T> FromBytesBE for Vec<T> where T: FromBytesBE {
773  fn from_bytes_be(src: &[u8]) -> Result<(usize,Self),Error> {
774    let (mut offset,len) = varint::decode(src)?;
775    let end = offset + (len as usize);
776    let mut v = vec![];
777    while offset < end {
778      let (size,x) = T::from_bytes_be(&src[offset..])?;
779      v.push(x);
780      offset += size;
781    }
782    Ok((end,v))
783  }
784}
785impl<T> FromBytesLE for Vec<T> where T: FromBytesLE {
786  fn from_bytes_le(src: &[u8]) -> Result<(usize,Self),Error> {
787    let (mut offset,len) = varint::decode(src)?;
788    let end = offset + (len as usize);
789    let mut v = vec![];
790    while offset < end {
791      let (size,x) = T::from_bytes_le(&src[offset..])?;
792      v.push(x);
793      offset += size;
794    }
795    Ok((end,v))
796  }
797}
798
799impl<T> CountBytes for Vec<T> where T: CountBytes {
800  fn count_from_bytes(buf: &[u8]) -> Result<usize,Error> {
801    let (mut offset,len) = varint::decode(buf)?;
802    let end = (offset as u64) + len;
803    while (offset as u64) < end {
804      offset += T::count_from_bytes(&buf[offset..])?;
805    }
806    Ok(offset)
807  }
808  fn count_bytes(&self) -> usize {
809    let mut len = 0;
810    for x in self.iter() {
811      len += x.count_bytes();
812    }
813    return len + varint::length(len as u64);
814  }
815}
816impl<T> CountBytesBE for Vec<T> where T: CountBytesBE {
817  fn count_from_bytes_be(buf: &[u8]) -> Result<usize,Error> {
818    let (mut offset,len) = varint::decode(buf)?;
819    let end = (offset as u64) + len;
820    while (offset as u64) < end {
821      offset += T::count_from_bytes_be(&buf[offset..])?;
822    }
823    Ok(offset)
824  }
825  fn count_bytes_be(&self) -> usize {
826    let mut len = 0;
827    for x in self.iter() {
828      len += x.count_bytes_be();
829    }
830    return len + varint::length(len as u64);
831  }
832}
833impl<T> CountBytesLE for Vec<T> where T: CountBytesLE {
834  fn count_from_bytes_le(buf: &[u8]) -> Result<usize,Error> {
835    let (mut offset,len) = varint::decode(buf)?;
836    let end = (offset as u64) + len;
837    while (offset as u64) < end {
838      offset += T::count_from_bytes_le(&buf[offset..])?;
839    }
840    Ok(offset)
841  }
842  fn count_bytes_le(&self) -> usize {
843    let mut len = 0;
844    for x in self.iter() {
845      len += x.count_bytes_le();
846    }
847    return len + varint::length(len as u64);
848  }
849}