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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/// 24-bit unsigned integer.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bytemuck", derive(bytemuck::AnyBitPattern))]
#[repr(transparent)]
pub struct Int24(i32);

impl Int24 {
    /// The smallest value that can be represented by this integer type.
    pub const MIN: Self = Int24(-0x80_00_00);

    /// The largest value that can be represented by this integer type.
    pub const MAX: Self = Int24(0x7F_FF_FF);

    /// Create from a i32. Saturates on overflow.
    pub const fn new(raw: i32) -> Int24 {
        let overflow = raw > Self::MAX.0;
        let underflow = raw < Self::MIN.0;
        let raw = raw * !(overflow || underflow) as i32
            + Self::MAX.0 * overflow as i32
            + Self::MIN.0 * underflow as i32;
        Int24(raw)
    }

    /// Create from a i32, returning `None` if the value overflows.
    pub const fn checked_new(raw: i32) -> Option<Int24> {
        if raw > Self::MAX.0 || raw < Self::MIN.0 {
            None
        } else {
            Some(Int24(raw))
        }
    }

    /// Returns this value as an unsigned 32-bit integer.
    pub const fn to_i32(self) -> i32 {
        self.0
    }

    pub const fn to_be_bytes(self) -> [u8; 3] {
        let bytes = self.0.to_be_bytes();
        [bytes[1], bytes[2], bytes[3]]
    }

    pub const fn from_be_bytes(bytes: [u8; 3]) -> Self {
        let extra_byte = ((bytes[0] & 0b10000000) >> 7) * 0b11111111;
        let extra_byte = (extra_byte as i32) << 24;
        Int24::new(extra_byte | (bytes[0] as i32) << 16 | (bytes[1] as i32) << 8 | bytes[2] as i32)
    }
}

impl From<Int24> for i32 {
    fn from(src: Int24) -> i32 {
        src.0
    }
}

impl std::fmt::Display for Int24 {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn constructor() {
        assert_eq!(Int24::MAX, Int24::new(i32::MAX));
        assert_eq!(Int24::MIN, Int24::new(i32::MIN));
        assert_eq!(-10, Int24::new(-10).to_i32());
        assert_eq!(10, Int24::new(10).to_i32());
    }

    #[test]
    fn to_be_bytes() {
        assert_eq!(
            Int24::new(0).to_be_bytes(),
            [0b00000000, 0b00000000, 0b00000000]
        );

        assert_eq!(
            Int24::new(123_456).to_be_bytes(),
            [0b00000001, 0b11100010, 0b01000000]
        );
        assert_eq!(
            Int24::new(-123_456).to_be_bytes(),
            [0b11111110, 0b00011101, 0b11000000]
        );

        assert_eq!(
            Int24::new(0x7F_FF_FF).to_be_bytes(),
            [0b01111111, 0b11111111, 0b11111111]
        );
        assert_eq!(
            Int24::new(-0x80_00_00).to_be_bytes(),
            [0b10000000, 0b00000000, 0b00000000]
        );
    }

    #[test]
    fn from_be_bytes() {
        assert_eq!(
            Int24::from_be_bytes([0b00000000, 0b00000000, 0b00000000]),
            Int24::new(0)
        );

        assert_eq!(
            Int24::from_be_bytes([0b00000001, 0b11100010, 0b01000000]),
            Int24::new(123_456)
        );
        assert_eq!(
            Int24::from_be_bytes([0b11111110, 0b00011101, 0b11000000]),
            Int24::new(-123_456)
        );

        assert_eq!(
            Int24::from_be_bytes([0b01111111, 0b11111111, 0b11111111]),
            Int24::new(0x7F_FF_FF)
        );
        assert_eq!(
            Int24::from_be_bytes([0b10000000, 0b00000000, 0b00000000]),
            Int24::new(-0x80_00_00)
        );
    }

    #[test]
    fn round_trip() {
        for v in Int24::MIN.to_i32()..=Int24::MAX.to_i32() {
            let int = Int24::new(v);
            let bytes = int.to_be_bytes();
            let int_prime = Int24::from_be_bytes(bytes);
            assert_eq!(int_prime, int);
        }
    }
}