Skip to main content

osom_encoders_x86_64/models/
size.rs

1use core::mem::transmute;
2
3/// Represents all the sizes used by the project from the X64 instruction set.
4#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
5#[repr(u8)]
6#[must_use]
7pub enum Size {
8    Bit8 = 1, // We start from 1 to allow Option<Size> optimization
9    Bit16 = 2,
10    Bit32 = 3,
11    Bit64 = 4,
12}
13
14impl Size {
15    /// Compares two [`Size`] values for equality.
16    #[inline(always)]
17    #[must_use]
18    pub const fn equals(self, other: Self) -> bool {
19        self.as_u8() == other.as_u8()
20    }
21
22    #[inline(always)]
23    #[must_use]
24    pub(crate) const fn as_u8(self) -> u8 {
25        unsafe {
26            let result = transmute::<Self, u8>(self);
27            core::hint::assert_unchecked(result > 0);
28            core::hint::assert_unchecked(result <= 4);
29            result
30        }
31    }
32
33    #[inline]
34    #[allow(dead_code)]
35    pub(crate) const fn from_u8(value: u8) -> Self {
36        debug_assert!(value > 0 && value <= 4, "Invalid size value");
37        unsafe { transmute(value) }
38    }
39}