bytesbox/primitives/mod.rs
1/// A trait to convert primitive types into byte representations (`Vec<u8>`).
2///
3/// This trait is implemented for common primitive types such as `u8`, `u16`, `i32`, `f32`, etc.
4/// It allows these types to be converted into a byte format for storage in `ByteBox`.
5///
6/// # Examples
7///
8/// ```rust
9/// use bytesbox::primitives::BytesPrimitives;
10///
11/// let num: u32 = 42;
12/// let bytes = num.to_bytes();
13/// assert_eq!(bytes, b"42");
14/// ```
15pub trait BytesPrimitives {
16 /// Converts the primitive into a `Vec<u8>` representing its byte form.
17 ///
18 /// The default implementation converts the primitive to its string representation and then to bytes.
19 ///
20 /// # Examples
21 ///
22 /// ```rust
23 /// use bytesbox::primitives::BytesPrimitives;
24 ///
25 /// let num: u16 = 256;
26 /// let bytes = num.to_bytes();
27 /// assert_eq!(bytes, b"256");
28 /// ```
29 fn to_bytes(&self) -> Vec<u8>;
30}
31
32// Implement the `BytesPrimitives` trait for various primitive types.
33
34impl BytesPrimitives for u8 {
35 /// Converts a `u8` into its byte representation.
36 ///
37 /// # Examples
38 ///
39 /// ```rust
40 /// use bytesbox::primitives::BytesPrimitives;
41 ///
42 /// let num: u8 = 255;
43 /// let bytes = num.to_bytes();
44 /// assert_eq!(bytes, b"255");
45 /// ```
46 fn to_bytes(&self) -> Vec<u8> {
47 format!("{}", &self).into_bytes()
48 }
49}
50
51impl BytesPrimitives for u16 {
52 /// Converts a `u16` into its byte representation.
53 ///
54 /// # Examples
55 ///
56 /// ```rust
57 /// use bytesbox::primitives::BytesPrimitives;
58 ///
59 /// let num: u16 = 65535;
60 /// let bytes = num.to_bytes();
61 /// assert_eq!(bytes, b"65535");
62 /// ```
63 fn to_bytes(&self) -> Vec<u8> {
64 format!("{}", &self).into_bytes()
65 }
66}
67
68impl BytesPrimitives for u32 {
69 /// Converts a `u32` into its byte representation.
70 ///
71 /// # Examples
72 ///
73 /// ```rust
74 /// use bytesbox::primitives::BytesPrimitives;
75 ///
76 /// let num: u32 = 4294967295;
77 /// let bytes = num.to_bytes();
78 /// assert_eq!(bytes, b"4294967295");
79 /// ```
80 fn to_bytes(&self) -> Vec<u8> {
81 format!("{}", &self).into_bytes()
82 }
83}
84
85impl BytesPrimitives for u64 {
86 /// Converts a `u64` into its byte representation.
87 ///
88 /// # Examples
89 ///
90 /// ```rust
91 /// use bytesbox::primitives::BytesPrimitives;
92 ///
93 /// let num: u64 = 18446744073709551615;
94 /// let bytes = num.to_bytes();
95 /// assert_eq!(bytes, b"18446744073709551615");
96 /// ```
97 fn to_bytes(&self) -> Vec<u8> {
98 format!("{}", &self).into_bytes()
99 }
100}
101
102impl BytesPrimitives for i8 {
103 /// Converts an `i8` into its byte representation.
104 ///
105 /// # Examples
106 ///
107 /// ```rust
108 /// use bytesbox::primitives::BytesPrimitives;
109 ///
110 /// let num: i8 = -128;
111 /// let bytes = num.to_bytes();
112 /// assert_eq!(bytes, b"-128");
113 /// ```
114 fn to_bytes(&self) -> Vec<u8> {
115 format!("{}", &self).into_bytes()
116 }
117}
118
119impl BytesPrimitives for i16 {
120 /// Converts an `i16` into its byte representation.
121 ///
122 /// # Examples
123 ///
124 /// ```rust
125 /// use bytesbox::primitives::BytesPrimitives;
126 ///
127 /// let num: i16 = -32768;
128 /// let bytes = num.to_bytes();
129 /// assert_eq!(bytes, b"-32768");
130 /// ```
131 fn to_bytes(&self) -> Vec<u8> {
132 format!("{}", &self).into_bytes()
133 }
134}
135
136impl BytesPrimitives for i32 {
137 /// Converts an `i32` into its byte representation.
138 ///
139 /// # Examples
140 ///
141 /// ```rust
142 /// use bytesbox::primitives::BytesPrimitives;
143 ///
144 /// let num: i32 = -2147483648;
145 /// let bytes = num.to_bytes();
146 /// assert_eq!(bytes, b"-2147483648");
147 /// ```
148 fn to_bytes(&self) -> Vec<u8> {
149 format!("{}", &self).into_bytes()
150 }
151}
152
153impl BytesPrimitives for i64 {
154 /// Converts an `i64` into its byte representation.
155 ///
156 /// # Examples
157 ///
158 /// ```rust
159 /// use bytesbox::primitives::BytesPrimitives;
160 ///
161 /// let num: i64 = -9223372036854775808;
162 /// let bytes = num.to_bytes();
163 /// assert_eq!(bytes, b"-9223372036854775808");
164 /// ```
165 fn to_bytes(&self) -> Vec<u8> {
166 format!("{}", &self).into_bytes()
167 }
168}
169
170impl BytesPrimitives for f32 {
171 /// Converts an `f32` into its byte representation.
172 ///
173 /// # Examples
174 ///
175 /// ```rust
176 /// use bytesbox::primitives::BytesPrimitives;
177 ///
178 /// let num: f32 = 3.14;
179 /// let bytes = num.to_bytes();
180 /// assert_eq!(bytes, b"3.14");
181 /// ```
182 fn to_bytes(&self) -> Vec<u8> {
183 format!("{}", &self).into_bytes()
184 }
185}
186
187impl BytesPrimitives for f64 {
188 /// Converts an `f64` into its byte representation.
189 ///
190 /// # Examples
191 ///
192 /// ```rust
193 /// use bytesbox::primitives::BytesPrimitives;
194 ///
195 /// let num: f64 = 2.718281828459045;
196 /// let bytes = num.to_bytes();
197 /// assert_eq!(bytes, b"2.718281828459045");
198 /// ```
199 fn to_bytes(&self) -> Vec<u8> {
200 format!("{}", &self).into_bytes()
201 }
202}
203
204impl BytesPrimitives for usize {
205 /// Converts a `usize` into its byte representation.
206 ///
207 /// # Examples
208 ///
209 /// ```rust
210 /// use bytesbox::primitives::BytesPrimitives;
211 ///
212 /// let num: usize = 1024;
213 /// let bytes = num.to_bytes();
214 /// assert_eq!(bytes, b"1024");
215 /// ```
216 fn to_bytes(&self) -> Vec<u8> {
217 format!("{}", &self).into_bytes()
218 }
219}
220
221impl BytesPrimitives for isize {
222 /// Converts an `isize` into its byte representation.
223 ///
224 /// # Examples
225 ///
226 /// ```rust
227 /// use bytesbox::primitives::BytesPrimitives;
228 ///
229 /// let num: isize = -1024;
230 /// let bytes = num.to_bytes();
231 /// assert_eq!(bytes, b"-1024");
232 /// ```
233 fn to_bytes(&self) -> Vec<u8> {
234 format!("{}", &self).into_bytes()
235 }
236}