1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//! Functions for calculating field offsets.
use crate;
/// Calculates the offset of a field in bytes,given the previous field.
///
/// # Parameters
///
/// `Struct` is the struct that contains the field that this calculates the offset for.
///
/// `Prev` is the type of the previous field.
///
/// `Next` is the type of the field that this calculates the offset for.
///
/// `previous_offset` is the offset in bytes of the previous field,of `Prev` type.
///
/// # Example
///
/// ```
/// use repr_offset::offset_calc::next_field_offset;
///
/// #[repr(C, packed)]
/// struct Foo(u8, u16, u32, u64);
///
/// assert_eq!( OFFSET_1, 1 );
/// assert_eq!( OFFSET_2, 3 );
/// assert_eq!( OFFSET_3, 7 );
///
/// const OFFSET_0: usize = 0;
/// const OFFSET_1: usize = next_field_offset::<Foo, u8, u16>(OFFSET_0);
/// const OFFSET_2: usize = next_field_offset::<Foo, u16, u32>(OFFSET_1);
/// const OFFSET_3: usize = next_field_offset::<Foo, u32, u64>(OFFSET_2);
///
/// ```
pub const
/// Calculates the offset (in bytes) of a field, with the `call` method.
///
/// # Example
///
/// ```
/// use repr_offset::offset_calc::GetNextFieldOffset;
///
/// use std::mem;
///
/// #[repr(C, packed)]
/// struct Foo(u8, u16, u32, u64);
///
/// assert_eq!( OFFSET_1, 1 );
/// assert_eq!( OFFSET_2, 3 );
/// assert_eq!( OFFSET_3, 7 );
///
/// const OFFSET_0: usize = 0;
///
/// const OFFSET_1: usize = GetNextFieldOffset{
/// previous_offset: OFFSET_0,
/// previous_size: mem::size_of::<u8>(),
/// container_alignment: mem::align_of::<Foo>(),
/// next_alignment: mem::align_of::<u16>(),
/// }.call();
///
/// const OFFSET_2: usize = GetNextFieldOffset{
/// previous_offset: OFFSET_1,
/// previous_size: mem::size_of::<u16>(),
/// container_alignment: mem::align_of::<Foo>(),
/// next_alignment: mem::align_of::<u32>(),
/// }.call();
///
/// const OFFSET_3: usize = GetNextFieldOffset{
/// previous_offset: OFFSET_2,
/// previous_size: mem::size_of::<u32>(),
/// container_alignment: mem::align_of::<Foo>(),
/// next_alignment: mem::align_of::<u64>(),
/// }.call();
///
/// ```