|
| 1 | +//! Map heterogeneous tuples to homogeneous tuples by arg |
| 2 | +//! |
| 3 | +//! ## Example |
| 4 | +//! |
| 5 | +//! - |
| 6 | +//! ```rust |
| 7 | +//! # use tuples::*; |
| 8 | +//! let a = (1, 2, 3); |
| 9 | +//! let r = a.uniform_map_by(&0, |x: &i32, a: i32| a > *x); |
| 10 | +//! assert_eq!(r, (true, true, true)) |
| 11 | +//! ``` |
| 12 | +//! - |
| 13 | +//! ```rust |
| 14 | +//! # use tuples::*; |
| 15 | +//! let a = (1, 'c'); |
| 16 | +//! let r = a.uniform_map_by(&0, (|x: &i32, a: i32| a > *x, |x: &i32, b: char| b as i32 > *x)); |
| 17 | +//! assert_eq!(r, (true, true)) |
| 18 | +//! ``` |
| 19 | +//! - |
| 20 | +//! ```rust |
| 21 | +//! # use tuples::*; |
| 22 | +//! let a = (1, 'c'); |
| 23 | +//! let r = a.uniform_map_by(&mut 0, (|x: &mut i32, a: i32| a > *x, |x: &mut i32, b: char| b as i32 > *x)); |
| 24 | +//! assert_eq!(r, (true, true)) |
| 25 | +//! ``` |
| 26 | +//! |
| 27 | +
|
| 28 | +use crate::param::Param; |
| 29 | + |
| 30 | +/// Map heterogeneous tuples to homogeneous tuples by arg |
| 31 | +pub trait TupleUniformMapperBy<Input, Target, Arg> { |
| 32 | + type Output; |
| 33 | + |
| 34 | + /// Map heterogeneous tuples to homogeneous tuples by arg |
| 35 | + fn apply_uniform_map_by(self, arg: Arg, input: Input) -> Self::Output; |
| 36 | +} |
| 37 | + |
| 38 | +/// Map heterogeneous tuples to homogeneous tuples by arg |
| 39 | +pub trait TupleUniformMapBy<Target, Arg, Mapper: TupleUniformMapperBy<Self, Target, Arg>>: Sized { |
| 40 | + /// Map heterogeneous tuples to homogeneous tuples by arg |
| 41 | + fn uniform_map_by(self, arg: Arg, mapper: Mapper) -> <Mapper as TupleUniformMapperBy<Self, Target, Arg>>::Output; |
| 42 | +} |
| 43 | + |
| 44 | +impl<Tuple, Target, Arg, Mapper: TupleUniformMapperBy<Tuple, Target, Arg>> TupleUniformMapBy<Target, Arg, Mapper> for Tuple { |
| 45 | + fn uniform_map_by(self, arg: Arg, mapper: Mapper) -> <Mapper as TupleUniformMapperBy<Self, Target, Arg>>::Output { |
| 46 | + mapper.apply_uniform_map_by(arg, self) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl<A, T, U, F: FnOnce(A, T) -> U> TupleUniformMapperBy<(T,), U, A> for F { |
| 51 | + type Output = (U,); |
| 52 | + |
| 53 | + fn apply_uniform_map_by(self, arg: A, input: (T,)) -> Self::Output { |
| 54 | + ((self)(arg, input.0),) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl<A, T, U, F: FnOnce(A, &T) -> U> TupleUniformMapperBy<&(T,), U, A> for F { |
| 59 | + type Output = (U,); |
| 60 | + |
| 61 | + fn apply_uniform_map_by(self, arg: A, input: &(T,)) -> Self::Output { |
| 62 | + ((self)(arg, &input.0),) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl<A, T, U, F: FnOnce(A, &mut T) -> U> TupleUniformMapperBy<&mut (T,), U, A> for F { |
| 67 | + type Output = (U,); |
| 68 | + |
| 69 | + fn apply_uniform_map_by(self, arg: A, input: &mut (T,)) -> Self::Output { |
| 70 | + ((self)(arg, &mut input.0),) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl<A, T, U, F: FnOnce(A, T) -> U> TupleUniformMapperBy<(T,), U, A> for (F,) { |
| 75 | + type Output = (U,); |
| 76 | + |
| 77 | + fn apply_uniform_map_by(self, arg: A, input: (T,)) -> Self::Output { |
| 78 | + ((self.0)(arg, input.0),) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl<A, T, U, F: FnMut(A, T) -> U> TupleUniformMapperBy<(T,), U, A> for &mut (F,) { |
| 83 | + type Output = (U,); |
| 84 | + |
| 85 | + fn apply_uniform_map_by(self, arg: A, input: (T,)) -> Self::Output { |
| 86 | + ((self.0)(arg, input.0),) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl<A, T, U, F: Fn(A, T) -> U> TupleUniformMapperBy<(T,), U, A> for &(F,) { |
| 91 | + type Output = (U,); |
| 92 | + |
| 93 | + fn apply_uniform_map_by(self, arg: A, input: (T,)) -> Self::Output { |
| 94 | + ((self.0)(arg, input.0),) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl<A, T, U, F: FnOnce(A, &T) -> U> TupleUniformMapperBy<&(T,), U, A> for (F,) { |
| 99 | + type Output = (U,); |
| 100 | + |
| 101 | + fn apply_uniform_map_by(self, arg: A, input: &(T,)) -> Self::Output { |
| 102 | + ((self.0)(arg, &input.0),) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl<A, T, U, F: FnMut(A, &T) -> U> TupleUniformMapperBy<&(T,), U, A> for &mut (F,) { |
| 107 | + type Output = (U,); |
| 108 | + |
| 109 | + fn apply_uniform_map_by(self, arg: A, input: &(T,)) -> Self::Output { |
| 110 | + ((self.0)(arg, &input.0),) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +impl<A, T, U, F: Fn(A, &T) -> U> TupleUniformMapperBy<&(T,), U, A> for &(F,) { |
| 115 | + type Output = (U,); |
| 116 | + |
| 117 | + fn apply_uniform_map_by(self, arg: A, input: &(T,)) -> Self::Output { |
| 118 | + ((self.0)(arg, &input.0),) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +impl<A, T, U, F: FnOnce(A, &mut T) -> U> TupleUniformMapperBy<&mut (T,), U, A> for (F,) { |
| 123 | + type Output = (U,); |
| 124 | + |
| 125 | + fn apply_uniform_map_by(self, arg: A, input: &mut (T,)) -> Self::Output { |
| 126 | + ((self.0)(arg, &mut input.0),) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl<A, T, U, F: FnMut(A, &mut T) -> U> TupleUniformMapperBy<&mut (T,), U, A> for &mut (F,) { |
| 131 | + type Output = (U,); |
| 132 | + |
| 133 | + fn apply_uniform_map_by(self, arg: A, input: &mut (T,)) -> Self::Output { |
| 134 | + ((self.0)(arg, &mut input.0),) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +impl<A, T, U, F: Fn(A, &mut T) -> U> TupleUniformMapperBy<&mut (T,), U, A> for &(F,) { |
| 139 | + type Output = (U,); |
| 140 | + |
| 141 | + fn apply_uniform_map_by(self, arg: A, input: &mut (T,)) -> Self::Output { |
| 142 | + ((self.0)(arg, &mut input.0),) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +include!("./gen/uniform_map_by.rs"); |
| 147 | + |
| 148 | +#[cfg(test)] |
| 149 | +mod tests { |
| 150 | + use super::*; |
| 151 | + |
| 152 | + #[test] |
| 153 | + fn test0() { |
| 154 | + let a = (1, 2, 3); |
| 155 | + let r = a.uniform_map_by(&0, |x: &i32, a: i32| a > *x); |
| 156 | + assert_eq!(r, (true, true, true)) |
| 157 | + } |
| 158 | + |
| 159 | + #[test] |
| 160 | + fn test1() { |
| 161 | + let a = (1, 'c'); |
| 162 | + let r = a.uniform_map_by(&0, (|x: &i32, a: i32| a > *x, |x: &i32, b: char| b as i32 > *x)); |
| 163 | + assert_eq!(r, (true, true)) |
| 164 | + } |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn test2() { |
| 168 | + let a = (1, 'c'); |
| 169 | + let r = a.uniform_map_by(&mut 0, (|x: &mut i32, a: i32| a > *x, |x: &mut i32, b: char| b as i32 > *x)); |
| 170 | + assert_eq!(r, (true, true)) |
| 171 | + } |
| 172 | +} |
0 commit comments