infallible narrow tosses fractional bits and has the same or greater integer bits

This commit is contained in:
Quaternions 2024-08-23 14:59:49 -07:00
parent 47f6e75de9
commit 73e8afbc77

View File

@ -34,4 +34,17 @@ mod tests {
assert!(matches!(64i16.try_narrow(),Ok(64i8)));
assert!(matches!((-257i16).try_narrow(),Err(NarrowError::Underflow)));
}
impl Narrow for fixed::FixedI16<typenum::consts::U8>{
type Output=i8;
fn narrow(self)->Self::Output{
(self.to_bits()>>8) as i8
}
}
#[test]
fn test_fixed_i16_i8(){
let a=fixed::FixedI16::<typenum::consts::U8>::from(5)/2;
assert_eq!(a.narrow(),2);
}
}