From dda8ebefc4212b6efef38a8527528baf003f19ab Mon Sep 17 00:00:00 2001 From: Quaternions Date: Mon, 26 Aug 2024 17:04:41 -0700 Subject: [PATCH] create wide_traits crate --- fixed_wide/Cargo.lock | 5 ++ fixed_wide/Cargo.toml | 1 + fixed_wide/src/lib.rs | 1 - fixed_wide/src/wide.rs | 6 +-- fixed_wide_vectors/Cargo.lock | 6 +++ fixed_wide_vectors/Cargo.toml | 1 + fixed_wide_vectors/src/macros/wide.rs | 4 +- fixed_wide_vectors/src/tests/mod.rs | 2 +- wide_traits/.gitignore | 1 + wide_traits/Cargo.lock | 63 +++++++++++++++++++++++ wide_traits/Cargo.toml | 10 ++++ wide_traits/src/lib.rs | 2 + {fixed_wide => wide_traits}/src/narrow.rs | 20 ++++--- wide_traits/src/wide.rs | 4 ++ 14 files changed, 110 insertions(+), 16 deletions(-) create mode 100644 wide_traits/.gitignore create mode 100644 wide_traits/Cargo.lock create mode 100644 wide_traits/Cargo.toml create mode 100644 wide_traits/src/lib.rs rename {fixed_wide => wide_traits}/src/narrow.rs (61%) create mode 100644 wide_traits/src/wide.rs diff --git a/fixed_wide/Cargo.lock b/fixed_wide/Cargo.lock index b4a8917..b181692 100644 --- a/fixed_wide/Cargo.lock +++ b/fixed_wide/Cargo.lock @@ -14,6 +14,7 @@ version = "0.1.0" dependencies = [ "bnum", "typenum", + "wide_traits", ] [[package]] @@ -21,3 +22,7 @@ name = "typenum" version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "wide_traits" +version = "0.1.0" diff --git a/fixed_wide/Cargo.toml b/fixed_wide/Cargo.toml index 3ce0e48..86f99f6 100644 --- a/fixed_wide/Cargo.toml +++ b/fixed_wide/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" [dependencies] bnum = "0.11.0" typenum = "1.17.0" +wide_traits = { version = "0.1.0", path = "../wide_traits" } diff --git a/fixed_wide/src/lib.rs b/fixed_wide/src/lib.rs index 655a298..8bdda41 100644 --- a/fixed_wide/src/lib.rs +++ b/fixed_wide/src/lib.rs @@ -1,3 +1,2 @@ pub mod wide; pub mod types; -pub mod narrow; diff --git a/fixed_wide/src/wide.rs b/fixed_wide/src/wide.rs index 7b2db63..0a5140a 100644 --- a/fixed_wide/src/wide.rs +++ b/fixed_wide/src/wide.rs @@ -1,5 +1,6 @@ use bnum::cast::As; use bnum::BInt; +use wide_traits::wide::WideMul; use typenum::{Sum,Unsigned}; use std::marker::PhantomData; @@ -82,11 +83,6 @@ impl_operator!( Fixed, BitOr, bitor, Self ); impl_assign_operator!( Fixed, BitXorAssign, bitxor_assign ); impl_operator!( Fixed, BitXor, bitxor, Self ); -pub trait WideMul{ - type Output; - fn wide_mul(self,rhs:Rhs)->Self::Output; -} - //going wider native macro_rules! impl_wide_mul { ($lhs: expr,$rhs: expr) => { diff --git a/fixed_wide_vectors/Cargo.lock b/fixed_wide_vectors/Cargo.lock index 79830bd..5c9399b 100644 --- a/fixed_wide_vectors/Cargo.lock +++ b/fixed_wide_vectors/Cargo.lock @@ -14,6 +14,7 @@ version = "0.1.0" dependencies = [ "bnum", "typenum", + "wide_traits", ] [[package]] @@ -21,6 +22,7 @@ name = "fixed_wide_vectors" version = "0.1.0" dependencies = [ "fixed_wide", + "wide_traits", ] [[package]] @@ -28,3 +30,7 @@ name = "typenum" version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "wide_traits" +version = "0.1.0" diff --git a/fixed_wide_vectors/Cargo.toml b/fixed_wide_vectors/Cargo.toml index b46e6a5..b96d9c1 100644 --- a/fixed_wide_vectors/Cargo.toml +++ b/fixed_wide_vectors/Cargo.toml @@ -5,3 +5,4 @@ edition = "2021" [dependencies] fixed_wide = { version = "0.1.0", path = "../fixed_wide" } +wide_traits = { version = "0.1.0", path = "../wide_traits" } diff --git a/fixed_wide_vectors/src/macros/wide.rs b/fixed_wide_vectors/src/macros/wide.rs index 7e12358..434d383 100644 --- a/fixed_wide_vectors/src/macros/wide.rs +++ b/fixed_wide_vectors/src/macros/wide.rs @@ -2,7 +2,7 @@ #[macro_export(local_inner_macros)] macro_rules! impl_wide_operations { ( $struct: ident { $($field: ident), + }, $size: expr ) => { - impl> fixed_wide::wide::WideMul for $struct { + impl> wide_traits::wide::WideMul for $struct { type Output=$struct; #[inline] fn wide_mul(self, rhs: Self) -> Self::Output { @@ -11,7 +11,7 @@ macro_rules! impl_wide_operations { } } } - impl,T:Copy+fixed_wide::wide::WideMul> $struct { + impl,T:Copy+wide_traits::wide::WideMul> $struct { #[inline] pub fn wide_dot(self, other: Self) -> U { $crate::sum_repeating!( diff --git a/fixed_wide_vectors/src/tests/mod.rs b/fixed_wide_vectors/src/tests/mod.rs index c15e56d..b48a11c 100644 --- a/fixed_wide_vectors/src/tests/mod.rs +++ b/fixed_wide_vectors/src/tests/mod.rs @@ -1,4 +1,4 @@ -use fixed_wide::wide::WideMul; +use wide_traits::wide::WideMul; use crate::Vector3; diff --git a/wide_traits/.gitignore b/wide_traits/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/wide_traits/.gitignore @@ -0,0 +1 @@ +/target diff --git a/wide_traits/Cargo.lock b/wide_traits/Cargo.lock new file mode 100644 index 0000000..a3a18a8 --- /dev/null +++ b/wide_traits/Cargo.lock @@ -0,0 +1,63 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "az" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973" + +[[package]] +name = "bytemuck" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fd4c6dcc3b0aea2f5c0b4b82c2b15fe39ddbc76041a310848f4706edf76bb31" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + +[[package]] +name = "fixed" +version = "1.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85c6e0b89bf864acd20590dbdbad56f69aeb898abfc9443008fd7bd48b2cc85a" +dependencies = [ + "az", + "bytemuck", + "half", + "typenum", +] + +[[package]] +name = "half" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" +dependencies = [ + "cfg-if", + "crunchy", +] + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "wide_traits" +version = "0.1.0" +dependencies = [ + "fixed", + "typenum", +] diff --git a/wide_traits/Cargo.toml b/wide_traits/Cargo.toml new file mode 100644 index 0000000..806fb3e --- /dev/null +++ b/wide_traits/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "wide_traits" +version = "0.1.0" +edition = "2021" + +[dependencies] + +[dev-dependencies] +fixed = "1.28.0" +typenum = "1.17.0" diff --git a/wide_traits/src/lib.rs b/wide_traits/src/lib.rs new file mode 100644 index 0000000..21c9224 --- /dev/null +++ b/wide_traits/src/lib.rs @@ -0,0 +1,2 @@ +pub mod wide; +pub mod narrow; diff --git a/fixed_wide/src/narrow.rs b/wide_traits/src/narrow.rs similarity index 61% rename from fixed_wide/src/narrow.rs rename to wide_traits/src/narrow.rs index f48483b..698eacc 100644 --- a/fixed_wide/src/narrow.rs +++ b/wide_traits/src/narrow.rs @@ -3,13 +3,19 @@ pub trait Narrow{ fn narrow(self)->Self::Output; } #[derive(Debug)] -pub enum NarrowError{ +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; + fn try_narrow(self)->Result; } #[cfg(test)] mod tests { @@ -18,12 +24,12 @@ mod tests { //TODO: use num_traits to do a blanket implementation (selfResult{ + fn try_narrow(self)->Result{ if self{ diff --git a/wide_traits/src/wide.rs b/wide_traits/src/wide.rs new file mode 100644 index 0000000..341aea9 --- /dev/null +++ b/wide_traits/src/wide.rs @@ -0,0 +1,4 @@ +pub trait WideMul{ + type Output; + fn wide_mul(self,rhs:Rhs)->Self::Output; +}