fixed_wide_vectors/fixed_wide/src/narrow.rs
2024-08-23 17:55:14 -07:00

52 lines
1.1 KiB
Rust

pub trait Narrow{
type Output;
fn narrow(self)->Self::Output;
}
#[derive(Debug)]
pub enum NarrowError{
Overflow,
Underflow,
}
pub trait TryNarrow{
type Output;
fn try_narrow(self)->Result<Self::Output,NarrowError>;
}
#[cfg(test)]
mod tests {
use super::*;
//TODO: use num_traits to do a blanket implementation (self<T::MIN as U)
impl TryNarrow for i16{
type Output=i8;
fn try_narrow(self)->Result<Self::Output,NarrowError>{
if self<i8::MIN as i16{
return Err(NarrowError::Underflow);
}
if (i8::MAX as i16)<self{
return Err(NarrowError::Overflow);
}
Ok(self as i8)
}
}
#[test]
fn test_i16_i8(){
assert!(matches!(257i16.try_narrow(),Err(NarrowError::Overflow)));
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);
}
}