tuplez 0.1.4

Tuples represented in recursive form
Documentation
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
macro_rules! __from_primitive {
    ($tup:ident; $($v:ident)*) => {{
        use paste::paste;
        paste!{
            let ($([< $v:lower >],)*) = $tup;
            tuple!($([< $v:lower  >]),*)
        }
    }};
}

macro_rules! __to_primitive {
    ($tup:ident $(;)?) => { () };
    ($tup:ident; $($v:ident)+) => {{
        use paste::paste;
        paste!{
            let __to_primitive!(@expand $([< $v:lower >])+) = $tup;
            ($([< $v:lower >],)+)
        }
    }};
    (@expand $v:ident) => {
        Tuple($v, _)
    };
    (@expand $v:ident $($vs:ident)+) => {
        Tuple($v, __to_primitive!(@expand $($vs)+))
    };
}

macro_rules! __from_array {
    ($arr:ident; $($v:ident)*) => {{
        use paste::paste;
        paste!{
            let [$([< $v:lower >],)*] = $arr;
            tuple!($([< $v:lower  >]),*)
        }
    }};
}

macro_rules! __to_array {
    ($tup:ident; $($v:ident)+) => {{
        use paste::paste;
        paste!{
            let __to_primitive!(@expand $([< $v:lower >])+) = $tup;
            [ $([< $v:lower >],)+ ]
        }
    }};
    (@expand $v:ident) => {
        Tuple($v, _)
    };
    (@expand $v:ident $($vs:ident)+) => {
        Tuple($v, __to_primitive!(@expand $($vs)+))
    };
}

#[cfg(not(feature = "any_array"))]
macro_rules! __tuple_array_impl {
    ($cnt:literal;) => {
        impl<T> ToArray<T> for Unit {
            type Array = [T; 0];
            fn to_array(self) -> Self::Array {
                Default::default()
            }
        }

        impl<T> From<[T; 0]> for Unit {
            fn from(_: [T; 0]) -> Self {
                Unit
            }
        }
    };
    ($cnt:literal; $($ts:ident)+) => {
        impl<T> ToArray<T> for tuple_t!(T; $cnt) {
            type Array = [T; $cnt];
            fn to_array(self) -> Self::Array {
                __to_array!(self; $($ts)*)
            }
        }

        impl<T> From<[T; $cnt]> for tuple_t!(T; $cnt) {
            fn from(arr: [T; $cnt]) -> Self {
                __from_array!(arr; $($ts)*)
            }
        }
    };
}

macro_rules! __tuple_traits_impl {
    ($cnt:literal; $($ts:ident)*) => {
        impl<$($ts),*> ToPrimitive for tuple_t!($($ts),*)
        {
            type Primitive = ($($ts,)*);
            fn primitive(self)  -> Self::Primitive {
                __to_primitive!(self; $($ts)*)
            }
        }

        impl<$($ts),*> From<($($ts,)*)> for tuple_t!($($ts),*) {
            fn from(prim: ($($ts,)*)) -> Self {
                __from_primitive!(prim; $($ts)*)
            }
        }

        #[cfg(not(feature = "any_array"))]
        __tuple_array_impl!{ $cnt; $($ts)* }
    };
}

/// Provides a simple way to create a functor that implements [`Mapper`](crate::Mapper).
///
/// # Syntax
///
/// ```text
/// Introduce   = Variable [, Lifetime1, Lifetime2, ... ]
/// TypeMap     = TypeIn [=> TypeOut]
/// Specialized = Introduce : TypeMap : Expr
/// Universal   = _ : TypeMap : Function [where Bounds]
///
/// mapper!([ Specialized1; Specialized2; ... ] [; Univsersal])
/// ```
///
/// *The `[` and `]` markers only indicate optional content but not that the `[` and `]` need to be input.*
///
/// *Similarly, `...` indicates several repeated segments, rather than inputing `...`.*
///
/// # Explanation
///
/// ## Specialized mapping rules
///
/// Firstly introduce a variable name used to represent the element (Recommand using `x`) and all possible lifetime paramters.
/// If the element type contains a reference or generic lifetime parameter, please explicitly annotate all lifetimes:
///
/// ```text
/// x, 'a
/// ```
///
/// Then specify the mapping of the type, that is, element type => output type.
/// If the element type and output type are the same type, the output type can be omitted:
///
/// ```text
/// &'a str => &'a [u8]
/// ```
///
/// Next, write down the conversion expression, remember that `x` is an immutable reference to the element:
///
/// ```text
/// x.as_bytes()
/// ```
///
/// Finally, use `:` to link them, and a specialized mapping rule is complete:
///
/// ```text
/// x, 'a : &'a str => &'a [u8] : x.as_bytes()
/// ```
///
/// Construct specialized mapping rules for all element types in the tuple,
/// and then combine them in a [`mapper!`](crate::mapper!) macro to traverse the tuple:
///
/// ```
/// use tuplez::*;
///
/// let tup = tuple!(1, "hello", 3.14).foreach(mapper!{
///    x: i32 : *x + 1;                                 // Omit the output type
///    x: f32 => String: x.to_string();
///    x, 'a: &'a str => &'a [u8]: x.as_bytes()
/// });
/// assert_eq!(tup, tuple!(2, b"hello" as &[u8], "3.14".to_string()));
/// ```
///
/// ## Universal mapping rule
///
/// Universal mapping rule currently only supports conversion through generic functions, not expressions.
/// So first define the mapping function:
///
/// ```
/// fn to_string<T: ToString>(v: &T) -> String {
///     v.to_string()
/// }
/// ```
///
/// Write down the mapping of types as before, only this time we use an `_` for element types.
///
/// ```text
/// _ => String
/// ```
///
/// Finally, use `:` to link them. But there is a little noise here. We need to write down the bounds of the mapping
/// function as well:
///
/// ```text
/// _ => String : to_string where ToString
/// ```
///
/// Put it in the [`mapper!`](crate::mapper!) macro to traverse the tuple:
///
/// ```
/// use tuplez::*;
///
/// fn to_string<T: ToString>(v: &T) -> String {
///     v.to_string()
/// }
///
/// let tup = tuple!(1, "hello", 3.14);
/// let tup2 = tup.foreach(mapper! {
///     _ => String: to_string where ToString
/// });
/// assert_eq!(
///     tup2,
///     tuple!("1".to_string(), "hello".to_string(), "3.14".to_string())
/// );
/// ```
///
/// If the mapping function output the same type, the output type can be omitted:
///
/// ```
/// use tuplez::*;
///
/// fn just<T: Copy>(v: &T) -> T { *v }
///
/// let tup = tuple!(1, "hello", 3.14);
/// let tup2 = tup.foreach(mapper! {
///     _ : just where Copy
/// });
/// assert_eq!(tup, tup2);
/// ```
///
/// ## Use both
///
/// You can use both multiple specialized mapping rules and one universal mapping rule in a [`mapper!`](crate::mapper!) macro,
/// but there are some restrictions.
///
/// 1. The universal mapping rule must be placed after all specialized mapping rules.
/// 2. Types that use specialized mapping rules must be exclusive from the bounds of the universal mapping rule.
/// 3. Either all types that use specialized mapping rules is your custom types, or the bounds of the universal mapping rule contain
/// your custom traits.
///
/// Example of custom type:
///
/// ```
/// use tuplez::*;
///
/// struct MyElement(i32);
///
/// fn to_string<T: ToString>(v: &T) -> String {
///     v.to_string()
/// }
///
/// let tup = tuple!(MyElement(12), "hello", 3.14);
/// let tup2 = tup.foreach(mapper! {
///     x : MyElement => i32 : x.0;
///     _ => String: to_string where ToString
/// });
/// assert_eq!(tup2, tuple!(12, "hello".to_string(), "3.14".to_string()));
/// ```
///
/// Example of custom trait:
///
/// ```
/// use tuplez::*;
///
/// trait MyToString: ToString {}
/// impl MyToString for &str {}
/// impl MyToString for f32 {}
///
/// fn to_string<T: MyToString>(v: &T) -> String {
///     v.to_string()
/// }
///
/// let tup = tuple!(vec![12, 14], "hello", 3.14);
/// /* Using `ToString` here is not allowed because
///  * neither `Vec` nor `ToString` is defined in current crate,
///  * even though `Vec` does not implement `ToString`.
///  */
/// let tup2 = tup.foreach(mapper! {
///     x : Vec<i32> => i32 : x[0];
///     _ => String: to_string where MyToString
/// });
/// assert_eq!(tup2, tuple!(12, "hello".to_string(), "3.14".to_string()));
/// ```
#[macro_export]
macro_rules! mapper {
    ($($x:ident $(, $lt:lifetime)*: $it:ty $(=> $ot:ty)? : $e:expr);* $(;)?) => {{
        struct __Mapper;
        $(
            mapper!(@impl $x $(, $lt)* : $it $(=> $ot)? : $e);
        )*
        &mut __Mapper
    }};
    ($($x:ident $(, $lt:lifetime)*: $it:ty $(=> $ot:ty)? : $e:expr ;)*
        $(_ $(=> $ot2:ty)? : $f:ident $(where $($t:tt)*)?)?) => {{
        struct __Mapper;
        $(
            mapper!(@impl $x $(, $lt)* : $it $(=> $ot)? : $e);
        )*
        $(
            mapper!(@impl _ $(=> $ot2)? : $f $(where $($t)*)?);
        )?
        &mut __Mapper
    }};
    (@impl _ : $f:ident $(where $($t:tt)*)?) => {{
        impl<T $(: $($t)*)?> Mapper<T> for __Mapper
        {
            type Output = T;
            fn map(&mut self, value: &T) -> Self::Output {
                $f(value)
            }
        }
    }};
    (@impl _ => $ot:ty : $f:ident $(where $($t:tt)*)?) => {{
        impl<T$(: $($t)*)?> Mapper<T> for __Mapper
        {
            type Output = $ot;
            fn map(&mut self, value: &T) -> Self::Output {
                $f(value)
            }
        }
    }};
    (@impl $x:ident : $it:ty : $e:expr) => {
        impl Mapper<$it> for __Mapper {
            type Output = $it;
            fn map(&mut self, value: &$it) -> Self::Output {
                let f = |$x : &$it| -> $it { $e };
                f(value)
            }
        }
    };
    (@impl $x:ident $(, $lt:lifetime)* : $it:ty : $e:expr) => {
        impl<$($lt),*> Mapper<$it> for __Mapper {
            type Output = $it;
            fn map(&mut self, value: &$it) -> Self::Output {
                let f = |$x : &$it| -> $it { $e };
                f(value)
            }
        }
    };
    (@impl $x:ident : $it:ty => $ot:ty : $e:expr) => {
        impl Mapper<$it> for __Mapper {
            type Output = $ot;
            fn map(&mut self, value: &$it) -> Self::Output {
                let f = |$x : &$it| -> $ot { $e };
                f(value)
            }
        }
    };
    (@impl $x:ident $(, $lt:lifetime)* : $it:ty => $ot:ty : $e:expr) => {
        impl<$($lt),*> Mapper<$it> for __Mapper {
            type Output = $ot;
            fn map(&mut self, value: &$it) -> Self::Output {
                let f = |$x : &$it| -> $ot { $e };
                f(value)
            }
        }
    };
}

/// Provides a simple way to create a functor that implements [`MapperMut`](crate::MapperMut).
///
/// # Syntax
///
/// The syntax is exactly the same as [`mapper!`](crate::mapper!). The difference is that [`mapper_mut!`](crate::mapper_mut!) passes in mutable references
/// to the elements of the tuple instead of immutable references.
///
/// # Example
///
/// ```
/// use tuplez::*;
///
/// let mut tup = tuple!(2, "hello", 3.14);
/// let tup2 = tup.foreach_mut(mapper_mut!{
///     x: i32: { (*x) *= (*x); *x - 1 };
///     x: f32 => (): *x += 1.0;
///     x, 'a: &'a str: *x
/// });
/// assert_eq!(tup, tuple!(4, "hello", 3.14 + 1.0));
/// assert_eq!(tup2, tuple!(3, "hello", ()));
/// ```
#[macro_export]
macro_rules! mapper_mut {
    ($($x:ident $(, $lt:lifetime)*: $it:ty $(=> $ot:ty)? : $e:expr);* $(;)?) => {{
        struct __MapperMut;
        $(
            mapper_mut!(@impl $x $(, $lt)* : $it $(=> $ot)? : $e);
        )*
        &mut __MapperMut
    }};
    ($($x:ident $(, $lt:lifetime)*: $it:ty $(=> $ot:ty)? : $e:expr ;)*
        $(_ $(=> $ot2:ty)? : $f:ident $(where $($t:tt)*)?)?) => {{
        struct __MapperMut;
        $(
            mapper_mut!(@impl $x $(, $lt)* : $it $(=> $ot)? : $e);
        )*
        $(
            mapper_mut!(@impl _ $(=> $ot2)? : $f $(where $($t)*)?);
        )?
        &mut __MapperMut
    }};
    (@impl _ : $f:ident $(where $($t:tt)*)?) => {{
        impl<T $(: $($t)*)?> MapperMut<T> for __MapperMut
        {
            type Output = T;
            fn map_mut(&mut self, value: &mut T) -> Self::Output {
                $f(value)
            }
        }
    }};
    (@impl _ => $ot:ty : $f:ident $(where $($t:tt)*)?) => {{
        impl<T$(: $($t)*)?> MapperMut<T> for __MapperMut
        {
            type Output = $ot;
            fn map_mut(&mut self, value: &mut T) -> Self::Output {
                $f(value)
            }
        }
    }};
    (@impl $x:ident : $it:ty : $e:expr) => {
        impl MapperMut<$it> for __MapperMut {
            type Output = $it;
            fn map_mut(&mut self, value: &mut $it) -> Self::Output {
                let f = |$x : &mut $it| -> $it { $e };
                f(value)
            }
        }
    };
    (@impl $x:ident $(, $lt:lifetime)* : $it:ty : $e:expr) => {
        impl<$($lt),*> MapperMut<$it> for __MapperMut {
            type Output = $it;
            fn map_mut(&mut self, value: &mut $it) -> Self::Output {
                let f = |$x : &mut $it| -> $it { $e };
                f(value)
            }
        }
    };
    (@impl $x:ident : $it:ty => $ot:ty : $e:expr) => {
        impl MapperMut<$it> for __MapperMut {
            type Output = $ot;
            fn map_mut(&mut self, value: &mut $it) -> Self::Output {
                let f = |$x : &mut $it| -> $ot { $e };
                f(value)
            }
        }
    };
    (@impl $x:ident $(, $lt:lifetime)* : $it:ty => $ot:ty : $e:expr) => {
        impl<$($lt),*> MapperMut<$it> for __MapperMut {
            type Output = $ot;
            fn map_mut(&mut self, value: &mut $it) -> Self::Output {
                let f = |$x : &mut $it| -> $ot { $e };
                f(value)
            }
        }
    };
}

/// Provides a simple way to create a functor that implements [`MapperOnce`](crate::MapperOnce).
///
/// # Syntax
///
/// The syntax is exactly the same as [`mapper!`](crate::mapper!). The difference is that [`mapper_once!`](crate::mapper_once!) take elements of the tuple
/// instead of pass in their immutable references.
///
/// # Example
///
/// ```
/// use tuplez::*;
///
/// let tup = tuple!(vec![1, 2, 3], "hello".to_string());
/// let tup2 = tup.foreach_once(mapper_once!{
///     x: Vec<i32> => Box<[i32]> : x.into();
///     x: String : x
/// });
/// // assert_eq!(tup, ... ); // No, `tup` has been moved
/// assert_eq!(
///     tup2,
///     tuple!(
///         Box::<[i32; 3]>::new([1, 2, 3]) as Box<[i32]>,
///         "hello".to_string()
///     )
/// );
/// ```
#[macro_export]
macro_rules! mapper_once {
    ($($x:ident $(, $lt:lifetime)*: $it:ty $(=> $ot:ty)? : $e:expr);* $(;)?) => {{
        struct __MapperOnce;
        $(
            mapper_once!(@impl $x $(, $lt)* : $it $(=> $ot)? : $e);
        )*
        &mut __MapperOnce
    }};
    ($($x:ident $(, $lt:lifetime)*: $it:ty $(=> $ot:ty)? : $e:expr ;)*
        $(_ $(=> $ot2:ty)? : $f:ident $(where $($t:tt)*)?)?) => {{
        struct __MapperOnce;
        $(
            mapper_once!(@impl $x $(, $lt)* : $it $(=> $ot)? : $e);
        )*
        $(
            mapper_once!(@impl _ $(=> $ot2)? : $f $(where $($t)*)?);
        )?
        &mut __MapperOnce
    }};
    (@impl _ : $f:ident $(where $($t:tt)*)?) => {{
        impl<T $(: $($t)*)?> MapperOnce<T> for __MapperOnce
        {
            type Output = T;
            fn map_once(&mut self, value: T) -> Self::Output {
                $f(value)
            }
        }
    }};
    (@impl _ => $ot:ty : $f:ident $(where $($t:tt)*)?) => {{
        impl<T$(: $($t)*)?> MapperOnce<T> for __MapperOnce
        {
            type Output = $ot;
            fn map_once(&mut self, value: T) -> Self::Output {
                $f(value)
            }
        }
    }};
    (@impl $x:ident : $it:ty : $e:expr) => {
        impl MapperOnce<$it> for __MapperOnce {
            type Output = $it;
            fn map_once(&mut self, value: $it) -> Self::Output {
                #[allow(unused_mut)]
                let f = |mut $x : $it| -> $it { $e };
                f(value)
            }
        }
    };
    (@impl $x:ident $(, $lt:lifetime)* : $it:ty : $e:expr) => {
        impl<$($lt),*> MapperOnce<$it> for __MapperOnce {
            type Output = $it;
            fn map_once(&mut self, value: $it) -> Self::Output {
                #[allow(unused_mut)]
                let f = |mut $x : $it| -> $it { $e };
                f(value)
            }
        }
    };
    (@impl $x:ident : $it:ty => $ot:ty : $e:expr) => {
        impl MapperOnce<$it> for __MapperOnce {
            type Output = $ot;
            fn map_once(&mut self, value: $it) -> Self::Output {
                #[allow(unused_mut)]
                let f = |mut $x : $it| -> $ot { $e };
                f(value)
            }
        }
    };
    (@impl $x:ident $(, $lt:lifetime)* : $it:ty => $ot:ty : $e:expr) => {
        impl<$($lt),*> MapperOnce<$it> for __MapperOnce {
            type Output = $ot;
            fn map_once(&mut self, value: $it) -> Self::Output {
                #[allow(unused_mut)]
                let f = |mut $x : $it| -> $ot { $e };
                f(value)
            }
        }
    };
}