|
1 | 1 | pub trait ToBytes { |
2 | 2 | fn to_bytes(&self) -> Vec<u8>; |
| 3 | + |
| 4 | + const TYPE_ID: &'static [u8]; |
3 | 5 | } |
4 | 6 |
|
5 | 7 | // Integer types (Little Endian) |
6 | 8 | impl ToBytes for u8 { |
7 | 9 | fn to_bytes(&self) -> Vec<u8> { |
8 | 10 | vec![*self] |
9 | 11 | } |
| 12 | + |
| 13 | + const TYPE_ID: &'static [u8] = b"u8"; |
10 | 14 | } |
11 | 15 |
|
12 | 16 | impl ToBytes for u16 { |
13 | 17 | fn to_bytes(&self) -> Vec<u8> { |
14 | 18 | self.to_le_bytes().to_vec() |
15 | 19 | } |
| 20 | + |
| 21 | + const TYPE_ID: &'static [u8] = b"u16"; |
16 | 22 | } |
17 | 23 |
|
18 | 24 | impl ToBytes for u32 { |
19 | 25 | fn to_bytes(&self) -> Vec<u8> { |
20 | 26 | self.to_le_bytes().to_vec() |
21 | 27 | } |
| 28 | + |
| 29 | + const TYPE_ID: &'static [u8] = b"u32"; |
22 | 30 | } |
23 | 31 |
|
24 | 32 | impl ToBytes for u64 { |
25 | 33 | fn to_bytes(&self) -> Vec<u8> { |
26 | 34 | self.to_le_bytes().to_vec() |
27 | 35 | } |
| 36 | + |
| 37 | + const TYPE_ID: &'static [u8] = b"u64"; |
28 | 38 | } |
29 | 39 |
|
30 | 40 | impl ToBytes for u128 { |
31 | 41 | fn to_bytes(&self) -> Vec<u8> { |
32 | 42 | self.to_le_bytes().to_vec() |
33 | 43 | } |
| 44 | + |
| 45 | + const TYPE_ID: &'static [u8] = b"u128"; |
34 | 46 | } |
35 | 47 |
|
36 | 48 | impl ToBytes for i8 { |
37 | 49 | fn to_bytes(&self) -> Vec<u8> { |
38 | 50 | vec![*self as u8] |
39 | 51 | } |
| 52 | + |
| 53 | + const TYPE_ID: &'static [u8] = b"i8"; |
| 54 | + |
40 | 55 | } |
41 | 56 |
|
42 | 57 | impl ToBytes for i16 { |
43 | 58 | fn to_bytes(&self) -> Vec<u8> { |
44 | 59 | self.to_le_bytes().to_vec() |
45 | 60 | } |
| 61 | + |
| 62 | + const TYPE_ID: &'static [u8] = b"i16"; |
46 | 63 | } |
47 | 64 |
|
48 | 65 | impl ToBytes for i32 { |
49 | 66 | fn to_bytes(&self) -> Vec<u8> { |
50 | 67 | self.to_le_bytes().to_vec() |
51 | 68 | } |
| 69 | + |
| 70 | + const TYPE_ID: &'static [u8] = b"i32"; |
52 | 71 | } |
53 | 72 |
|
54 | 73 | impl ToBytes for i64 { |
55 | 74 | fn to_bytes(&self) -> Vec<u8> { |
56 | 75 | self.to_le_bytes().to_vec() |
57 | 76 | } |
| 77 | + |
| 78 | + const TYPE_ID: &'static [u8] = b"i64"; |
58 | 79 | } |
59 | 80 |
|
60 | 81 | impl ToBytes for i128 { |
61 | 82 | fn to_bytes(&self) -> Vec<u8> { |
62 | 83 | self.to_le_bytes().to_vec() |
63 | 84 | } |
| 85 | + |
| 86 | + const TYPE_ID: &'static [u8] = b"i128"; |
64 | 87 | } |
65 | 88 |
|
66 | 89 | // Floating point types (Native Endian) |
67 | 90 | impl ToBytes for f32 { |
68 | 91 | fn to_bytes(&self) -> Vec<u8> { |
69 | 92 | self.to_ne_bytes().to_vec() |
70 | 93 | } |
| 94 | + |
| 95 | + const TYPE_ID: &'static [u8] = b"f32"; |
71 | 96 | } |
72 | 97 |
|
73 | 98 | impl ToBytes for f64 { |
74 | 99 | fn to_bytes(&self) -> Vec<u8> { |
75 | 100 | self.to_ne_bytes().to_vec() |
76 | 101 | } |
| 102 | + |
| 103 | + const TYPE_ID: &'static [u8] = b"f64"; |
77 | 104 | } |
78 | 105 |
|
79 | 106 |
|
80 | 107 | impl ToBytes for &str { |
81 | 108 | fn to_bytes(&self) -> Vec<u8> { |
82 | 109 | self.as_bytes().to_vec() |
83 | 110 | } |
| 111 | + |
| 112 | + const TYPE_ID: &'static [u8] = b"&str"; |
84 | 113 | } |
85 | 114 |
|
86 | 115 | impl ToBytes for String { |
87 | 116 | fn to_bytes(&self) -> Vec<u8> { |
88 | 117 | self.as_bytes().to_vec() |
89 | 118 | } |
| 119 | + |
| 120 | + const TYPE_ID: &'static [u8] = b"String"; |
90 | 121 | } |
0 commit comments