From c1fd28aa92789fe7340d28c003101d0baa78ecbe Mon Sep 17 00:00:00 2001 From: joshua-spacetime Date: Tue, 10 Oct 2023 09:13:19 -0700 Subject: [PATCH] feat(397): Add min and max value helpers for builtin types (#398) Fixes #397. Adding these helpers for computing multidimensional index bounds. Any unbounded or infinite dimensions need to be made finite by computing the min or max value for the type of said dimension. For example ``` a < 5 and b < 6 ``` needs to become ``` a >= MIN and a < 5 and b >= MIN and b < 5 ``` --- crates/sats/src/algebraic_type.rs | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/crates/sats/src/algebraic_type.rs b/crates/sats/src/algebraic_type.rs index 7d4fbdb9cd..052efc1d40 100644 --- a/crates/sats/src/algebraic_type.rs +++ b/crates/sats/src/algebraic_type.rs @@ -240,6 +240,46 @@ impl AlgebraicType { pub fn from_value(value: &AlgebraicValue) -> Result { Self::deserialize(ValueDeserializer::from_ref(value)) } + + #[inline] + /// Given an AlgebraicType, returns the min value for that type. + pub fn min_value(&self) -> Option { + match *self { + Self::I8 => Some(i8::MIN.into()), + Self::U8 => Some(u8::MIN.into()), + Self::I16 => Some(i16::MIN.into()), + Self::U16 => Some(u16::MIN.into()), + Self::I32 => Some(i32::MIN.into()), + Self::U32 => Some(u32::MIN.into()), + Self::I64 => Some(i64::MIN.into()), + Self::U64 => Some(u64::MIN.into()), + Self::I128 => Some(i128::MIN.into()), + Self::U128 => Some(u128::MIN.into()), + Self::F32 => Some(f32::MIN.into()), + Self::F64 => Some(f64::MIN.into()), + _ => None, + } + } + + #[inline] + /// Given an AlgebraicType, returns the max value for that type. + pub fn max_value(&self) -> Option { + match *self { + Self::I8 => Some(i8::MAX.into()), + Self::U8 => Some(u8::MAX.into()), + Self::I16 => Some(i16::MAX.into()), + Self::U16 => Some(u16::MAX.into()), + Self::I32 => Some(i32::MAX.into()), + Self::U32 => Some(u32::MAX.into()), + Self::I64 => Some(i64::MAX.into()), + Self::U64 => Some(u64::MAX.into()), + Self::I128 => Some(i128::MAX.into()), + Self::U128 => Some(u128::MAX.into()), + Self::F32 => Some(f32::MAX.into()), + Self::F64 => Some(f64::MAX.into()), + _ => None, + } + } } #[cfg(test)]