Skip to content

Commit 7e3eedb

Browse files
committed
Doc changes
1 parent c8a8961 commit 7e3eedb

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

src/lib.rs

+41-26
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,54 @@
4949
//! Inside `typle` code, individual components of a tuple can be selected using
5050
//! `<{i}>` for types and `[[i]]` for values. The value `i` must be a *typle
5151
//! 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.
5754
//!
5855
//! ```rust
5956
//! # use typle::typle;
6057
//! // Split off the first component
6158
//! #[typle(Tuple for 1..=12)]
6259
//! fn split<T: Tuple>(
6360
//! 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>,...))
6562
//! {
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,...))
6764
//! }
6865
//!
6966
//! assert_eq!(split(('1', 2, 3.0)), ('1', (2, 3.0)));
7067
//! assert_eq!(split((2, 3.0)), (2, (3.0,)));
7168
//! assert_eq!(split((3.0,)), (3.0, ()));
7269
//! ```
7370
//!
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+
//!
7488
//! The [`typle_fold!`] macro reduces a tuple to a single value.
7589
//!
90+
//! The default bounds for a macro range are `0..Tuple::LEN`, that is, for all
91+
//! components of the tuple.
92+
//!
7693
//! ```rust
7794
//! # use typle::typle;
7895
//! #[typle(Tuple for 0..=12)]
7996
//! pub fn sum<T: Tuple<u32>>(t: T) -> u32 {
8097
//! typle_fold!(0; i in .. => |total| total + t[[i]])
8198
//! }
99+
//!
82100
//! assert_eq!(sum(()), 0);
83101
//! assert_eq!(sum((1, 4, 9, 16)), 30);
84102
//! ```
@@ -119,9 +137,6 @@
119137
//! Use the `typle_index!` macro in a `for` loop to iterate over a range bounded
120138
//! by typle index expressions.
121139
//!
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-
//!
125140
//! ```rust
126141
//! # use typle::typle;
127142
//! # struct MyStruct<T> {
@@ -614,21 +629,6 @@ pub fn typle_any(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
614629
/// Insert tuple components into a sequence.
615630
///
616631
/// 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.
632632
/// ```
633633
/// # use typle::typle;
634634
/// #[typle(Tuple for 0..=12)]
@@ -648,7 +648,9 @@ pub fn typle_any(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
648648
/// );
649649
/// ```
650650
///
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.
652654
/// ```
653655
/// # use typle::typle;
654656
/// #[typle(Tuple for 0..=12)]
@@ -666,6 +668,19 @@ pub fn typle_any(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
666668
/// coalesce_some((1, 2), (3, 4)),
667669
/// ((Some(1), Some(2)), (Some(3), Some(4)))
668670
/// );
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));
669684
/// ```
670685
#[proc_macro]
671686
pub fn typle_args(item: proc_macro::TokenStream) -> proc_macro::TokenStream {

tests/compile/unzip.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ where
5656

5757
fn try_unzip(self) -> Result<typle_for!(i in .. => Vec<T<{i}>>), Self::Error> {
5858
#[typle_attr_if(T::LEN == 0, allow(unused_mut))]
59-
let mut vecs = typle_for!(i in .. => Vec::new());
59+
let mut vecs = typle_for!(.. => Vec::new());
6060
for result in self {
6161
#[typle_attr_if(T::LEN == 0, allow(clippy::let_unit_value, unused_variables))]
6262
let t = result?;
@@ -94,7 +94,7 @@ where
9494
I: Iterator<Item = Result<T, E>>,
9595
{
9696
#[typle_attr_if(T::LEN == 0, allow(unused_mut))]
97-
let mut vecs = typle_for!(i in .. => Vec::new());
97+
let mut vecs = typle_for!(.. => Vec::new());
9898
for result in iter {
9999
#[typle_attr_if(T::LEN == 0, allow(clippy::let_unit_value, unused_variables))]
100100
let t = result?;

0 commit comments

Comments
 (0)