Skip to content

Commit 9b238aa

Browse files
committed
uniform map by
1 parent 08bb66f commit 9b238aa

8 files changed

Lines changed: 4237 additions & 492 deletions

File tree

code_gen/src/code_gen.rs

Lines changed: 289 additions & 97 deletions
Large diffs are not rendered by default.

tuples/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license = "MIT"
88
name = "tuples"
99
readme = "../README.md"
1010
repository = "https://github.com/libsugar/tuplers"
11-
version = "1.18.0"
11+
version = "1.19.0"
1212

1313
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1414

@@ -43,6 +43,7 @@ default = [
4343
"combinations",
4444
"afn",
4545
"uniform_map",
46+
"uniform_map_by",
4647
]
4748
flatten = []
4849
permutations = []
@@ -64,6 +65,7 @@ tuple_map = []
6465
tuple_meta = []
6566
tuple_swap_n = []
6667
uniform_map = []
68+
uniform_map_by = []
6769

6870
[package.metadata.docs.rs]
6971
all-features = true

tuples/src/gen/uniform_map.rs

Lines changed: 372 additions & 372 deletions
Large diffs are not rendered by default.

tuples/src/gen/uniform_map_by.rs

Lines changed: 3350 additions & 0 deletions
Large diffs are not rendered by default.

tuples/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,11 @@ pub mod afn;
224224
pub mod uniform_map;
225225
#[cfg(any(all(feature = "uniform_map", feature = "re-exports"), test, doc))]
226226
pub use uniform_map::*;
227+
228+
#[cfg(any(feature = "uniform_map_by", test, doc))]
229+
pub mod uniform_map_by;
230+
#[cfg(any(all(feature = "uniform_map_by", feature = "re-exports"), test, doc))]
231+
pub use uniform_map_by::*;
232+
233+
#[cfg(any(feature = "uniform_map_by", test, doc))]
234+
mod param;

tuples/src/param.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pub trait Param<'s, S = &'s Self> {
2+
type Target;
3+
4+
fn pass(&'s mut self) -> Self::Target;
5+
}
6+
7+
impl<'s, 'a, T> Param<'s> for &'a T {
8+
type Target = &'s T;
9+
10+
fn pass(&'s mut self) -> Self::Target {
11+
*self
12+
}
13+
}
14+
15+
impl<'s, 'a, T> Param<'s> for &'a mut T {
16+
type Target = &'s mut T;
17+
18+
fn pass(&'s mut self) -> Self::Target {
19+
*self
20+
}
21+
}

tuples/src/uniform_map.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
//! Map tuples to a single type
2-
//!
1+
//! Map heterogeneous tuples to homogeneous tuples.
2+
//!
33
//! ## Example
4-
//!
5-
//! -
4+
//!
5+
//! -
66
//! ```rust
77
//! # use tuples::*;
88
//! let a = (1, 2, 3);
99
//! let r = a.uniform_map(|a| a > 0);
1010
//! assert_eq!(r, (true, true, true))
1111
//! ```
12-
//! -
12+
//! -
1313
//! ```rust
1414
//! # use tuples::*;
1515
//! let a = (1, 'c', true);
@@ -23,19 +23,19 @@
2323
//! let r = (&a).uniform_map((|a: &i32| *a > 0, |b: &char| b.is_ascii(), |c: &bool| *c));
2424
//! assert_eq!(r, (true, true, true))
2525
//! ```
26-
//!
26+
//!
2727
28-
/// Map tuples to a single type
28+
/// Map heterogeneous tuples to homogeneous tuples.
2929
pub trait TupleUniformMapper<Input, Target> {
3030
type Output;
3131

32-
/// Map tuples to a single type
32+
/// Map heterogeneous tuples to homogeneous tuples.
3333
fn apply_uniform_map(self, input: Input) -> Self::Output;
3434
}
3535

36-
/// Map tuples to a single type
36+
/// Map heterogeneous tuples to homogeneous tuples.
3737
pub trait TupleUniformMap<Target, Mapper: TupleUniformMapper<Self, Target>>: Sized {
38-
/// Map tuples to a single type
38+
/// Map heterogeneous tuples to homogeneous tuples.
3939
fn uniform_map(self, mapper: Mapper) -> <Mapper as TupleUniformMapper<Self, Target>>::Output;
4040
}
4141

@@ -45,95 +45,95 @@ impl<Tuple, Target, Mapper: TupleUniformMapper<Tuple, Target>> TupleUniformMap<T
4545
}
4646
}
4747

48-
impl<F: FnOnce(T) -> U, U, T> TupleUniformMapper<(T,), U> for F {
48+
impl<U, T, F: FnOnce(T) -> U> TupleUniformMapper<(T,), U> for F {
4949
type Output = (U,);
5050

5151
fn apply_uniform_map(self, input: (T,)) -> Self::Output {
5252
((self)(input.0),)
5353
}
5454
}
5555

56-
impl<F: FnOnce(&T) -> U, U, T> TupleUniformMapper<&(T,), U> for F {
56+
impl<U, T, F: FnOnce(&T) -> U> TupleUniformMapper<&(T,), U> for F {
5757
type Output = (U,);
5858

5959
fn apply_uniform_map(self, input: &(T,)) -> Self::Output {
6060
((self)(&input.0),)
6161
}
6262
}
6363

64-
impl<F: FnOnce(&mut T) -> U, U, T> TupleUniformMapper<&mut (T,), U> for F {
64+
impl<U, T, F: FnOnce(&mut T) -> U> TupleUniformMapper<&mut (T,), U> for F {
6565
type Output = (U,);
6666

6767
fn apply_uniform_map(self, input: &mut (T,)) -> Self::Output {
6868
((self)(&mut input.0),)
6969
}
7070
}
7171

72-
impl<F: FnOnce(T) -> U, U, T> TupleUniformMapper<(T,), U> for (F,) {
72+
impl<U, T, F: FnOnce(T) -> U> TupleUniformMapper<(T,), U> for (F,) {
7373
type Output = (U,);
7474

7575
fn apply_uniform_map(self, input: (T,)) -> Self::Output {
7676
((self.0)(input.0),)
7777
}
7878
}
7979

80-
impl<F: FnMut(T) -> U, U, T> TupleUniformMapper<(T,), U> for &mut (F,) {
80+
impl<U, T, F: FnMut(T) -> U> TupleUniformMapper<(T,), U> for &mut (F,) {
8181
type Output = (U,);
8282

8383
fn apply_uniform_map(self, input: (T,)) -> Self::Output {
8484
((self.0)(input.0),)
8585
}
8686
}
8787

88-
impl<F: Fn(T) -> U, U, T> TupleUniformMapper<(T,), U> for &(F,) {
88+
impl<U, T, F: Fn(T) -> U> TupleUniformMapper<(T,), U> for &(F,) {
8989
type Output = (U,);
9090

9191
fn apply_uniform_map(self, input: (T,)) -> Self::Output {
9292
((self.0)(input.0),)
9393
}
9494
}
9595

96-
impl<F: FnOnce(&T) -> U, U, T> TupleUniformMapper<&(T,), U> for (F,) {
96+
impl<U, T, F: FnOnce(&T) -> U> TupleUniformMapper<&(T,), U> for (F,) {
9797
type Output = (U,);
9898

9999
fn apply_uniform_map(self, input: &(T,)) -> Self::Output {
100100
((self.0)(&input.0),)
101101
}
102102
}
103103

104-
impl<F: FnMut(&T) -> U, U, T> TupleUniformMapper<&(T,), U> for &mut (F,) {
104+
impl<U, T, F: FnMut(&T) -> U> TupleUniformMapper<&(T,), U> for &mut (F,) {
105105
type Output = (U,);
106106

107107
fn apply_uniform_map(self, input: &(T,)) -> Self::Output {
108108
((self.0)(&input.0),)
109109
}
110110
}
111111

112-
impl<F: Fn(&T) -> U, U, T> TupleUniformMapper<&(T,), U> for &(F,) {
112+
impl<U, T, F: Fn(&T) -> U> TupleUniformMapper<&(T,), U> for &(F,) {
113113
type Output = (U,);
114114

115115
fn apply_uniform_map(self, input: &(T,)) -> Self::Output {
116116
((self.0)(&input.0),)
117117
}
118118
}
119119

120-
impl<F: FnOnce(&mut T) -> U, U, T> TupleUniformMapper<&mut (T,), U> for (F,) {
120+
impl<U, T, F: FnOnce(&mut T) -> U> TupleUniformMapper<&mut (T,), U> for (F,) {
121121
type Output = (U,);
122122

123123
fn apply_uniform_map(self, input: &mut (T,)) -> Self::Output {
124124
((self.0)(&mut input.0),)
125125
}
126126
}
127127

128-
impl<F: FnMut(&mut T) -> U, U, T> TupleUniformMapper<&mut (T,), U> for &mut (F,) {
128+
impl<U, T, F: FnMut(&mut T) -> U> TupleUniformMapper<&mut (T,), U> for &mut (F,) {
129129
type Output = (U,);
130130

131131
fn apply_uniform_map(self, input: &mut (T,)) -> Self::Output {
132132
((self.0)(&mut input.0),)
133133
}
134134
}
135135

136-
impl<F: Fn(&mut T) -> U, U, T> TupleUniformMapper<&mut (T,), U> for &(F,) {
136+
impl<U, T, F: Fn(&mut T) -> U> TupleUniformMapper<&mut (T,), U> for &(F,) {
137137
type Output = (U,);
138138

139139
fn apply_uniform_map(self, input: &mut (T,)) -> Self::Output {

tuples/src/uniform_map_by.rs

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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

Comments
 (0)