diff --git a/fixed_wide_vectors/src/macros/matrix.rs b/fixed_wide_vectors/src/macros/matrix.rs index eaa507c..f3ebce4 100644 --- a/fixed_wide_vectors/src/macros/matrix.rs +++ b/fixed_wide_vectors/src/macros/matrix.rs @@ -73,6 +73,26 @@ macro_rules! impl_matrix { ), + } } + + /// Consumes the matrix and returns a new matrix with the given function applied on each field. + /// + /// # Example + /// + /// ``` + /// use fixed_wide_vectors::Vector2; + /// + /// let mat2 = Vector2::new( + /// Vector2::new(1, 2), + /// Vector2::new(3, 4) + /// ) + /// .transpose(); + /// + /// assert_eq!(mat2, Vector2::new(Vector2::new(1, 3), Vector2::new(2, 4))); + /// ``` + #[inline] + pub fn transpose(self) -> $struct_inner<$struct_outer>{ + $crate::matrix_transpose_outer!{self,$fields_inner,($struct_outer { $($field_outer), + })} + } } impl $struct_outer<$struct_inner> { @@ -114,6 +134,32 @@ macro_rules! matrix_map2d_inner { } } } +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! matrix_transpose_outer { + ( + $value:ident, + ($struct_outer: ident { $($field_outer: ident), + }), + $fields_inner:tt + ) => { + $struct_outer { + $( + $field_outer: $crate::matrix_transpose_inner!{$value,$field_outer,$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), + }) ) => { + $struct_inner { + $( + $field_inner: $value.$field_inner.$field_outer + ), + + } + } +} /* macro_rules! nested {