Skip to main content

cranpose_core/
packed_u64.rs

1//! A 64-bit number kept at the alignment of a 32-bit word.
2
3use std::fmt;
4
5/// A 64-bit number kept at the alignment of a 32-bit word on ESP-IDF. There a
6/// `u64` takes 8-byte alignment, and so would every record that holds one:
7/// padding around the field, and on a heap that hands out 4-byte aligned
8/// blocks, a block 8 bytes longer. Elsewhere it is a `u64`, which the
9/// compiler reads and compares without copying it out of a packed field.
10#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
11#[cfg_attr(target_os = "espidf", repr(C, packed(4)))]
12#[cfg_attr(not(target_os = "espidf"), repr(transparent))]
13pub struct PackedU64(u64);
14
15impl PackedU64 {
16    /// `value`, kept at a word's alignment.
17    pub const fn new(value: u64) -> Self {
18        Self(value)
19    }
20
21    /// The number.
22    pub const fn get(self) -> u64 {
23        self.0
24    }
25}
26
27impl From<u64> for PackedU64 {
28    fn from(value: u64) -> Self {
29        Self(value)
30    }
31}
32
33impl fmt::Debug for PackedU64 {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        self.get().fmt(f)
36    }
37}