pub trait Narrow{ type Output; fn narrow(self)->Self::Output; } #[derive(Debug)] pub enum Error{ Overflow, Underflow, } impl std::fmt::Display for Error{ fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{ write!(f,"{self:?}") } } impl std::error::Error for Error{} pub trait TryNarrow{ type Output; fn try_narrow(self)->Result; } #[cfg(test)] mod tests { use super::*; //TODO: use num_traits to do a blanket implementation (selfResult{ if self{ 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); } }