pub struct U31(u32);
impl U31 {
pub const fn new(value: u32) -> Option<Self> {
if value > i32::MAX as _ {
None
} else {
Some(Self(value))
}
}
}
impl From<U31> for i32 {
fn from(value: U31) -> Self {
value.0 as _
}
}
pub struct NegativeI32(i32);
impl NegativeI32 {
pub const fn new(value: i32) -> Option<Self> {
if value < 0 {
Some(Self(value))
} else {
None
}
}
}
impl From<NegativeI32> for i32 {
fn from(value: NegativeI32) -> Self {
value.0
}
}
pub fn fold_into_i32<T, E>(result: Result<T, E>) -> i32
where
T: Into<U31>,
E: Into<NegativeI32>,
{
match result {
Ok(t) => i32::from(t.into()),
Err(e) => i32::from(e.into()),
}
}