from_bytes/
lib.rs

1use std::mem::size_of;
2use duplicate::duplicate;
3use std::io::Read;
4
5pub trait StructFromBytes<T: PackedSize = Self>: PackedSize {
6    /// Creates an instance of `T` by parsing a slice of bytes.
7    /// 
8    /// This method assumes that the byte slice contains unaligned data.
9    fn from_bytes(slice: &[u8], offset: usize) -> std::io::Result<Box<Self>>;
10    fn from_stream<R>(stream: &mut R) -> std::io::Result<Box<Self>> where R: Read;
11}
12
13
14pub trait PackedSize {
15    /// Returns the packed size of the struct. 
16    /// 
17    /// What is th packed size? Normally, struct members are aligned in a way that optimizes
18    /// access speed. For example, on a 64bit platform, all members are aligned to start
19    /// on a 64bit boundary [https://doc.rust-lang.org/reference/type-layout.html](https://doc.rust-lang.org/reference/type-layout.html).
20    /// 
21    /// *Packed* means what would be the size if no alignment would be used. In fact,
22    /// packed size equals the sum of sizes of all members.
23    fn packed_size() -> usize;
24}
25
26#[duplicate(
27    int_type;
28    [u8];
29    [u16];
30    [u32];
31    [u64];
32    [u128];
33    [i8];
34    [i16];
35    [i32];
36    [i64];
37    [i128];
38)]
39impl PackedSize for int_type { fn packed_size() -> usize { return size_of::<int_type>(); } }
40
41#[cfg(feature = "u8_arrays")]
42#[duplicate(array_length; [1]; [2]; [3]; [4]; [6]; [8]; [10]; [12]; [14]; [16]; )]
43impl PackedSize for [u8;array_length] { fn packed_size() -> usize { return array_length * size_of::<u8>(); } }
44
45#[cfg(feature = "u16_arrays")]
46#[duplicate(array_length; [1]; [2]; [3]; [4]; [6]; [8]; [10]; [12]; [14]; [16]; )]
47impl PackedSize for [u16;array_length] { fn packed_size() -> usize { return array_length * size_of::<u16>(); } }
48
49#[cfg(feature = "u32_arrays")]
50#[duplicate(array_length; [1]; [2]; [3]; [4]; [6]; [8]; [10]; [12]; [14]; [16]; )]
51impl PackedSize for [u32;array_length] { fn packed_size() -> usize { return array_length * size_of::<u32>(); } }
52
53#[cfg(feature = "u64_arrays")]
54#[duplicate(array_length; [1]; [2]; [3]; [4]; [6]; [8]; [10]; [12]; [14]; [16]; )]
55impl PackedSize for [u64;array_length] { fn packed_size() -> usize { return array_length * size_of::<u64>(); } }
56
57#[cfg(feature = "i8_arrays")]
58#[duplicate(array_length; [1]; [2]; [3]; [4]; [6]; [8]; [10]; [12]; [14]; [16]; )]
59impl PackedSize for [i8;array_length] { fn packed_size() -> usize { return array_length * size_of::<i8>(); } }
60
61#[cfg(feature = "i16_arrays")]
62#[duplicate(array_length; [1]; [2]; [3]; [4]; [6]; [8]; [10]; [12]; [14]; [16]; )]
63impl PackedSize for [i16;array_length] { fn packed_size() -> usize { return array_length * size_of::<i16>(); } }
64
65#[cfg(feature = "i32_arrays")]
66#[duplicate(array_length; [1]; [2]; [3]; [4]; [6]; [8]; [10]; [12]; [14]; [16]; )]
67impl PackedSize for [i32;array_length] { fn packed_size() -> usize { return array_length * size_of::<i32>(); } }
68
69#[cfg(feature = "i64_arrays")]
70#[duplicate(array_length; [1]; [2]; [3]; [4]; [6]; [8]; [10]; [12]; [14]; [16]; )]
71impl PackedSize for [i64;array_length] { fn packed_size() -> usize { return array_length * size_of::<i64>(); } }