num_packer/
packer.rs

1/// Trait for packing and unpacking two `bool` values into a single value.
2pub trait BoolPacker {
3    /// Packs two `bool` values into a single value of the implementing type.
4    ///
5    /// # Example
6    ///
7    /// ```rust
8    /// use num_packer::BoolPacker;
9    /// let packed = u8::pack_bool(true, false);
10    /// assert_eq!(packed, 0b10);
11    /// ```
12    fn pack_bool(first: bool, second: bool) -> Self;
13
14    /// Gets the first `bool` value from the packed representation.
15    ///
16    /// # Example
17    ///
18    /// ```rust
19    /// use num_packer::BoolPacker;
20    /// let packed = u8::pack_bool(true, false);
21    /// assert_eq!(packed.first_bool(), true);
22    /// ```
23    fn first_bool(&self) -> bool;
24
25    /// Gets the second `bool` value from the packed representation.
26    ///
27    /// # Example
28    ///
29    /// ```rust
30    /// use num_packer::BoolPacker;
31    /// let packed = u8::pack_bool(true, false);
32    /// assert_eq!(packed.second_bool(), false);
33    /// ```
34    fn second_bool(&self) -> bool;
35
36    /// Unpacks the single value back into two `bool` values.
37    ///
38    /// # Example
39    ///
40    /// ```rust
41    /// use num_packer::BoolPacker;
42    /// let packed = u8::pack_bool(true, false);
43    /// let (first, second) = packed.unpack_bool();
44    /// assert_eq!((first, second), (true, false));
45    /// ```
46    fn unpack_bool(&self) -> (bool, bool) {
47        (self.first_bool(), self.second_bool())
48    }
49}
50
51/// Trait for packing and unpacking two `u8` values into a single value.
52pub trait U8Packer {
53    /// Packs two `u8` values into a single value of the implementing type.
54    ///
55    /// # Example
56    ///
57    /// ```rust
58    /// use num_packer::U8Packer;
59    /// let packed = u16::pack_u8(200, 55);
60    /// assert_eq!(packed, (200 << 8) + 55);
61    /// ```
62    fn pack_u8(first: u8, second: u8) -> Self;
63
64    /// Gets the first `u8` value from the packed representation.
65    ///
66    /// # Example
67    ///
68    /// ```rust
69    /// use num_packer::U8Packer;
70    /// let packed = u16::pack_u8(200, 55);
71    /// assert_eq!(packed.first_u8(), 200);
72    /// ```
73    fn first_u8(&self) -> u8;
74
75    /// Gets the second `u8` value from the packed representation.
76    ///
77    /// # Example
78    ///
79    /// ```rust
80    /// use num_packer::U8Packer;
81    /// let packed = u16::pack_u8(200, 55);
82    /// assert_eq!(packed.second_u8(), 55);
83    /// ```
84    fn second_u8(&self) -> u8;
85
86    /// Unpacks the single value back into two `u8` values.
87    ///
88    /// # Example
89    ///
90    /// ```rust
91    /// use num_packer::U8Packer;
92    /// let packed = u16::pack_u8(200, 55);
93    /// let (first, second) = packed.unpack_u8();
94    /// assert_eq!((first, second), (200, 55));
95    /// ```
96    fn unpack_u8(&self) -> (u8, u8) {
97        (self.first_u8(), self.second_u8())
98    }
99}
100
101/// Trait for packing and unpacking two `u16` values into a single value.
102pub trait U16Packer {
103    /// Packs two `u16` values into a single value of the implementing type.
104    ///
105    /// # Example
106    ///
107    /// ```rust
108    /// use num_packer::U16Packer;
109    /// let packed = u32::pack_u16(50000, 40000);
110    /// assert_eq!(packed, (50000 << 16) + 40000);
111    /// ```
112    fn pack_u16(first: u16, second: u16) -> Self;
113
114    /// Gets the first `u16` value from the packed representation.
115    ///
116    /// # Example
117    ///
118    /// ```rust
119    /// use num_packer::U16Packer;
120    /// let packed = u32::pack_u16(50000, 40000);
121    /// assert_eq!(packed.first_u16(), 50000);
122    /// ```
123    fn first_u16(&self) -> u16;
124
125    /// Gets the second `u16` value from the packed representation.
126    ///
127    /// # Example
128    ///
129    /// ```rust
130    /// use num_packer::U16Packer;
131    /// let packed = u32::pack_u16(50000, 40000);
132    /// assert_eq!(packed.second_u16(), 40000);
133    /// ```
134    fn second_u16(&self) -> u16;
135
136    /// Unpacks the single value back into two `u16` values.
137    ///
138    /// # Example
139    ///
140    /// ```rust
141    /// use num_packer::U16Packer;
142    /// let packed = u32::pack_u16(50000, 40000);
143    /// let (first, second) = packed.unpack_u16();
144    /// assert_eq!((first, second), (50000, 40000));
145    /// ```
146    fn unpack_u16(&self) -> (u16, u16) {
147        (self.first_u16(), self.second_u16())
148    }
149}
150
151/// Trait for packing and unpacking two `u32` values into a single value.
152pub trait U32Packer {
153    /// Packs two `u32` values into a single value of the implementing type.
154    ///
155    /// # Example
156    ///
157    /// ```rust
158    /// use num_packer::U32Packer;
159    /// let packed = u64::pack_u32(200, 55);
160    /// assert_eq!(packed, (200u64 << 32) + 55);
161    /// ```
162    fn pack_u32(first: u32, second: u32) -> Self;
163
164    /// Gets the first `u32` value from the packed representation.
165    ///
166    /// # Example
167    ///
168    /// ```rust
169    /// use num_packer::U32Packer;
170    /// let packed = u64::pack_u32(200, 55);
171    /// assert_eq!(packed.first_u32(), 200);
172    /// ```
173    fn first_u32(&self) -> u32;
174
175    /// Gets the second `u32` value from the packed representation.
176    ///
177    /// # Example
178    ///
179    /// ```rust
180    /// use num_packer::U32Packer;
181    /// let packed = u64::pack_u32(200, 55);
182    /// assert_eq!(packed.second_u32(), 55);
183    /// `
184    fn second_u32(&self) -> u32;
185
186    /// Unpacks the single value back into two `u32` values.
187    ///
188    /// # Example
189    ///
190    /// ```rust
191    /// use num_packer::U32Packer;
192    /// let packed = u64::pack_u32(200, 55);
193    /// let (first, second) = packed.unpack_u32();
194    /// assert_eq!((first, second), (200, 55));
195    /// ```
196    fn unpack_u32(&self) -> (u32, u32) {
197        (self.first_u32(), self.second_u32())
198    }
199}
200
201/// Trait for packing and unpacking two `i8` values into a single value.
202pub trait I8Packer {
203    /// Packs two `i8` values into a single value of the implementing type.
204    ///
205    /// # Example
206    ///
207    /// ```rust
208    /// use num_packer::I8Packer;
209    /// let packed = u16::pack_i8(-100, 55);
210    /// assert_eq!(packed, ((-100i8 as u8 as u16) << 8) + (55u8 as u16));
211    /// ```
212    fn pack_i8(first: i8, second: i8) -> Self;
213
214    /// Gets the first `i8` value from the packed representation.
215    ///
216    /// # Example
217    ///
218    /// ```rust
219    /// use num_packer::I8Packer;
220    /// let packed = u16::pack_i8(-100, 55);
221    /// assert_eq!(packed.first_i8(), -100);
222    /// ```
223    fn first_i8(&self) -> i8;
224
225    /// Gets the second `i8` value from the packed representation.
226    ///
227    /// # Example
228    ///
229    /// ```rust
230    /// use num_packer::I8Packer;
231    /// let packed = u16::pack_i8(-100, 55);
232    /// assert_eq!(packed.second_i8(), 55);
233    /// ```
234    fn second_i8(&self) -> i8;
235
236    /// Unpacks the single value back into two `i8` values.
237    ///
238    /// # Example
239    ///
240    /// ```rust
241    /// use num_packer::I8Packer;
242    /// let packed = u16::pack_i8(-100, 55);
243    /// let (first, second) = packed.unpack_i8();
244    /// assert_eq!((first, second), (-100, 55));
245    /// ```
246    fn unpack_i8(&self) -> (i8, i8) {
247        (self.first_i8(), self.second_i8())
248    }
249}
250
251/// Trait for packing and unpacking two `i16` values into a single value.
252pub trait I16Packer {
253    /// Packs two `i16` values into a single value of the implementing type.
254    ///
255    /// # Example
256    ///
257    /// ```rust
258    /// use num_packer::I16Packer;
259    /// let packed = u32::pack_i16(-20000, 15000);
260    /// assert_eq!(packed, ((-20000i16 as u16 as u32) << 16) + (15000u16 as u32));
261    /// ```
262    fn pack_i16(first: i16, second: i16) -> Self;
263
264    /// Gets the first `i16` value from the packed representation.
265    ///
266    /// # Example
267    ///
268    /// ```rust
269    /// use num_packer::I16Packer;
270    /// let packed = u32::pack_i16(-20000, 15000);
271    /// assert_eq!(packed.first_i16(), -20000);
272    /// ```
273    fn first_i16(&self) -> i16;
274
275    /// Gets the second `i16` value from the packed representation.
276    ///
277    /// # Example
278    ///
279    /// ```rust
280    /// use num_packer::I16Packer;
281    /// let packed = u32::pack_i16(-20000, 15000);
282    /// assert_eq!(packed.second_i16(), 15000);
283    /// ```
284    fn second_i16(&self) -> i16;
285
286    /// Unpacks the single value back into two `i16` values.
287    ///
288    /// # Example
289    ///
290    /// ```rust
291    /// use num_packer::I16Packer;
292    /// let packed = u32::pack_i16(-20000, 15000);
293    /// let (first, second) = packed.unpack_i16();
294    /// assert_eq!((first, second), (-20000, 15000));
295    /// ```
296    fn unpack_i16(&self) -> (i16, i16) {
297        (self.first_i16(), self.second_i16())
298    }
299}
300
301/// Trait for packing and unpacking two `i32` values into a single value.
302pub trait I32Packer {
303    /// Packs two `i32` values into a single value of the implementing type.
304    ///
305    /// # Example
306    ///
307    /// ```rust
308    /// use num_packer::I32Packer;
309    /// let packed = u64::pack_i32(-200, 15);
310    /// assert_eq!(packed, ((-200i32 as u32 as u64) << 32) + (15u32 as u64));
311    /// ```
312    fn pack_i32(first: i32, second: i32) -> Self;
313
314    /// Gets the first `i32` value from the packed representation.
315    ///
316    /// # Example
317    ///
318    /// ```rust
319    /// use num_packer::I32Packer;
320    /// let packed = u64::pack_i32(-200, 15);
321    /// assert_eq!(packed.first_i32(), -200);
322    /// ```
323    fn first_i32(&self) -> i32;
324
325    /// Gets the second `i32` value from the packed representation.
326    ///
327    /// # Example
328    ///
329    /// ```rust
330    /// use num_packer::I32Packer;
331    /// let packed = u64::pack_i32(-200, 15);
332    /// assert_eq!(packed.second_i32(), 15);
333    /// ```
334    fn second_i32(&self) -> i32;
335
336    /// Unpacks the single value back into two `i32` values.
337    ///
338    /// # Example
339    ///
340    /// ```rust
341    /// use num_packer::I32Packer;
342    /// let packed = u64::pack_i32(-200, 15);
343    /// let (first, second) = packed.unpack_i32();
344    /// assert_eq!((first, second), (-200, 15));
345    /// ```
346    fn unpack_i32(&self) -> (i32, i32) {
347        (self.first_i32(), self.second_i32())
348    }
349}
350
351/// Trait for packing and unpacking two `f32` values into a single value.
352pub trait F32Packer {
353    /// Packs two `f32` values into a single value of the implementing type.
354    ///
355    /// # Example
356    ///
357    /// ```rust
358    /// use num_packer::F32Packer;
359    /// let packed = u64::pack_f32(1.5, -2.5);
360    /// ```
361    fn pack_f32(first: f32, second: f32) -> Self;
362
363    /// Gets the first `f32` value from the packed representation.
364    ///
365    /// # Example
366    ///
367    /// ```rust
368    /// use num_packer::F32Packer;
369    /// let packed = u64::pack_f32(1.5, -2.5);
370    /// assert_eq!(packed.first_f32(), 1.5);
371    /// ```
372    fn first_f32(&self) -> f32;
373
374    /// Gets the second `f32` value from the packed representation.
375    ///
376    /// # Example
377    ///
378    /// ```rust
379    /// use num_packer::F32Packer;
380    /// let packed = u64::pack_f32(1.5, -2.5);
381    /// assert_eq!(packed.second_f32(), -2.5);
382    /// ```
383    fn second_f32(&self) -> f32;
384
385    /// Unpacks the single value back into two `f32` values.
386    ///
387    /// # Example
388    ///
389    /// ```rust
390    /// use num_packer::F32Packer;
391    /// let packed = u64::pack_f32(1.5, -2.5);
392    /// let (first, second) = packed.unpack_f32();
393    /// assert_eq!((first, second), (1.5, -2.5));
394    /// ```
395    fn unpack_f32(&self) -> (f32, f32) {
396        (self.first_f32(), self.second_f32())
397    }
398}