Skip to content

Commit

Permalink
Add From<T> conversions to FieldValue for usize and NonZero types. (
Browse files Browse the repository at this point in the history
  • Loading branch information
obi1kenobi authored Jan 16, 2025
1 parent faa064e commit a88b2d6
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions trustfall_core/src/ir/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,18 @@ macro_rules! impl_field_value_from_uint {
impl_field_value_from_int!(i8 i16 i32 i64);
impl_field_value_from_uint!(u8 u16 u32 u64);

impl From<usize> for FieldValue {
fn from(value: usize) -> Self {
Self::Uint64(value.try_into().expect("failed to convert usize to u64"))
}
}

impl From<isize> for FieldValue {
fn from(value: isize) -> Self {
Self::Int64(value.try_into().expect("failed to convert isize to i64"))
}
}

impl TryFrom<Option<f32>> for FieldValue {
type Error = (f32, &'static str);

Expand All @@ -407,6 +419,44 @@ impl TryFrom<Option<f64>> for FieldValue {
}
}

macro_rules! impl_field_value_from_unsigned_nonzero {
( $( $NonZero:path )+ ) => {
$(
impl From<$NonZero> for FieldValue {
fn from(v: $NonZero) -> Self {
Self::Uint64(v.get().into())
}
}
)+
}
}

impl_field_value_from_unsigned_nonzero!(
std::num::NonZeroU8
std::num::NonZeroU16
std::num::NonZeroU32
std::num::NonZeroU64
);

macro_rules! impl_field_value_from_signed_nonzero {
( $( $NonZero:path )+ ) => {
$(
impl From<$NonZero> for FieldValue {
fn from(v: $NonZero) -> Self {
Self::Int64(v.get().into())
}
}
)+
}
}

impl_field_value_from_signed_nonzero!(
std::num::NonZeroI8
std::num::NonZeroI16
std::num::NonZeroI32
std::num::NonZeroI64
);

impl<T: Into<FieldValue>> From<Option<T>> for FieldValue {
fn from(opt: Option<T>) -> FieldValue {
match opt {
Expand Down

0 comments on commit a88b2d6

Please sign in to comment.