userspace/macros/tuples/
default.rs

1#[macro_export]
2macro_rules! tuple {
3    ($tuple_visualization:vis $tuple_identifier:ident, $($ordinal_type:ty),*) => {
4        $tuple_visualization struct $tuple_identifier($($ordinal_type),*);
5
6
7
8        // impl macros::traits::ByteXElfSize for $tuple_identifier {
9        //     const SIZE: usize = $(<$ordinal_type>::BYTES_SIZE + )* 0;
10        // }
11
12
13        impl crate::traits::Bytes<crate::Origin,crate::Origin> for $tuple_identifier {
14
15            fn to_bytes(&self, endianness: bool) -> [u8; $tuple_identifier::BYTES_SIZE] {
16                let mut b = [0u8; $tuple_identifier::BYTES_SIZE];
17                let mut o = 0;
18                $(
19                    b[o..(o+<$ordinal_type>::BYTES_SIZE)].copy_from_slice(
20                        &if endianness {
21                            self.$ordinal_type.to_le_bytes()
22                        } else {
23                            self.$ordinal_type.to_be_bytes()
24                        }
25                    );
26                    o = o + <$ordinal_type>::BYTES_SIZE;
27                )*
28                b
29            }
30
31            fn from_bytes(bytes : [u8; $tuple_identifier::BYTES_SIZE], endianness: bool) -> $tuple_identifier {
32                let mut o = 0;
33                $tuple_identifier (
34                    $(
35                        {
36                            let mut field_bytes = [0u8; <$ordinal_type>::BYTES_SIZE];
37                            field_bytes.copy_from_slice(&bytes[o..(o+<$ordinal_type>::BYTES_SIZE)]);
38                            let ordinal = if endianness {
39                                <$ordinal_type>::from_le_bytes(field_bytes)
40                            } else {
41                                <$ordinal_type>::from_be_bytes(field_bytes)
42                            };
43                            o = o + <$ordinal_type>::BYTES_SIZE;
44                            ordinal
45                        }
46                    ),*
47                )
48            }
49        }
50    };
51}
52pub use tuple;