1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! Provides the ability to traverse tuples.
//!
//! Check the documentation page of [`Mapper`] for details.
use crate::;
/// Define mappers for traversing the tuple.
///
/// To traverse a tuple with type [`Tuple<T0, T1, ... Tn>`](crate::Tuple), you need to construct a custom mapper type,
/// which implements [`Mapper<T0>`], [`Mapper<T1>`] ... [`Mapper<Tn>`].
/// Pass your mapper to tuple's [`foreach()`](TupleLike::foreach()) method, then the tuple will call
/// mapper's [`map()`](Mapper::map()) method in order of its elements and move the elements in.
///
/// NOTE: Traversing a tuple will consume it. If this is not what you want, call [`as_ref()`](TupleLike::as_ref())
/// or [`as_mut()`](TupleLike::as_mut()) to create a new tuple that references its all members before traversing.
///
/// Tip: [`Mapper`] map elements by their types. If you are looking for a way to map elements by their order,
/// then what you are looking for is to
/// [pass a tuple containing callable objects into `fold()` method](Tuple#fold-tuples-in-order-of-their-elements-but-collecting-results-in-a-tuple).
///
/// # Quickly build a mapper by macros
///
/// Here are two ways you can quickly build a folder.
///
/// ## Traverse tuples by element types
///
/// The [`mapper!`](crate::mapper!) macro helps you build a mapper that traverses tuples according to their element types.
///
/// For example:
///
/// ```
/// use tuplez::{mapper, tuple, TupleLike};
///
/// let tup = tuple!(1, "hello", 3.14).foreach(mapper! {
/// |x: i32| -> i64 { x as i64 }
/// |x: f32| -> String { x.to_string() }
/// <'a> |x: &'a str| -> &'a [u8] { x.as_bytes() }
/// });
/// assert_eq!(tup, tuple!(1i64, b"hello" as &[u8], "3.14".to_string()));
/// ```
///
/// ## Traverse tuples in order of their elements
///
/// You can create a new tuple with the same number of elements, whose elements are all callable objects that accepts an element
/// and returns another value ([`FnOnce(T) -> U`](std::ops::FnOnce)), then, you can use that tuple as a mapper.
///
/// ```
/// use tuplez::{tuple, TupleLike};
///
/// let tup = tuple!(1, 2, 3);
/// let result = tup.foreach(
/// tuple!(
/// |x| x as f32,
/// |x: i32| x.to_string(),
/// |x: i32| Some(x),
/// )
/// );
/// assert_eq!(result, tuple!(1.0, "2".to_string(), Some(3)));
/// ```
///
/// # Custom mapper
///
/// For more complex cases that cannot be covered by the [`mapper!`](crate::mapper!) macro,
/// for example, you want to save some results into context variables,
/// you need to implement [`Mapper<Ti>`] for your mapper for all element type `Ti`s in tuples.
/// Generic can be used.
///
/// For example:
///
/// ```
/// use tuplez::{foreach::Mapper, tuple, TupleLike};
///
/// struct MyElement(i32);
///
/// struct Collector<'a>(&'a mut Vec<String>);
///
/// impl<T: ToString> Mapper<&T> for Collector<'_> {
/// type Output = ();
/// type NextMapper = Self;
/// fn map(self, value: &T) -> (Self::Output, Self::NextMapper) {
/// (
/// self.0.push(format!(
/// "{} : {}",
/// std::any::type_name::<T>(),
/// value.to_string()
/// )),
/// self,
/// )
/// }
/// }
///
/// impl Mapper<&MyElement> for Collector<'_> {
/// type Output = ();
/// type NextMapper = Self;
/// fn map(self, value: &MyElement) -> (Self::Output, Self::NextMapper) {
/// (self.0.push(format!("MyElement : {}", value.0)), self)
/// }
/// }
///
/// let mut buffers = vec![];
/// let collector = Collector(&mut buffers);
/// tuple!(1, "hello", MyElement(14))
/// .as_ref()
/// .foreach(collector);
/// assert_eq!(
/// buffers,
/// vec![
/// "i32 : 1".to_string(),
/// "&str : hello".to_string(),
/// "MyElement : 14".to_string()
/// ]
/// );
/// ```
/// Traverse the tuple.
///
/// # The mapper `F`
///
/// For traversing [`Tuple<T0, T1, ... Tn>`](crate::Tuple), you need to build a mapper,
/// which needs to implement [`Mapper<T0>`], [`Mapper<T1>`] ... [`Mapper<Tn>`].
///
/// See the documentation page of [`Mapper`] for details.