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
use crate::*;
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug, Hash, GcCompat)]
pub struct Align { raw: Int }
impl Align {
pub const ONE: Align = Align { raw: Int::ONE };
pub fn from_bytes(align: impl Into<Int>) -> Option<Align> {
let raw = align.into();
if raw.is_power_of_two() {
Some(Align { raw })
} else { None }
}
pub const fn from_bytes_const(align: u64) -> Option<Align> {
if align.is_power_of_two() {
let raw = Int::from(align);
Some(Align { raw })
} else { None }
}
pub fn from_bits(align: impl Into<Int>) -> Option<Align> {
let align = align.into();
if align % 8 != 0 { return None; }
Align::from_bytes(align / 8)
}
pub const fn from_bits_const(align: u64) -> Option<Align> {
if align % 8 != 0 { return None; }
Align::from_bytes_const(align / 8)
}
pub fn bytes(self) -> Int {
self.raw
}
pub fn max_for_offset(offset: Size) -> Option<Align> {
offset.bytes().trailing_zeros()
.map(|trailing| {
let bytes = Int::from(2).pow(trailing);
Align::from_bytes(bytes).unwrap()
})
}
pub fn restrict_for_offset(self, offset: Size) -> Align {
Align::max_for_offset(offset)
.map(|align| align.min(self))
.unwrap_or(self)
}
}