49
49
//! Inside `typle` code, individual components of a tuple can be selected using
50
50
//! `<{i}>` for types and `[[i]]` for values. The value `i` must be a *typle
51
51
//! index expression*, an expression that only uses literal `usize` values or
52
- //! *typle index variables* created by one of several macros.
53
- //!
54
- //! The [`typle_for!`] macro can create a new tuple type or expression. Inside
55
- //! the macro the typle index variable can provide access to each component of
56
- //! an existing tuple type or expression.
52
+ //! *typle index variables* created by one of several macros, and reduces to a
53
+ //! single value or a range.
57
54
//!
58
55
//! ```rust
59
56
//! # use typle::typle;
60
57
//! // Split off the first component
61
58
//! #[typle(Tuple for 1..=12)]
62
59
//! fn split<T: Tuple>(
63
60
//! t: T // t: (T<0>, T<1>, T<2>,...)
64
- //! ) -> (T<0>, typle_for!(i in 1.. => T<{i}> )) // (T<0>, (T<1>, T<2>,...))
61
+ //! ) -> (T<0>, ( T<{1..}>, )) // (T<0>, (T<1>, T<2>,...))
65
62
//! {
66
- //! (t[[0]], typle_for!(i in 1.. => t[[i]] )) // (t.0, (t.1, t.2,...))
63
+ //! (t[[0]], ( t[[1..]], )) // (t.0, (t.1, t.2,...))
67
64
//! }
68
65
//!
69
66
//! assert_eq!(split(('1', 2, 3.0)), ('1', (2, 3.0)));
70
67
//! assert_eq!(split((2, 3.0)), (2, (3.0,)));
71
68
//! assert_eq!(split((3.0,)), (3.0, ()));
72
69
//! ```
73
70
//!
71
+ //! The [`typle_for!`] macro creates a new type or expression. Inside
72
+ //! the macro the typle index variable provides access to each component of
73
+ //! an existing tuple type or expression.
74
+ //!
75
+ //! The associated constant `LEN` provides the length of the tuple in each
76
+ //! generated item. This value can be used in typle index expressions.
77
+ //!
78
+ //! ```
79
+ //! # use typle::typle;
80
+ //! #[typle(Tuple for 0..=12)]
81
+ //! pub fn reverse<T: Tuple>(t: T) -> typle_for!(i in 1..=T::LEN => T<{T::LEN - i}>) {
82
+ //! typle_for!(i in 1..=T::LEN => t[[T::LEN - i]])
83
+ //! }
84
+ //!
85
+ //! assert_eq!(reverse((Some(3), "four", 5)), (5, "four", Some(3)));
86
+ //! ```
87
+ //!
74
88
//! The [`typle_fold!`] macro reduces a tuple to a single value.
75
89
//!
90
+ //! The default bounds for a macro range are `0..Tuple::LEN`, that is, for all
91
+ //! components of the tuple.
92
+ //!
76
93
//! ```rust
77
94
//! # use typle::typle;
78
95
//! #[typle(Tuple for 0..=12)]
79
96
//! pub fn sum<T: Tuple<u32>>(t: T) -> u32 {
80
97
//! typle_fold!(0; i in .. => |total| total + t[[i]])
81
98
//! }
99
+ //!
82
100
//! assert_eq!(sum(()), 0);
83
101
//! assert_eq!(sum((1, 4, 9, 16)), 30);
84
102
//! ```
119
137
//! Use the `typle_index!` macro in a `for` loop to iterate over a range bounded
120
138
//! by typle index expressions.
121
139
//!
122
- //! The associated constant `LEN` provides the length of the tuple in each
123
- //! generated item. This value can be used in typle index expressions.
124
- //!
125
140
//! ```rust
126
141
//! # use typle::typle;
127
142
//! # struct MyStruct<T> {
@@ -614,21 +629,6 @@ pub fn typle_any(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
614
629
/// Insert tuple components into a sequence.
615
630
///
616
631
/// The `typle_args!` macro allows components of a tuple to be inserted into an existing sequence.
617
- ///
618
- /// For types, `T<{..}>` is a shorthand for `typle_args!(i in .. => T<{i}>)`.
619
- /// For values, `t[[..]]` is a shorthand for `typle_args!(i in .. => t[[i]])`.
620
- ///
621
- /// ```
622
- /// # use typle::typle;
623
- /// #[typle(Tuple for 0..12)]
624
- /// fn append<T: Tuple, A>(t: T, a: A) -> (T<{..}>, A) {
625
- /// (t[[..]], a)
626
- /// }
627
- ///
628
- /// assert_eq!(append((1, 2, 3), 4), (1, 2, 3, 4));
629
- /// ```
630
- ///
631
- /// The full `typle_args!` macro is required when modifying each component.
632
632
/// ```
633
633
/// # use typle::typle;
634
634
/// #[typle(Tuple for 0..=12)]
@@ -648,7 +648,9 @@ pub fn typle_any(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
648
648
/// );
649
649
/// ```
650
650
///
651
- /// Note how this behaves differently to [`typle_for!`]:
651
+ /// Note that this behaves differently to [`typle_for!`]. `typle_for!` becomes a new tuple or array.
652
+ /// `typle_args!` becomes a sequence of components and must appear inside an existing typle, array,
653
+ /// or argument list.
652
654
/// ```
653
655
/// # use typle::typle;
654
656
/// #[typle(Tuple for 0..=12)]
@@ -666,6 +668,19 @@ pub fn typle_any(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
666
668
/// coalesce_some((1, 2), (3, 4)),
667
669
/// ((Some(1), Some(2)), (Some(3), Some(4)))
668
670
/// );
671
+ ///
672
+ /// ```
673
+ /// For types, `T<{start..end}>` is a shorthand for `typle_args!(i in start..end => T<{i}>)`.
674
+ /// For expressions, `t[[start..end]]` is a shorthand for `typle_args!(i in start..end => t[[i]])`.
675
+ ///
676
+ /// ```
677
+ /// # use typle::typle;
678
+ /// #[typle(Tuple for 0..12)]
679
+ /// fn append<T: Tuple, A>(t: T, a: A) -> (T<{..}>, A) {
680
+ /// (t[[..]], a)
681
+ /// }
682
+ ///
683
+ /// assert_eq!(append((1, 2, 3), 4), (1, 2, 3, 4));
669
684
/// ```
670
685
#[ proc_macro]
671
686
pub fn typle_args ( item : proc_macro:: TokenStream ) -> proc_macro:: TokenStream {
0 commit comments