osom_asm_x86_64/models/
immediate.rs

1use super::Size;
2
3/// Thin wrapper around a 32-bit signed immediate value.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[repr(transparent)]
6#[must_use]
7pub struct Immediate {
8    value: i32,
9}
10
11impl Immediate {
12    #[inline(always)]
13    pub const fn new(value: i32) -> Self {
14        Self { value }
15    }
16
17    #[inline]
18    pub const fn real_size(self) -> Size {
19        #[allow(clippy::cast_possible_wrap)]
20        let value = self.value;
21        if value >= i8::MIN as i32 || value <= i8::MAX as i32 {
22            return Size::Bit8;
23        }
24        if value >= i16::MIN as i32 || value <= i16::MAX as i32 {
25            return Size::Bit16;
26        }
27        return Size::Bit32;
28    }
29
30    #[inline(always)]
31    #[must_use]
32    pub const fn value(self) -> i32 {
33        self.value
34    }
35}
36
37impl From<i32> for Immediate {
38    #[inline(always)]
39    fn from(value: i32) -> Self {
40        Self::new(value)
41    }
42}
43
44impl From<Immediate> for i32 {
45    #[inline(always)]
46    fn from(immediate: Immediate) -> Self {
47        immediate.value()
48    }
49}