use crate::traversals::{Traversal, TraversalOver, TraversalTraverse}; #[derive(Clone, Copy)] pub struct EachInner; #[allow(non_upper_case_globals)] pub const each: Traversal = Traversal(EachInner); impl TraversalTraverse> for EachInner { type Field = T; fn traverse(&self, thing: Vec) -> Vec { thing } } impl TraversalOver> for EachInner { fn over(&self, thing: Vec, f: F) -> Vec where F: FnMut(Self::Field) -> Self::Field, { thing.into_iter().map(f).collect() } } // TODO i'd like to have this so we get it for free on any iterable // problem is, arrays/tuples don't implement FromIter // and having both the blanket implementation and the one below complains cause // other crates could add the trait in the future // impl TraversalTraverse for EachInner // where // I: IntoIterator, // { // type Field = T; // fn traverse(&self, thing: I) -> Vec { // thing.into_iter().collect() // } // } // impl TraversalOver for EachInner // where // I: IntoIterator + FromIterator, // { // fn over(&self, thing: I, f: F) -> I // where // F: FnMut(Self::Field) -> Self::Field, // { // thing.into_iter().map(f).collect() // } // } macro_rules! make_tuples { ($f:ident, ( $( $v:ident ),* ), ( $( $t:ident ),* ) ) => { impl TraversalTraverse<( $($t,)* )> for EachInner { type Field = T; fn traverse(&self, ( $($v,)* ): ($($t,)*)) -> Vec { vec![ $($v,)* ] } } impl TraversalOver<( $($t,)* )> for EachInner { fn over( &self, ($($v,)*): ($($t,)*), mut f: F ) -> ( $($t,)* ) where F: FnMut(Self::Field) -> Self::Field { ( $( f($v), )* ) } } impl<'a, T> TraversalTraverse<&'a ( $($t,)* )> for EachInner { type Field = &'a T; fn traverse(&self, ( $($v,)* ): &'a ($($t,)*)) -> Vec { vec![ $($v,)* ] } } impl<'a, T> TraversalTraverse<&'a mut ( $($t,)* )> for EachInner { type Field = &'a mut T; fn traverse(&self, ( $($v,)* ): &'a mut ($($t,)*)) -> Vec { vec![ $($v,)* ] } } }; } make_tuples!(t, (t), (T)); make_tuples!(t, (t, u), (T, T)); make_tuples!(t, (t, u, v), (T, T, T)); make_tuples!(t, (t, u, v, w), (T, T, T, T)); make_tuples!(t, (t, u, v, w, x), (T, T, T, T, T)); make_tuples!(t, (t, u, v, w, x, y), (T, T, T, T, T, T)); make_tuples!(t, (t, u, v, w, x, y, z), (T, T, T, T, T, T, T)); // not doing more cause i'm lazy, open a pr if you need more :) macro_rules! make_arrays { ($f:ident, $n:expr, [$( $v:ident ),*]) => { impl TraversalTraverse<[T; $n]> for EachInner { type Field = T; fn traverse(&self, [ $($v,)* ]: [T; $n]) -> Vec { vec![ $($v,)* ] } } impl TraversalOver<[T; $n]> for EachInner { fn over( &self, [ $($v,)* ]: [T; $n], mut fun: F ) -> [T; $n] where F: FnMut(Self::Field) -> Self::Field { [$(fun($v),)*] } } impl<'a, T> TraversalTraverse<&'a [T; $n]> for EachInner { type Field = &'a T; fn traverse(&self, [ $($v,)* ]: &'a [T; $n]) -> Vec { vec![ $($v,)* ] } } impl<'a, T> TraversalTraverse<&'a mut [T; $n]> for EachInner { type Field = &'a mut T; fn traverse(&self, [ $($v,)* ]: &'a mut [T; $n]) -> Vec { vec![ $($v,)* ] } } }; } make_arrays!(t, 1, [t]); make_arrays!(t, 2, [t, _a]); make_arrays!(t, 3, [t, _a, _b]); make_arrays!(t, 4, [t, _a, _b, _c]); make_arrays!(t, 5, [t, _a, _b, _c, _d]); make_arrays!(t, 6, [t, _a, _b, _c, _d, _e]); make_arrays!(t, 7, [t, _a, _b, _c, _d, _e, _g]);