From 73e8afbc77c1ff8455f9d202e058f74084ea9d4f Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 23 Aug 2024 14:59:49 -0700 Subject: [PATCH] infallible narrow tosses fractional bits and has the same or greater integer bits --- fixed_wide/src/narrow.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/fixed_wide/src/narrow.rs b/fixed_wide/src/narrow.rs index ee18418..4ba82a9 100644 --- a/fixed_wide/src/narrow.rs +++ b/fixed_wide/src/narrow.rs @@ -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{ + type Output=i8; + fn narrow(self)->Self::Output{ + (self.to_bits()>>8) as i8 + } + } + + #[test] + fn test_fixed_i16_i8(){ + let a=fixed::FixedI16::::from(5)/2; + assert_eq!(a.narrow(),2); + } }