num_packer/
impls.rs

1use crate::{
2    BoolPacker, F32Packer, I8Packer, I16Packer, I32Packer, U8Packer, U16Packer, U32Packer,
3};
4
5macro_rules! impl_bool_packer_for_num {
6    ($t:ty) => {
7        impl BoolPacker for $t {
8            fn pack_bool(first: bool, second: bool) -> Self {
9                ((first as $t) << 1) | (second as $t)
10            }
11
12            fn unpack_bool(&self) -> (bool, bool) {
13                ((self & 0b10) != 0, (self & 0b01) != 0)
14            }
15        }
16    };
17}
18
19impl_bool_packer_for_num!(u8);
20impl_bool_packer_for_num!(u16);
21impl_bool_packer_for_num!(u32);
22impl_bool_packer_for_num!(u64);
23impl_bool_packer_for_num!(usize);
24impl_bool_packer_for_num!(i8);
25impl_bool_packer_for_num!(i16);
26impl_bool_packer_for_num!(i32);
27impl_bool_packer_for_num!(i64);
28impl_bool_packer_for_num!(isize);
29
30// impl U8Packer
31macro_rules! impl_u8_packer_for_num {
32    ($t:ty) => {
33        impl U8Packer for $t {
34            fn pack_u8(first: u8, second: u8) -> Self {
35                ((first as $t) << 8) | (second as $t)
36            }
37
38            fn unpack_u8(&self) -> (u8, u8) {
39                (((self >> 8) & 0xFF) as u8, (self & 0xFF) as u8)
40            }
41        }
42    };
43}
44
45impl_u8_packer_for_num!(u16);
46impl_u8_packer_for_num!(u32);
47impl_u8_packer_for_num!(u64);
48impl_u8_packer_for_num!(usize);
49impl_u8_packer_for_num!(i16);
50impl_u8_packer_for_num!(i32);
51impl_u8_packer_for_num!(i64);
52impl_u8_packer_for_num!(isize);
53
54// impl U16Packer
55macro_rules! impl_u16_packer_for_num {
56    ($t:ty) => {
57        impl U16Packer for $t {
58            fn pack_u16(first: u16, second: u16) -> Self {
59                ((first as $t) << 16) | (second as $t)
60            }
61
62            fn unpack_u16(&self) -> (u16, u16) {
63                (((self >> 16) & 0xFFFF) as u16, (self & 0xFFFF) as u16)
64            }
65        }
66    };
67}
68
69impl_u16_packer_for_num!(u32);
70impl_u16_packer_for_num!(u64);
71#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
72impl_u16_packer_for_num!(usize);
73impl_u16_packer_for_num!(i32);
74impl_u16_packer_for_num!(i64);
75#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
76impl_u16_packer_for_num!(isize);
77
78// impl U32Packer
79macro_rules! impl_u32_packer_for_num {
80    ($t:ty) => {
81        impl U32Packer for $t {
82            fn pack_u32(first: u32, second: u32) -> Self {
83                ((first as $t) << 32) | (second as $t)
84            }
85
86            fn unpack_u32(&self) -> (u32, u32) {
87                (
88                    ((self >> 32) & 0xFFFFFFFF) as u32,
89                    (self & 0xFFFFFFFF) as u32,
90                )
91            }
92        }
93    };
94}
95
96impl_u32_packer_for_num!(u64);
97#[cfg(target_pointer_width = "64")]
98impl_u32_packer_for_num!(usize);
99impl_u32_packer_for_num!(i64);
100#[cfg(target_pointer_width = "64")]
101impl_u32_packer_for_num!(isize);
102
103macro_rules! impl_i8_packer_for_num {
104    ($t:ty) => {
105        impl I8Packer for $t {
106            fn pack_i8(first: i8, second: i8) -> Self {
107                let first_u = first as u8 as $t;
108                let second_u = second as u8 as $t;
109                (first_u << 8) | second_u
110            }
111
112            fn unpack_i8(&self) -> (i8, i8) {
113                let first = ((self >> 8) & 0xFF) as u8 as i8;
114                let second = (self & 0xFF) as u8 as i8;
115                (first, second)
116            }
117        }
118    };
119}
120
121impl_i8_packer_for_num!(i16);
122impl_i8_packer_for_num!(i32);
123impl_i8_packer_for_num!(i64);
124impl_i8_packer_for_num!(isize);
125impl_i8_packer_for_num!(u16);
126impl_i8_packer_for_num!(u32);
127impl_i8_packer_for_num!(u64);
128impl_i8_packer_for_num!(usize);
129
130macro_rules! impl_i16_packer_for_num {
131    ($t:ty) => {
132        impl I16Packer for $t {
133            fn pack_i16(first: i16, second: i16) -> Self {
134                let first_u = first as u16 as $t;
135                let second_u = second as u16 as $t;
136                (first_u << 16) | second_u
137            }
138
139            fn unpack_i16(&self) -> (i16, i16) {
140                let first = ((self >> 16) & 0xFFFF) as u16 as i16;
141                let second = (self & 0xFFFF) as u16 as i16;
142                (first, second)
143            }
144        }
145    };
146}
147
148impl_i16_packer_for_num!(u32);
149impl_i16_packer_for_num!(u64);
150#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
151impl_i16_packer_for_num!(usize);
152impl_i16_packer_for_num!(i32);
153impl_i16_packer_for_num!(i64);
154#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
155impl_i16_packer_for_num!(isize);
156
157macro_rules! impl_i32_packer_for_num {
158    ($t:ty) => {
159        impl I32Packer for $t {
160            fn pack_i32(first: i32, second: i32) -> Self {
161                let first_u = first as u32 as $t;
162                let second_u = second as u32 as $t;
163                (first_u << 32) | second_u
164            }
165
166            fn unpack_i32(&self) -> (i32, i32) {
167                let first = ((self >> 32) & 0xFFFFFFFF) as u32 as i32;
168                let second = (self & 0xFFFFFFFF) as u32 as i32;
169                (first, second)
170            }
171        }
172    };
173}
174
175impl_i32_packer_for_num!(u64);
176#[cfg(target_pointer_width = "64")]
177impl_i32_packer_for_num!(usize);
178impl_i32_packer_for_num!(i64);
179#[cfg(target_pointer_width = "64")]
180impl_i32_packer_for_num!(isize);
181
182macro_rules! impl_f32_packer_for_num {
183    ($t:ty) => {
184        impl F32Packer for $t {
185            fn pack_f32(first: f32, second: f32) -> Self {
186                let first_u = first.to_bits() as $t;
187                let second_u = second.to_bits() as $t;
188                (first_u << 32) | second_u
189            }
190
191            fn unpack_f32(&self) -> (f32, f32) {
192                let first = ((self >> 32) & 0xFFFFFFFF) as u32;
193                let second = (self & 0xFFFFFFFF) as u32;
194                (f32::from_bits(first), f32::from_bits(second))
195            }
196        }
197    };
198}
199
200impl_f32_packer_for_num!(u64);
201#[cfg(target_pointer_width = "64")]
202impl_f32_packer_for_num!(usize);
203impl_f32_packer_for_num!(i64);
204#[cfg(target_pointer_width = "64")]
205impl_f32_packer_for_num!(isize);