pub enum Constant {
Bool(bool),
U32(u32),
U64(u64),
U128(u128),
}
impl Constant {
pub(super) fn required_bits(&self) -> u32 {
match *self {
Constant::Bool(value) => 32 - (value as u32).leading_zeros(),
Constant::U32(value) => 32 - value.leading_zeros(),
Constant::U64(value) => 64 - value.leading_zeros(),
Constant::U128(value) => 128 - value.leading_zeros(),
}
}
pub(crate) fn numeric_value(&self) -> u128 {
match *self {
Constant::Bool(value) => value.into(),
Constant::U32(value) => value.into(),
Constant::U64(value) => value.into(),
Constant::U128(value) => value,
}
}
}
impl From<bool> for Constant {
fn from(value: bool) -> Self {
Constant::Bool(value)
}
}
impl From<u8> for Constant {
fn from(value: u8) -> Self {
Constant::U32(value as _)
}
}
impl From<u16> for Constant {
fn from(value: u16) -> Self {
Constant::U32(value as _)
}
}
impl From<u32> for Constant {
fn from(value: u32) -> Self {
Constant::U32(value)
}
}
impl From<u64> for Constant {
fn from(value: u64) -> Self {
Constant::U64(value)
}
}
impl From<u128> for Constant {
fn from(value: u128) -> Self {
Constant::U128(value)
}
}