From 47f6e75de96cdb5d46261654adfb9f9056f12f17 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 23 Aug 2024 14:50:23 -0700 Subject: [PATCH] narrow concept --- fixed_wide/src/lib.rs | 1 + fixed_wide/src/narrow.rs | 37 +++++++++++++++++++++++++++++++++++++ fixed_wide/src/wide.rs | 23 ----------------------- 3 files changed, 38 insertions(+), 23 deletions(-) create mode 100644 fixed_wide/src/narrow.rs 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; -}