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