From c3026c67e987a8f142d46dffadb6b432aca11186 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 5 Sep 2024 12:49:20 -0700 Subject: [PATCH] delete everything and start over --- fixed_wide/Cargo.toml | 1 - fixed_wide_vectors/Cargo.toml | 4 +- fixed_wide_vectors/src/macros/common.rs | 137 -------- fixed_wide_vectors/src/macros/fixed_wide.rs | 369 -------------------- fixed_wide_vectors/src/macros/matrix.rs | 200 ----------- fixed_wide_vectors/src/macros/mod.rs | 14 - fixed_wide_vectors/src/macros/vector.rs | 154 -------- fixed_wide_vectors/src/matrix.rs | 22 +- fixed_wide_vectors/src/vector.rs | 66 +--- 9 files changed, 14 insertions(+), 953 deletions(-) diff --git a/fixed_wide/Cargo.toml b/fixed_wide/Cargo.toml index cc218bc..eb8c705 100644 --- a/fixed_wide/Cargo.toml +++ b/fixed_wide/Cargo.toml @@ -11,4 +11,3 @@ zeroes=["ratio","dep:arrayvec"] [dependencies] bnum = "0.11.0" arrayvec = { version = "0.7.6", optional = true } -paste = "1.0.15" diff --git a/fixed_wide_vectors/Cargo.toml b/fixed_wide_vectors/Cargo.toml index b2f5856..556dee1 100644 --- a/fixed_wide_vectors/Cargo.toml +++ b/fixed_wide_vectors/Cargo.toml @@ -4,9 +4,9 @@ version = "0.1.0" edition = "2021" [features] -default=["fixed_wide"] +default=["fixed_wide","named-fields"] +named-fields=[] fixed_wide=["dep:fixed_wide"] [dependencies] fixed_wide = { version = "0.1.0", path = "../fixed_wide", optional = true } -paste = "1.0.15" diff --git a/fixed_wide_vectors/src/macros/common.rs b/fixed_wide_vectors/src/macros/common.rs index bb92dc7..8b13789 100644 --- a/fixed_wide_vectors/src/macros/common.rs +++ b/fixed_wide_vectors/src/macros/common.rs @@ -1,138 +1 @@ -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_common { - ( $struct: ident { $($field: ident), + }, $size: expr ) => { - impl $struct { - /// Constructs a new vector with the specified values for each field. - /// - /// # Example - /// - /// ``` - /// use fixed_wide_vectors::Vector2; - /// - /// let vec2 = Vector2::new(0, 0); - /// - /// assert_eq!(vec2.x, 0); - /// assert_eq!(vec2.y, 0); - /// ``` - #[inline(always)] - pub const fn new( $($field: T), + ) -> Self { - Self { - $( $field ), + - } - } - /// Consumes the vector and returns its values as an array. - /// - /// # Example - /// - /// ``` - /// use fixed_wide_vectors::Vector2; - /// - /// let vec2 = Vector2::new(0, 0); - /// let array = vec2.to_array(); - /// - /// assert_eq!(array, [0, 0]); - /// ``` - #[inline(always)] - pub fn to_array(self) -> [T; $size] { - [ $(self.$field), + ] - } - - /// Consumes the vector and returns a new vector with the given function applied on each field. - /// - /// # Example - /// - /// ``` - /// use fixed_wide_vectors::Vector2; - /// - /// let vec2 = Vector2::new(1, 2) - /// .map(|i| i * 2); - /// - /// assert_eq!(vec2, Vector2::new(2, 4)); - /// ``` - #[inline] - pub fn map(self, f: F) -> $struct - where - F: Fn(T) -> U - { - $struct { - $( $field: f(self.$field) ), + - } - } - } - - impl $struct { - /// Constructs a vector using the given `value` as the value for all of its fields. - /// - /// # Example - /// - /// ``` - /// use fixed_wide_vectors::Vector2; - /// - /// let vec2 = Vector2::from_value(0); - /// - /// assert_eq!(vec2, Vector2::new(0, 0)); - /// ``` - #[inline(always)] - pub const fn from_value(value: T) -> Self { - Self { - $( $field: value ), + - } - } - } - - impl From<[T; $size]> for $struct { - fn from(from: [T; $size]) -> Self { - let mut iterator = from.into_iter(); - - Self { - // SAFETY: We know the size of `from` so `iterator.next()` is always `Some(..)` - $( $field: unsafe { iterator.next().unwrap_unchecked() } ), + - } - } - } - - impl core::fmt::Debug for $struct { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - let identifier = core::stringify!($struct); - - f.debug_struct(identifier) - $( .field( core::stringify!($field), &self.$field ) ) + - .finish() - } - } - - impl PartialEq for $struct { - fn eq(&self, other: &Self) -> bool { - $( self.$field == other.$field ) && + - } - } - - impl Eq for $struct { } - - impl core::hash::Hash for $struct { - fn hash(&self, state: &mut H) { - $( self.$field.hash(state); ) + - } - } - - impl Clone for $struct { - fn clone(&self) -> Self { - Self { - $( $field: self.$field.clone() ), + - } - } - } - - impl Copy for $struct { } - - impl Default for $struct { - fn default() -> Self { - Self { - $( $field: T::default() ), + - } - } - } - } -} diff --git a/fixed_wide_vectors/src/macros/fixed_wide.rs b/fixed_wide_vectors/src/macros/fixed_wide.rs index 3812d86..8b13789 100644 --- a/fixed_wide_vectors/src/macros/fixed_wide.rs +++ b/fixed_wide_vectors/src/macros/fixed_wide.rs @@ -1,370 +1 @@ -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_wide_vector_operations_2arg_not_const_generic { - ( - ($struct: ident { $($field: ident), + }, $size: expr), - ($lhs:expr, $rhs:expr) - ) => { - impl $struct>{ - paste::item!{ - #[inline] - pub fn [](self,rhs:$struct>)->$struct>{ - $struct{ - $( $field: self.$field.[](rhs.$field) ), + - } - } - #[inline] - pub fn [](self,rhs:$struct>)->fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>{ - $crate::sum_repeating!( - $( + (self.$field.[](rhs.$field)) ) + - ) - } - } - } - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_wide_vector_operations_1arg_not_const_generic { - ( - ($struct: ident { $($field: ident), + }, $size: expr), - $n:expr - ) => { - impl $struct>{ - paste::item!{ - #[inline] - pub fn wide_length_squared(&self)->fixed_wide::fixed::Fixed<{$n*2},{$n*2*32}>{ - $crate::sum_repeating!( - $( + self.$field.[](self.$field) ) + - ) - } - } - } - }; -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! do_macro_8x8{ - ( - $macro:ident, - $any:tt - )=>{ - $crate::macro_repeated!($macro, $any, - (1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),(8,1), - (1,2),(2,2),(3,2),(4,2),(5,2),(6,2),(7,2),(8,2), - (1,3),(2,3),(3,3),(4,3),(5,3),(6,3),(7,3),(8,3), - (1,4),(2,4),(3,4),(4,4),(5,4),(6,4),(7,4),(8,4), - (1,5),(2,5),(3,5),(4,5),(5,5),(6,5),(7,5),(8,5), - (1,6),(2,6),(3,6),(4,6),(5,6),(6,6),(7,6),(8,6), - (1,7),(2,7),(3,7),(4,7),(5,7),(6,7),(7,7),(8,7), - (1,8),(2,8),(3,8),(4,8),(5,8),(6,8),(7,8),(8,8) - ); - }; -} - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! do_macro_8{ - ( - $macro:ident, - $any:tt - )=>{ - $crate::macro_repeated!($macro, $any, 1,2,3,4,5,6,7,8); - }; -} - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_wide_vector_operations { - ( $struct: ident { $($field: ident), + }, $size: expr ) => { - $crate::do_macro_8!(impl_wide_vector_operations_1arg_not_const_generic,($struct { $($field), + }, $size)); - $crate::do_macro_8x8!(impl_wide_vector_operations_2arg_not_const_generic,($struct { $($field), + }, $size)); - }; -} - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_vector_3_wide_cross { - ( - (), - ($lhs:expr, $rhs:expr) - )=>{ - impl Vector3>{ - paste::item!{ - #[inline] - pub fn [](self,rhs:Vector3>)->Vector3>{ - Vector3{ - x:self.y.[](rhs.z)-self.z.[](rhs.y), - y:self.z.[](rhs.x)-self.x.[](rhs.z), - z:self.x.[](rhs.y)-self.y.[](rhs.x) - } - } - } - } - } -} - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_vector_wide_3 { - ()=>{ - $crate::do_macro_8x8!(impl_vector_3_wide_cross,()); - } -} - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_wide_dot_transpose_helper { - ( - $lhs_axis:expr, $wide_mul:ident, $rhs:ident, - ($struct: ident { $($field: ident), + }), - ($from_struct: ident { $($from_field: ident), + }), - $static_field: ident - ) => { - $crate::sum_repeating!( - $( + $lhs_axis.$field.$wide_mul($rhs.$from_field.$static_field) ) + - ) - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_wide_dot_inner { - ( - // MatY.MatX = MatY - $lhs:ident, $lhs_field_outer:ident, $wide_mul:ident, $rhs:ident, - $struct_inner_thru: tt, //VecX - ($struct_inner: ident { $($field_inner: ident), + }), //VecX - ($rhs_struct_inner: ident { $($rhs_field_inner: ident), + }), //VecZ - $rhs_outer: tt //MatX - ) => { - $rhs_struct_inner { - $( - //directly dot product to avoid a copy - $rhs_field_inner: $crate::impl_matrix_wide_dot_transpose_helper!{ - //lhs.axis.wide_mul(rhs_t.axis) - $lhs.$lhs_field_outer,$wide_mul,$rhs, - $struct_inner_thru, //VecZ - $rhs_outer, //MatX - $rhs_field_inner - } - ), + - } - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_wide_dot_outer { - ( - // MatY.MatX = MatY - $lhs:ident, $wide_mul:ident, $rhs:ident, - //result matrix shape - ($struct_outer: ident { $($field_outer: ident), + }),//MatY - $rhs_struct_inner: tt,//VecZ - //inner loop shape - $struct_inner: tt,//VecX - $rhs_matrix: tt//MatX - - ) => { - $struct_outer { - $( - $field_outer: $crate::impl_matrix_wide_dot_inner!{ - $lhs, $field_outer, $wide_mul, $rhs, - $struct_inner, //VecX - $struct_inner, //VecX - $rhs_struct_inner, //VecZ - $rhs_matrix //MatX - } - ), + - } - } -} - -// Notes: -// Mat3.dot(Vec2) -> Vec3 -// lhs.dot(rhs) -> out -// lhs = Mat3 -// rhs = Mat4 -// out = Mat3 -// Mat3.dot(Mat4) -> Mat3 -// how to matrix multiply: -// RHS TRANSPOSE -// Mat4 -> Mat2 -// rhs_t = Mat2 -// inner loop: -// out[y][x] = lhs[y].dot(rhs_t[x]) - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_wide_dot { - ( - ($struct_outer: ident { $($field_outer: ident), + }, $vector_outer: ident { $($vector_field_outer: ident), + }, $size_outer: expr), - ($struct_inner: ident { $($field_inner: ident), + }, $matrix_inner: ident { $($matrix_field_inner: ident), + }, $size_inner: expr), - ($rhs_struct_inner: ident { $($rhs_field_inner: ident), + }, $rhs_matrix_inner: ident { $($rhs_matrix_field_inner: ident), + }, $rhs_size_inner: expr), - ($lhs: expr, $rhs: expr) - ) => { - impl $struct_outer<$struct_inner>>{ - paste::item!{ - #[inline] - pub fn [](self,rhs:$matrix_inner<$rhs_struct_inner>>)->$struct_outer<$rhs_struct_inner>>{ - $crate::impl_matrix_wide_dot_outer!( - //constituent idents - self,[],rhs, - //result matrix shape - ($struct_outer { $($field_outer), + }), - ($rhs_struct_inner { $($rhs_field_inner), + }), - //inner loop shape - ($struct_inner { $($field_inner), + }), - ($matrix_inner { $($matrix_field_inner), + }) - ) - } - } - } - } -} - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_wide_dot_shim { - ( - ($outer_info:tt,$inner_info:tt,$rhs_info:tt), - ($lhs: expr, $rhs: expr) - ) => { - $crate::impl_matrix_wide_dot!($outer_info,$inner_info,$rhs_info,($lhs,$rhs)); - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_wide_dot_8x8 { - ( - ($outer_info:tt,$inner_info:tt), - $rhs_info:tt - ) => { - $crate::do_macro_8x8!(impl_matrix_wide_dot_shim,($outer_info,$inner_info,$rhs_info)); - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_wide_dot_repeat_rhs { - ( - ($outer_info:tt,($($rhs_info:tt),+)), - $inner_info:tt - ) => { - $crate::macro_repeated!(impl_matrix_wide_dot_8x8,($outer_info,$inner_info),$($rhs_info),+); - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_wide_matrix_operations_2arg_not_const_generic { - ( - $lhs: expr, $rhs: expr, - ($struct_outer: ident { $($field_outer: ident), + }, $vector_outer: ident { $($vector_field_outer: ident), + }, $size_outer: expr), - ($struct_inner: ident { $($field_inner: ident), + }, $matrix_inner: ident { $($matrix_field_inner: ident), + }, $size_inner: expr) - ) => { - /* TODO: nasty determinant macro - impl,T:Copy+fixed_wide_traits::wide::WideMul> $struct { - #[inline] - pub fn wide_dot(&self) -> U { - $crate::sum_repeating!( - $( + self.$field.wide_mul(self.$field) ) + - ) - } - } - */ - }; -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_wide_matrix_operations_1arg_not_const_generic { - ( - $n: expr, - ($struct_outer: ident { $($field_outer: ident), + }, $vector_outer: ident { $($vector_field_outer: ident), + }, $size_outer: expr), - ) => { - /* TODO: nasty determinant macro - impl,T:Copy+fixed_wide_traits::wide::WideMul> $struct { - #[inline] - pub fn wide_det(&self) -> U { - $crate::sum_repeating!( - $( + self.$field.wide_mul(self.$field) ) + - ) - } - } - */ - }; -} - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! do_macro_4_dumb{ - ( - $macro:ident, - $any:tt - )=>{ - $crate::macro_repeated!($macro, $any, (1,2),(2,4),(3,6),(4,8)); - }; -} - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_wide_3x3_det_not_const_generic { - ( - $n: expr, - $_2n: expr - )=>{ - impl Matrix3>>{ - paste::item!{ - pub fn [](self)->fixed_wide::fixed::Fixed<{$n*3},{$n*3*32}>{ - //[] will not compile, so the doubles are hardcoded above - self.x_axis.[](self.y_axis.[](self.z_axis)) - } - } - } - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_wide_3x3_det_not_const_generic_shim { - ( - (),($n: expr,$_2n: expr) - )=>{ - $crate::impl_matrix_wide_3x3_det_not_const_generic!($n,$_2n); - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_wide_3x3_adjugate_not_const_generic { - ( - (), - $n: expr - )=>{ - impl Matrix3>>{ - paste::item!{ - pub fn [](self)->Matrix3>>{ - Matrix3{ - x_axis:Vector3{x:self.y_axis.y.[](self.z_axis.z)-self.y_axis.z.[](self.z_axis.y),y:self.x_axis.z.[](self.z_axis.y)-self.x_axis.y.[](self.z_axis.z),z:self.x_axis.y.[](self.y_axis.z)-self.x_axis.z.[](self.y_axis.y)}, - y_axis:Vector3{x:self.y_axis.z.[](self.z_axis.x)-self.y_axis.x.[](self.z_axis.z),y:self.x_axis.x.[](self.z_axis.z)-self.x_axis.z.[](self.z_axis.x),z:self.x_axis.z.[](self.y_axis.x)-self.x_axis.x.[](self.y_axis.z)}, - z_axis:Vector3{x:self.y_axis.x.[](self.z_axis.y)-self.y_axis.y.[](self.z_axis.x),y:self.x_axis.y.[](self.z_axis.x)-self.x_axis.x.[](self.z_axis.y),z:self.x_axis.x.[](self.y_axis.y)-self.x_axis.y.[](self.y_axis.x)}, - } - } - } - } - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_wide_3x3 { - ()=>{ - $crate::do_macro_4_dumb!(impl_matrix_wide_3x3_det_not_const_generic_shim,()); - $crate::do_macro_8!(impl_matrix_wide_3x3_adjugate_not_const_generic,()); - } -} - -// HACK: Allows us to sum repeating tokens in macros. -// See: https://stackoverflow.com/a/60187870/17452730 -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! sum_repeating { - ( + $($item: tt) * ) => { - $($item) * - }; -} diff --git a/fixed_wide_vectors/src/macros/matrix.rs b/fixed_wide_vectors/src/macros/matrix.rs index 37b3b99..8b13789 100644 --- a/fixed_wide_vectors/src/macros/matrix.rs +++ b/fixed_wide_vectors/src/macros/matrix.rs @@ -1,201 +1 @@ -// Stolen from https://github.com/c1m50c/fixed-vectors (MIT license) -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix { - ( - ($struct_outer: ident { $($field_outer: ident), + }, $vector_outer: ident { $($vector_field_outer: ident), + }, $size_outer: expr) - ) => { - $crate::impl_common!($struct_outer { $($field_outer), + }, $size_outer); - impl $struct_outer { - #[inline(always)] - pub fn to_vector(self) -> $vector_outer { - $vector_outer { - $( - $vector_field_outer: self.$field_outer - ), + - } - } - } - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_shim { - ( - (), - $matrix_info:tt - ) => { - $crate::impl_matrix!($matrix_info); - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrices { - ( - ($($matrix_info:tt),+), - $vector_infos:tt - ) => { - $crate::macro_repeated!(impl_matrix_shim,(),$($matrix_info),+); - $crate::macro_repeated!(impl_matrix_inner_shim,$vector_infos,$($matrix_info),+); - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_inner_shim { - ( - ($($vector_info:tt),+), - $matrix_info:tt - ) => { - $crate::macro_repeated!(impl_matrix_inner,$matrix_info,$($vector_info),+); - #[cfg(feature="fixed_wide")] - $crate::macro_repeated!(impl_matrix_wide_dot_repeat_rhs,($matrix_info,($($vector_info),+)),$($vector_info),+); - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_inner { - ( - ($struct_outer: ident { $($field_outer: ident), + }, $vector_outer: ident { $($vector_field_outer: ident), + }, $size_outer: expr), - ($struct_inner: ident { $($field_inner: ident), + }, $matrix_inner: ident { $($matrix_field_inner: ident), + }, $size_inner: expr) - ) => { - impl $struct_outer<$struct_inner> { - #[inline(always)] - pub fn to_array_2d(self) -> [[T; $size_inner]; $size_outer] { - [ $(self.$field_outer.to_array()), + ] - } - #[inline] - pub fn map_2d(self, f: F) -> $struct_outer<$struct_inner> - where - F: Fn(T) -> U - { - $crate::matrix_map2d_outer!{f,self,($struct_outer { $($field_outer), + }),($struct_inner { $($field_inner), + })} - } - - #[inline] - pub fn transpose(self) -> $matrix_inner<$vector_outer>{ - $crate::matrix_transpose_outer!{self, - ($matrix_inner { $($matrix_field_inner), + }),($struct_inner { $($field_inner), + }), - ($vector_outer { $($vector_field_outer), + }),($struct_outer { $($field_outer), + }) - } - } - } - - impl $struct_outer<$struct_inner> { - #[inline(always)] - pub const fn from_value_2d(value: T) -> Self { - Self { - $( $field_outer: $struct_inner::from_value(value) ), + - } - } - //TODO: diagonal - } - - // Impl floating-point based methods - //#[cfg(feature="fixed_wide_traits")] - //$crate::impl_wide_matrix_operations!( ($struct_outer { $($field_outer), + }, $size_outer), ($struct_inner, $size_inner), $fields_inner ); - }; -} - - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! matrix_map2d_outer { - ( $f:ident, $value:ident, ($struct_outer: ident { $($field_outer: ident), + }), $unparsed_inner:tt ) => { - $struct_outer { - $( - $field_outer: $crate::matrix_map2d_inner!{$f,$value,$field_outer,$unparsed_inner} - ), + - } - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! matrix_map2d_inner { - ( $f:ident, $value:ident, $field_outer:ident, ($struct_inner: ident { $($field_inner: ident), + }) ) => { - $struct_inner { - $( - $field_inner: $f($value.$field_outer.$field_inner) - ), + - } - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! matrix_transpose_outer { - ( - $value:ident, - ($struct_outer: ident { $($field_outer: ident), + }), - ($old_outer: ident { $($old_field_outer: ident), + }), - $fields_inner:tt, - $old_fields_inner:tt - ) => { - $struct_outer { - $( - $field_outer: $crate::matrix_transpose_inner!{$value,$old_field_outer,$fields_inner,$old_fields_inner} - ), + - } - } -} -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! matrix_transpose_inner { - ( $value:ident, $field_outer:ident, - ($struct_inner: ident { $($field_inner: ident), + }), - ($old_struct_inner: ident { $($old_field_inner: ident), + }) - ) => { - $struct_inner { - $( - $field_inner: $value.$old_field_inner.$field_outer - ), + - } - } -} - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_3x3 { - ()=>{ - #[cfg(feature="fixed_wide")] - $crate::impl_matrix_wide_3x3!(); - } -} - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_matrix_operator { - ( $struct: ident { $($field: ident), + }, $trait: ident, $method: ident, $output: ty ) => { - impl> core::ops::$trait for $struct { - type Output = $output; - - fn $method(self, other: Self) -> Self::Output { - Self { - $( $field: self.$field.$method(other.$field) ), + - } - } - } - impl+Copy> core::ops::$trait for $struct{ - type Output = $output; - - fn $method(self, other: T) -> Self::Output { - $struct { - $( $field: self.$field.$method(other) ), + - } - } - } - }; - - ( $struct: ident { $($field: ident), + }, $trait: ident, $method: ident ) => { - impl core::ops::$trait for $struct { - fn $method(&mut self, other: Self) { - $( self.$field.$method(other.$field) ); + - } - } - - impl core::ops::$trait for $struct { - fn $method(&mut self, other: T) { - $( self.$field.$method(other) ); + - } - } - }; -} diff --git a/fixed_wide_vectors/src/macros/mod.rs b/fixed_wide_vectors/src/macros/mod.rs index 572d35c..30d20cc 100644 --- a/fixed_wide_vectors/src/macros/mod.rs +++ b/fixed_wide_vectors/src/macros/mod.rs @@ -4,17 +4,3 @@ pub mod fixed_wide; pub mod common; pub mod vector; pub mod matrix; - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! macro_repeated{ - ( - $macro:ident, - $any:tt, - $($repeated:tt),* - )=>{ - $( - $crate::$macro!($any, $repeated); - )* - }; -} diff --git a/fixed_wide_vectors/src/macros/vector.rs b/fixed_wide_vectors/src/macros/vector.rs index ad7a72e..8b13789 100644 --- a/fixed_wide_vectors/src/macros/vector.rs +++ b/fixed_wide_vectors/src/macros/vector.rs @@ -1,155 +1 @@ -// Stolen from https://github.com/c1m50c/fixed-vectors (MIT license) -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_vector { - ( $struct: ident { $($field: ident), + }, $size: expr ) => { - $crate::impl_common!($struct { $($field), + }, $size); - impl $struct { - pub fn min(self, rhs: Self) -> $struct { - $struct{ - $( $field: self.$field.min(rhs.$field) ), + - } - } - pub fn max(self, rhs: Self) -> $struct { - $struct{ - $( $field: self.$field.max(rhs.$field) ), + - } - } - pub fn cmp(self, rhs: Self) -> $struct { - $struct{ - $( $field: self.$field.cmp(&rhs.$field) ), + - } - } - pub fn lt(self, rhs: Self) -> $struct { - $struct{ - $( $field: self.$field.lt(&rhs.$field) ), + - } - } - pub fn gt(self, rhs: Self) -> $struct { - $struct{ - $( $field: self.$field.gt(&rhs.$field) ), + - } - } - pub fn ge(self, rhs: Self) -> $struct { - $struct{ - $( $field: self.$field.ge(&rhs.$field) ), + - } - } - pub fn le(self, rhs: Self) -> $struct { - $struct{ - $( $field: self.$field.le(&rhs.$field) ), + - } - } - } - - impl $struct{ - pub fn all(&self)->bool{ - const ALL:[bool;$size]=[true;$size]; - core::matches!(self.to_array(),ALL) - } - pub fn any(&self)->bool{ - $( self.$field )|| + - } - } - - impl> core::ops::Neg for $struct { - type Output = Self; - - fn neg(self) -> Self::Output { - Self { - $( $field: -self.$field ), + - } - } - } - - // Impl arithmetic pperators - $crate::impl_vector_operator!( $struct { $($field), + }, AddAssign, add_assign ); - $crate::impl_vector_operator!( $struct { $($field), + }, Add, add, Self ); - $crate::impl_vector_operator!( $struct { $($field), + }, SubAssign, sub_assign ); - $crate::impl_vector_operator!( $struct { $($field), + }, Sub, sub, Self ); - $crate::impl_vector_operator!( $struct { $($field), + }, MulAssign, mul_assign ); - $crate::impl_vector_operator!( $struct { $($field), + }, Mul, mul, Self ); - $crate::impl_vector_operator!( $struct { $($field), + }, DivAssign, div_assign ); - $crate::impl_vector_operator!( $struct { $($field), + }, Div, div, Self ); - $crate::impl_vector_operator!( $struct { $($field), + }, RemAssign, rem_assign ); - $crate::impl_vector_operator!( $struct { $($field), + }, Rem, rem, Self ); - - // Impl bitwise operators - $crate::impl_vector_operator!( $struct { $($field), + }, BitAndAssign, bitand_assign ); - $crate::impl_vector_operator!( $struct { $($field), + }, BitAnd, bitand, Self ); - $crate::impl_vector_operator!( $struct { $($field), + }, BitOrAssign, bitor_assign ); - $crate::impl_vector_operator!( $struct { $($field), + }, BitOr, bitor, Self ); - $crate::impl_vector_operator!( $struct { $($field), + }, BitXorAssign, bitxor_assign ); - $crate::impl_vector_operator!( $struct { $($field), + }, BitXor, bitxor, Self ); - - // Impl floating-point based methods - #[cfg(feature="fixed_wide")] - $crate::impl_wide_vector_operations!( $struct { $($field), + }, $size ); - }; -} - - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_extend { - ( $struct: ident { $($field: ident), + }, $struct_extended: ident, $field_extended: ident ) => { - impl $struct { - #[inline(always)] - pub fn extend(self,value:T) -> $struct_extended { - $struct_extended { - $( $field:self.$field, ) + - $field_extended:value - } - } - } - }; -} - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_vector_operator { - ( $struct: ident { $($field: ident), + }, $trait: ident, $method: ident, $output: ty ) => { - impl> core::ops::$trait for $struct { - type Output = $output; - - fn $method(self, other: Self) -> Self::Output { - Self { - $( $field: self.$field.$method(other.$field) ), + - } - } - } - impl+Copy> core::ops::$trait for $struct{ - type Output = $output; - - fn $method(self, other: T) -> Self::Output { - $struct { - $( $field: self.$field.$method(other) ), + - } - } - } - }; - - ( $struct: ident { $($field: ident), + }, $trait: ident, $method: ident ) => { - impl core::ops::$trait for $struct { - fn $method(&mut self, other: Self) { - $( self.$field.$method(other.$field) ); + - } - } - - impl core::ops::$trait for $struct { - fn $method(&mut self, other: T) { - $( self.$field.$method(other) ); + - } - } - }; -} - -#[doc(hidden)] -#[macro_export(local_inner_macros)] -macro_rules! impl_vector_3 { - ()=>{ - #[cfg(feature="fixed_wide")] - $crate::impl_vector_wide_3!(); - } -} diff --git a/fixed_wide_vectors/src/matrix.rs b/fixed_wide_vectors/src/matrix.rs index 4966f28..41579df 100644 --- a/fixed_wide_vectors/src/matrix.rs +++ b/fixed_wide_vectors/src/matrix.rs @@ -16,22 +16,18 @@ pub struct Matrix4 { pub w_axis: T, } -crate::impl_extend!(Matrix2 { x_axis, y_axis }, Matrix3, z_axis); -crate::impl_extend!(Matrix3 { x_axis, y_axis, z_axis }, Matrix4, w_axis); -//TODO: extend vertically - -crate::impl_matrices!( - //outer struct and equivalent vector +crate::impl_matrix_named_fields!( + //outer struct ( - (Matrix2 { x_axis, y_axis }, Vector2 { x, y }, 2), - (Matrix3 { x_axis, y_axis, z_axis }, Vector3 { x, y, z }, 3), - (Matrix4 { x_axis, y_axis, z_axis, w_axis }, Vector4 { x, y, z, w }, 4) + (Matrix2 { x_axis, y_axis }, 2), + (Matrix3 { x_axis, y_axis, z_axis }, 3), + (Matrix4 { x_axis, y_axis, z_axis, w_axis }, 4) ), - //inner struct and equivalent matrix + //inner struct ( - (Vector2 { x, y }, Matrix2 { x_axis, y_axis }, 2), - (Vector3 { x, y, z }, Matrix3 { x_axis, y_axis, z_axis }, 3), - (Vector4 { x, y, z, w }, Matrix4 { x_axis, y_axis, z_axis, w_axis }, 4) + (Vector2 { x, y }, 2), + (Vector3 { x, y, z }, 3), + (Vector4 { x, y, z, w }, 4) ) ); diff --git a/fixed_wide_vectors/src/vector.rs b/fixed_wide_vectors/src/vector.rs index 5016c97..12ed55a 100644 --- a/fixed_wide_vectors/src/vector.rs +++ b/fixed_wide_vectors/src/vector.rs @@ -1,60 +1,14 @@ -// Stolen from https://github.com/c1m50c/fixed-vectors (MIT license) - -/// Vector for holding two-dimensional values. -/// -/// # Example -/// -/// ``` -/// use fixed_wide_vectors::Vector2; -/// -/// let mut vec2 = Vector2::new(1, 2); -/// vec2 += Vector2::new(1, 2); -/// -/// assert_eq!(vec2.x, 2); -/// assert_eq!(vec2.y, 4); -/// ``` pub struct Vector2 { pub x: T, pub y: T, } - -/// Vector for holding three-dimensional values. -/// -/// # Example -/// -/// ``` -/// use fixed_wide_vectors::Vector3; -/// -/// let mut vec3 = Vector3::new(1, 2, 3); -/// vec3 += Vector3::new(1, 2, 3); -/// -/// assert_eq!(vec3.x, 2); -/// assert_eq!(vec3.y, 4); -/// assert_eq!(vec3.z, 6); -/// ``` pub struct Vector3 { pub x: T, pub y: T, pub z: T, } - -/// Vector for holding four-dimensional values. -/// -/// # Example -/// -/// ``` -/// use fixed_wide_vectors::Vector4; -/// -/// let mut vec4 = Vector4::new(1, 2, 3, 4); -/// vec4 += Vector4::new(1, 2, 3, 4); -/// -/// assert_eq!(vec4.x, 2); -/// assert_eq!(vec4.y, 4); -/// assert_eq!(vec4.z, 6); -/// assert_eq!(vec4.w, 8); -/// ``` pub struct Vector4 { pub x: T, pub y: T, @@ -62,23 +16,9 @@ pub struct Vector4 { pub w: T, } - -crate::impl_vector!(Vector2 { x, y }, 2); -crate::impl_vector!(Vector3 { x, y, z }, 3); -crate::impl_vector!(Vector4 { x, y, z, w }, 4); - -crate::impl_extend!(Vector2 { x, y }, Vector3, z); -crate::impl_extend!(Vector3 { x, y, z }, Vector4, w); - -crate::impl_matrix_inner!((Vector2 { x, y }, Vector2 { x, y }, 2), (Vector2 { x, y }, Vector2 { x, y }, 2) ); -crate::impl_matrix_inner!((Vector2 { x, y }, Vector2 { x, y }, 2), (Vector3 { x, y, z }, Vector3 { x, y, z }, 3) ); -crate::impl_matrix_inner!((Vector2 { x, y }, Vector2 { x, y }, 2), (Vector4 { x, y, z, w }, Vector4 { x, y, z, w }, 4) ); -crate::impl_matrix_inner!((Vector3 { x, y, z }, Vector3 { x, y, z }, 3), (Vector2 { x, y }, Vector2 { x, y }, 2) ); -crate::impl_matrix_inner!((Vector3 { x, y, z }, Vector3 { x, y, z }, 3), (Vector3 { x, y, z }, Vector3 { x, y, z }, 3) ); -crate::impl_matrix_inner!((Vector3 { x, y, z }, Vector3 { x, y, z }, 3), (Vector4 { x, y, z, w }, Vector4 { x, y, z, w }, 4) ); -crate::impl_matrix_inner!((Vector4 { x, y, z, w }, Vector4 { x, y, z, w }, 4), (Vector2 { x, y }, Vector2 { x, y }, 2) ); -crate::impl_matrix_inner!((Vector4 { x, y, z, w }, Vector4 { x, y, z, w }, 4), (Vector3 { x, y, z }, Vector3 { x, y, z }, 3) ); -crate::impl_matrix_inner!((Vector4 { x, y, z, w }, Vector4 { x, y, z, w }, 4), (Vector4 { x, y, z, w }, Vector4 { x, y, z, w }, 4) ); +crate::impl_vector_named_fields!(Vector2 { x, y }, 2); +crate::impl_vector_named_fields!(Vector3 { x, y, z }, 3); +crate::impl_vector_named_fields!(Vector4 { x, y, z, w }, 4); //cross product crate::impl_vector_3!();