diff --git a/fixed_wide/src/lib.rs b/fixed_wide/src/lib.rs index 8bdda41..655a298 100644 --- a/fixed_wide/src/lib.rs +++ b/fixed_wide/src/lib.rs @@ -1,2 +1,3 @@ pub mod wide; pub mod types; +pub mod narrow; diff --git a/fixed_wide/src/narrow.rs b/fixed_wide/src/narrow.rs new file mode 100644 index 0000000..ee18418 --- /dev/null +++ b/fixed_wide/src/narrow.rs @@ -0,0 +1,37 @@ +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; +} +#[cfg(test)] +mod tests { + use super::*; + + impl TryNarrow for i16{ + type Output=i8; + fn try_narrow(self)->Result{ + if self WideMul> for FixedI256 } } } - -pub enum Narrower{ - Fits(T), - Max, - Min, -} - -fn tat(){ - let a:i8=match 257i64.try_into() { - Ok(it) => it, - Err(err) => panic!(), - }; -} - -//i16 -> i8 -// 257i16.try_narrow::() -> Narrower::Max -// 128i16.try_narrow::() -> Narrower::Fits(128i8) -// -257i16.try_narrow::() -> Narrower::Min - -pub trait TryNarrow{ - type Output; - fn wide_mul(self,rhs:Rhs)->Self::Output; -}