| 1 | pub(crate) trait Encode { |
| 2 | fn encode(&self, e: &mut Vec<u8>); |
| 3 | } |
| 4 | |
| 5 | impl<T: Encode + ?Sized> Encode for &'_ T { |
| 6 | fn encode(&self, e: &mut Vec<u8>) { |
| 7 | T::encode(self, e) |
| 8 | } |
| 9 | } |
| 10 | |
| 11 | impl<T: Encode + ?Sized> Encode for Box<T> { |
| 12 | fn encode(&self, e: &mut Vec<u8>) { |
| 13 | T::encode(self, e) |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | impl<T: Encode> Encode for [T] { |
| 18 | fn encode(&self, e: &mut Vec<u8>) { |
| 19 | self.len().encode(e); |
| 20 | for item: &T in self { |
| 21 | item.encode(e); |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | impl<T: Encode> Encode for Vec<T> { |
| 27 | fn encode(&self, e: &mut Vec<u8>) { |
| 28 | <[T]>::encode(self, e) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | impl Encode for str { |
| 33 | fn encode(&self, e: &mut Vec<u8>) { |
| 34 | self.len().encode(e); |
| 35 | e.extend_from_slice(self.as_bytes()); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl Encode for usize { |
| 40 | fn encode(&self, e: &mut Vec<u8>) { |
| 41 | assert!(*self <= u32::max_value() as usize); |
| 42 | (*self as u32).encode(e) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl Encode for u8 { |
| 47 | fn encode(&self, e: &mut Vec<u8>) { |
| 48 | e.push(*self); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | impl Encode for u32 { |
| 53 | fn encode(&self, e: &mut Vec<u8>) { |
| 54 | leb128::write::unsigned(w:e, (*self).into()).unwrap(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | impl Encode for i32 { |
| 59 | fn encode(&self, e: &mut Vec<u8>) { |
| 60 | leb128::write::signed(w:e, (*self).into()).unwrap(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | impl Encode for u64 { |
| 65 | fn encode(&self, e: &mut Vec<u8>) { |
| 66 | leb128::write::unsigned(w:e, *self).unwrap(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | impl Encode for i64 { |
| 71 | fn encode(&self, e: &mut Vec<u8>) { |
| 72 | leb128::write::signed(w:e, *self).unwrap(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | impl<T: Encode, U: Encode> Encode for (T, U) { |
| 77 | fn encode(&self, e: &mut Vec<u8>) { |
| 78 | self.0.encode(e); |
| 79 | self.1.encode(e); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl<T: Encode, U: Encode, V: Encode> Encode for (T, U, V) { |
| 84 | fn encode(&self, e: &mut Vec<u8>) { |
| 85 | self.0.encode(e); |
| 86 | self.1.encode(e); |
| 87 | self.2.encode(e); |
| 88 | } |
| 89 | } |
| 90 | |