[no description]
generalize over ref
| -rw-r--r-- | src/iterator.rs | 133 | ||||
| -rw-r--r-- | src/lib.rs | 6 |
2 files changed, 85 insertions, 54 deletions
diff --git a/src/iterator.rs b/src/iterator.rs index a38e8db..f288943 100644 --- a/src/iterator.rs +++ b/src/iterator.rs @@ -74,34 +74,6 @@ pub trait IteratorOfTuplesWithF<retval>: self.on().flat_map(f) } } -pub trait IteratorOfReferencesToTupleWithF<'a, T: Tupl + 'a, retval>: - Iterator<Item = &'a T> + Sized -{ - /// Map the item at: (|(x, y)| (f(x), y)) - fn map_at<const N: usize>( - self, - f: impl FnMut(<T::AsRef<'a> as Pick<N>>::At) -> retval + 'a, - ) -> impl Iterator<Item = <T::AsRef<'a> as RePick<N, retval>>::New> - where - T::AsRef<'a>: RePick<N, retval>, - { - self.as_ref().map_at(f) - } - /// map the item to just one, then map that one: (|x| f(x.0)) - fn map_on<const N: usize, U>( - self, - f: impl FnMut(<T::AsRef<'a> as Pick<N>>::At) -> U, - ) -> core::iter::Map< - impl Iterator<Item = <T::AsRef<'a> as Pick<N>>::At>, - impl FnMut(<T::AsRef<'a> as Pick<N>>::At) -> U, - > - where - T::AsRef<'a>: Pick<N>, - { - self.on().map(f) - } -} - /// may add more later pub trait IteratorOfTuples: Iterator<Item: Tupl> + Sized { /// make it just one: (|x| x.0) @@ -145,47 +117,104 @@ pub trait IteratorOfTuples: Iterator<Item: Tupl> + Sized { } } -/// may add more later -pub trait IteratorOfReferencesToTuple<'a, T: Tupl + 'a>: - Iterator<Item = &'a T> + Sized -{ - fn as_ref(self) -> core::iter::Map<Self, fn(&T) -> <T>::AsRef<'_>> { - self.map(T::as_ref) - } +impl<I: Iterator<Item: Tupl>> IteratorOfTuples for I {} +impl<U, I: Iterator<Item: Tupl>> IteratorOfTuplesWithF<U> for I {} - /// make it just one: (|x| x.0) - fn on<const N: usize>( +macro_rules! ref_traits { + ($name1: ident, $name2: ident, $ref:ident, $convert:ident $(, $ref_mut:tt )?) => { + /// may add more later + /// Regular + pub trait $name1<'a, T: Tupl + 'a>: + Iterator<Item = &'a $($ref_mut)? T> + Sized + { + fn $convert( + self, + ) -> core::iter::Map<Self, fn(& $($ref_mut)? T) -> <T>::$ref<'_>> { + self.map(T::$convert) + } + + /// make it just one: (|x| x.0) + fn on<const N: usize>( + self, + ) -> impl Iterator<Item = <T::$ref<'a> as Pick<N>>::At> + where + T::$ref<'a>: Pick<N>, + { + self.$convert().on() + } + fn map_all<retval: Tupl, functions: Tupl>( + self, + mut fns: functions, + ) -> impl Iterator<Item = retval> + where + T::$ref<'a>: MapAllMut<retval, functions>, + { + self.map(move |x| x.$convert().map_all_mut(&mut fns)) + } + + fn for_all<const N: usize>(self, f: impl FnMut(<T::$ref<'a> as Pick<N>>::At)) + where + T::$ref<'a>: Pick<N> { + self.on().for_each(f) + } + } +/// With f +pub trait $name2<'a, T: Tupl + 'a, retval>: + Iterator<Item = &'a $($ref_mut)? T> + Sized +{ + /// Map the item at: (|(x, y)| (f(x), y)) + fn map_at<const N: usize>( self, - ) -> impl Iterator<Item = <T::AsRef<'a> as Pick<N>>::At> + f: impl FnMut(<T::$ref<'a> as Pick<N>>::At) -> retval + 'a, + ) -> impl Iterator<Item = <T::$ref<'a> as RePick<N, retval>>::New> where - T::AsRef<'a>: Pick<N>, + T::$ref<'a>: RePick<N, retval>, { - self.as_ref().on() + self.$convert().map_at(f) } - fn map_all<retval: Tupl, functions: Tupl>( + /// map the item to just one, then map that one: (|x| f(x.0)) + fn map_on<const N: usize, U>( self, - mut fns: functions, - ) -> impl Iterator<Item = retval> + f: impl FnMut(<T::$ref<'a> as Pick<N>>::At) -> U, + ) -> core::iter::Map< + impl Iterator<Item = <T::$ref<'a> as Pick<N>>::At>, + impl FnMut(<T::$ref<'a> as Pick<N>>::At) -> U, + > where - T::AsRef<'a>: MapAllMut<retval, functions>, + T::$ref<'a>: Pick<N>, { - self.map(move |x| x.as_ref().map_all_mut(&mut fns)) + self.on().map(f) } -} -impl<I: Iterator<Item: Tupl>> IteratorOfTuples for I {} +} -impl<U, I: Iterator<Item: Tupl>> IteratorOfTuplesWithF<U> for I {} -impl<'a, T: Tupl + 'a, I: Iterator<Item = &'a T>> - IteratorOfReferencesToTuple<'a, T> for I +impl<'a, T: Tupl + 'a, I: Iterator<Item = &'a $($ref_mut)? T>> + $name1<'a, T> for I { } -impl<'a, U, T: Tupl + 'a, I: Iterator<Item = &'a T>> - IteratorOfReferencesToTupleWithF<'a, T, U> for I +impl<'a, U, T: Tupl + 'a, I: Iterator<Item = &'a $($ref_mut)? T>> + $name2<'a, T, U> for I { } + }; + +} +ref_traits!( + IteratorOfReferencesToTuple, + IteratorOfReferencesToTupleWithF, + AsRef, + as_ref +); +ref_traits!( + IteratorOfMutableReferencesToTuple, + IteratorOfMutableReferencesToTupleWithF, + AsMut, + as_mut, + mut +); + #[test] fn x() { let x = [(1, 2), (3, 4)].iter().map_at::<0>(|x| x + 2); @@ -13,8 +13,10 @@ mod implementations; mod iterator; mod picks; pub use iterator::{ - IteratorOfReferencesToTuple, IteratorOfReferencesToTupleWithF, - IteratorOfTuples, IteratorOfTuplesWithF, + IteratorOfMutableReferencesToTuple, + IteratorOfMutableReferencesToTupleWithF, IteratorOfReferencesToTuple, + IteratorOfReferencesToTupleWithF, IteratorOfTuples, + IteratorOfTuplesWithF, }; /// Reverse a tuple. pub trait Reverse: Tupl { |