Skip to main content

boa_engine/property/
nonmaxu32.rs

1/// An integer that is not `u32::MAX`.
2#[derive(PartialEq, Debug, Clone, Copy, Eq, Hash)]
3pub struct NonMaxU32 {
4    inner: u32,
5}
6
7impl NonMaxU32 {
8    /// Creates a non-max `u32`.
9    ///
10    /// # Safety
11    ///
12    /// The caller must ensure that the given value is not `u32::MAX`.
13    #[must_use]
14    pub const unsafe fn new_unchecked(inner: u32) -> Self {
15        debug_assert!(inner != u32::MAX);
16
17        Self { inner }
18    }
19
20    /// Creates a non-max `u32` if the given value is not `u32::MAX`.
21    #[must_use]
22    pub const fn new(inner: u32) -> Option<Self> {
23        if inner == u32::MAX {
24            return None;
25        }
26
27        // SAFETY: We checked that `inner` is not `u32::MAX`.
28        unsafe { Some(Self::new_unchecked(inner)) }
29    }
30
31    /// Returns the value as a primitive type.
32    #[must_use]
33    pub const fn get(&self) -> u32 {
34        self.inner
35    }
36}