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    /// Unpacks the single value back into two `bool` values.
15    ///
16    /// # Example
17    ///
18    /// ```rust
19    /// use num_packer::BoolPacker;
20    /// let packed = u8::pack_bool(true, false);
21    /// let (first, second) = packed.unpack_bool();
22    /// assert_eq!((first, second), (true, false));
23    /// ```
24    fn unpack_bool(&self) -> (bool, bool);
25}
26
27/// Trait for packing and unpacking two `u8` values into a single value.
28pub trait U8Packer {
29    /// Packs two `u8` values into a single value of the implementing type.
30    ///
31    /// # Example
32    ///
33    /// ```rust
34    /// use num_packer::U8Packer;
35    /// let packed = u16::pack_u8(200, 55);
36    /// assert_eq!(packed, (200 << 8) + 55);
37    /// ```
38    fn pack_u8(first: u8, second: u8) -> Self;
39
40    /// Unpacks the single value back into two `u8` values.
41    ///
42    /// # Example
43    ///
44    /// ```rust
45    /// use num_packer::U8Packer;
46    /// let packed = u16::pack_u8(200, 55);
47    /// let (first, second) = packed.unpack_u8();
48    /// assert_eq!((first, second), (200, 55));
49    /// ```
50    fn unpack_u8(&self) -> (u8, u8);
51}
52
53/// Trait for packing and unpacking two `u16` values into a single value.
54pub trait U16Packer {
55    /// Packs two `u16` values into a single value of the implementing type.
56    ///
57    /// # Example
58    ///
59    /// ```rust
60    /// use num_packer::U16Packer;
61    /// let packed = u32::pack_u16(50000, 40000);
62    /// assert_eq!(packed, (50000 << 16) + 40000);
63    /// ```
64    fn pack_u16(first: u16, second: u16) -> Self;
65
66    /// Unpacks the single value back into two `u16` values.
67    ///
68    /// # Example
69    ///
70    /// ```rust
71    /// use num_packer::U16Packer;
72    /// let packed = u32::pack_u16(50000, 40000);
73    /// let (first, second) = packed.unpack_u16();
74    /// assert_eq!((first, second), (50000, 40000));
75    /// ```
76    fn unpack_u16(&self) -> (u16, u16);
77}
78
79/// Trait for packing and unpacking two `u32` values into a single value.
80pub trait U32Packer {
81    /// Packs two `u32` values into a single value of the implementing type.
82    ///
83    /// # Example
84    ///
85    /// ```rust
86    /// use num_packer::U32Packer;
87    /// let packed = u64::pack_u32(200, 55);
88    /// assert_eq!(packed, (200u64 << 32) + 55);
89    /// ```
90    fn pack_u32(first: u32, second: u32) -> Self;
91
92    /// Unpacks the single value back into two `u32` values.
93    ///
94    /// # Example
95    ///
96    /// ```rust
97    /// use num_packer::U32Packer;
98    /// let packed = u64::pack_u32(200, 55);
99    /// let (first, second) = packed.unpack_u32();
100    /// assert_eq!((first, second), (200, 55));
101    /// ```
102    fn unpack_u32(&self) -> (u32, u32);
103}
104
105/// Trait for packing and unpacking two `i8` values into a single value.
106pub trait I8Packer {
107    /// Packs two `i8` values into a single value of the implementing type.
108    ///
109    /// # Example
110    ///
111    /// ```rust
112    /// use num_packer::I8Packer;
113    /// let packed = u16::pack_i8(-100, 55);
114    /// assert_eq!(packed, ((-100i8 as u8 as u16) << 8) + (55u8 as u16));
115    /// ```
116    fn pack_i8(first: i8, second: i8) -> Self;
117
118    /// Unpacks the single value back into two `i8` values.
119    ///
120    /// # Example
121    ///
122    /// ```rust
123    /// use num_packer::I8Packer;
124    /// let packed = u16::pack_i8(-100, 55);
125    /// let (first, second) = packed.unpack_i8();
126    /// assert_eq!((first, second), (-100, 55));
127    /// ```
128    fn unpack_i8(&self) -> (i8, i8);
129}
130
131/// Trait for packing and unpacking two `i16` values into a single value.
132pub trait I16Packer {
133    /// Packs two `i16` values into a single value of the implementing type.
134    ///
135    /// # Example
136    ///
137    /// ```rust
138    /// use num_packer::I16Packer;
139    /// let packed = u32::pack_i16(-20000, 15000);
140    /// assert_eq!(packed, ((-20000i16 as u16 as u32) << 16) + (15000u16 as u32));
141    /// ```
142    fn pack_i16(first: i16, second: i16) -> Self;
143
144    /// Unpacks the single value back into two `i16` values.
145    ///
146    /// # Example
147    ///
148    /// ```rust
149    /// use num_packer::I16Packer;
150    /// let packed = u32::pack_i16(-20000, 15000);
151    /// let (first, second) = packed.unpack_i16();
152    /// assert_eq!((first, second), (-20000, 15000));
153    /// ```
154    fn unpack_i16(&self) -> (i16, i16);
155}
156
157/// Trait for packing and unpacking two `i32` values into a single value.
158pub trait I32Packer {
159    /// Packs two `i32` values into a single value of the implementing type.
160    ///
161    /// # Example
162    ///
163    /// ```rust
164    /// use num_packer::I32Packer;
165    /// let packed = u64::pack_i32(-200, 15);
166    /// assert_eq!(packed, ((-200i32 as u32 as u64) << 32) + (15u32 as u64));
167    /// ```
168    fn pack_i32(first: i32, second: i32) -> Self;
169
170    /// Unpacks the single value back into two `i32` values.
171    ///
172    /// # Example
173    ///
174    /// ```rust
175    /// use num_packer::I32Packer;
176    /// let packed = u64::pack_i32(-200, 15);
177    /// let (first, second) = packed.unpack_i32();
178    /// assert_eq!((first, second), (-200, 15));
179    /// ```
180    fn unpack_i32(&self) -> (i32, i32);
181}
182
183/// Trait for packing and unpacking two `f32` values into a single value.
184pub trait F32Packer {
185    /// Packs two `f32` values into a single value of the implementing type.
186    ///
187    /// # Example
188    ///
189    /// ```rust
190    /// use num_packer::F32Packer;
191    /// let packed = u64::pack_f32(1.5, -2.5);
192    /// ```
193    fn pack_f32(first: f32, second: f32) -> Self;
194
195    /// Unpacks the single value back into two `f32` values.
196    ///
197    /// # Example
198    ///
199    /// ```rust
200    /// use num_packer::F32Packer;
201    /// let packed = u64::pack_f32(1.5, -2.5);
202    /// let (first, second) = packed.unpack_f32();
203    /// assert_eq!((first, second), (1.5, -2.5));
204    /// ```
205    fn unpack_f32(&self) -> (f32, f32);
206}