make UIntInRange[Inclusive][Type] castable from/to any UInt<N> and add methods to get bit_width, start, and end

This commit is contained in:
Jacob Lifshay 2025-10-23 23:52:41 -07:00
parent 040cefea21
commit 3e5b2f126a
Signed by: programmerjake
SSH key fingerprint: SHA256:HnFTLGpSm4Q4Fj502oCFisjZSoakwEuTsJJMSke63RQ

View file

@ -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<Start: Size, End: Size> fmt::Debug for $UIntInRangeType<Start, End> {
@ -477,18 +486,22 @@ macro_rules! define_uint_in_range_type {
}
}
impl<Start: Size, End: Size> ExprCastTo<UInt> for $UIntInRangeType<Start, End> {
fn cast_to(src: Expr<Self>, to_type: UInt) -> Expr<UInt> {
impl<Start: Size, End: Size, Width: Size> ExprCastTo<UIntType<Width>>
for $UIntInRangeType<Start, End>
{
fn cast_to(src: Expr<Self>, to_type: UIntType<Width>) -> Expr<UIntType<Width>> {
src.cast_to_bits().cast_to(to_type)
}
}
impl<Start: Size, End: Size> ExprCastTo<$UIntInRangeType<Start, End>> for UInt {
impl<Start: Size, End: Size, Width: Size> ExprCastTo<$UIntInRangeType<Start, End>>
for UIntType<Width>
{
fn cast_to(
src: Expr<Self>,
to_type: $UIntInRangeType<Start, End>,
) -> Expr<$UIntInRangeType<Start, End>> {
src.cast_bits_to(to_type)
src.cast_to(to_type.value).cast_bits_to(to_type)
}
}