From 3e5b2f126aa3fb5e99cae8f08af37489c5da2873 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Thu, 23 Oct 2025 23:52:41 -0700 Subject: [PATCH] make UIntInRange[Inclusive][Type] castable from/to any UInt and add methods to get bit_width, start, and end --- crates/fayalite/src/int/uint_in_range.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/crates/fayalite/src/int/uint_in_range.rs b/crates/fayalite/src/int/uint_in_range.rs index 39f4051..970a439 100644 --- a/crates/fayalite/src/int/uint_in_range.rs +++ b/crates/fayalite/src/int/uint_in_range.rs @@ -304,6 +304,15 @@ macro_rules! define_uint_in_range_type { $SerdeRange { start, end }.intern_sized(), )) } + pub fn bit_width(self) -> usize { + self.value.width() + } + pub fn start(self) -> Start::SizeType { + self.range.get().start + } + pub fn end(self) -> End::SizeType { + self.range.get().end + } } impl fmt::Debug for $UIntInRangeType { @@ -477,18 +486,22 @@ macro_rules! define_uint_in_range_type { } } - impl ExprCastTo for $UIntInRangeType { - fn cast_to(src: Expr, to_type: UInt) -> Expr { + impl ExprCastTo> + for $UIntInRangeType + { + fn cast_to(src: Expr, to_type: UIntType) -> Expr> { src.cast_to_bits().cast_to(to_type) } } - impl ExprCastTo<$UIntInRangeType> for UInt { + impl ExprCastTo<$UIntInRangeType> + for UIntType + { fn cast_to( src: Expr, to_type: $UIntInRangeType, ) -> Expr<$UIntInRangeType> { - src.cast_bits_to(to_type) + src.cast_to(to_type.value).cast_bits_to(to_type) } }