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
//! Implementation of unboxed 31-bit integers.
use super::VMGcRef;
/// A 31-bit integer for use with `i31ref`.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct I31(pub(super) u32);
impl Default for I31 {
#[inline]
fn default() -> Self {
Self::wrapping_u32(0)
}
}
impl std::fmt::Debug for I31 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("I31")
.field("as_u32", &self.get_u32())
.field("as_i32", &self.get_i32())
.finish()
}
}
impl I31 {
const DISCRIMINANT: u32 = VMGcRef::I31_REF_DISCRIMINANT;
/// Construct a new `I31` from the given unsigned value.
///
/// Returns `None` if the value does not fit in the bottom 31 bits.
#[inline]
pub fn new_u32(value: u32) -> Option<Self> {
if ((value << 1) >> 1) == value {
let i31 = Self::wrapping_u32(value);
debug_assert_eq!(i31.get_u32(), value);
Some(i31)
} else {
None
}
}
/// Construct a new `I31` from the given signed value.
///
/// Returns `None` if the value does not fit in the bottom 31 bits.
#[inline]
pub fn new_i32(value: i32) -> Option<Self> {
if ((value << 1) >> 1) == value {
let i31 = Self::wrapping_i32(value);
debug_assert_eq!(i31.get_i32(), value);
Some(i31)
} else {
None
}
}
/// Construct a new `I31` from the given unsigned value.
///
/// If the value doesn't fit in the bottom 31 bits, it is wrapped such that
/// the wrapped value does.
#[inline]
pub fn wrapping_u32(value: u32) -> Self {
Self((value << 1) | Self::DISCRIMINANT)
}
/// Construct a new `I31` from the given signed value.
///
/// If the value doesn't fit in the bottom 31 bits, it is wrapped such that
/// the wrapped value does.
#[inline]
#[allow(clippy::cast_sign_loss)]
pub fn wrapping_i32(value: i32) -> Self {
Self::wrapping_u32(value as u32)
}
/// Get this `I31`'s value as an unsigned integer.
#[inline]
pub fn get_u32(&self) -> u32 {
self.0 >> 1
}
/// Get this `I31`'s value as ansigned integer.
#[inline]
pub fn get_i32(&self) -> i32 {
(self.0 as i32) >> 1
}
}