Skip to main content

libmpegts/utils/
bits.rs

1#[macro_export]
2macro_rules! pack_bits {
3    ($type:ty, $($name:ident : $size:expr => $val:expr),* $(,)?) => {{
4        const SIZE: usize = core::mem::size_of::<$type>();
5        let mut _pos: u32 = SIZE as u32 * 8;
6        let mut _result: $type = 0;
7        $(
8            let _ = stringify!($name);
9            _pos -= $size;
10            _result |= (($val as $type) & ((1 << $size) - 1)) << _pos;
11        )*
12        _result.to_be_bytes()
13    }};
14}
15
16#[macro_export]
17macro_rules! unpack_bits {
18    ($type:ty, $data:expr, $($name:ident : $size:expr),* $(,)?) => {{
19        const SIZE: usize = core::mem::size_of::<$type>();
20        let mut _pos: u32 = SIZE as u32 * 8;
21        let _val = <$type>::from_be_bytes($data);
22        $(
23            _pos -= $size;
24            let $name = (_val >> _pos) & ((1 << $size) - 1);
25        )*
26        ($($name),*)
27    }};
28}