tuplities_row/
lib.rs

1#![no_std]
2
3//! [tuplities](https://github.com/lucacappelletti94/tuplities) suite crate providing the `TupleRow` and `TupleRowMut` traits.
4
5use tuplities_len::TupleLen;
6use tuplities_mut::TupleMut;
7use tuplities_ref::TupleRef;
8
9/// A trait for indexing rows in tuples of tuples.
10///
11/// This trait allows accessing elements at a specific index across all tuples in a tuple of tuples.
12/// For a tuple of tuples like `((A, B), (C, D))`, `TupleRow<U0>` would return `(&A, &C)` and `TupleRow<U1>` would return `(&B, &D)`.
13///
14/// Each inner tuple must implement `TupleIndex<Idx>`, and the returned row tuple implements `TupleRef`.
15///
16/// # Examples
17///
18/// ```
19/// use tuplities_row::TupleRow;
20/// use typenum::U0;
21///
22/// let matrix = ((1, 2), (3, 4), (5, 6));
23/// let first_row = TupleRow::<U0>::tuple_row(&matrix);
24/// assert_eq!(first_row, (&1, &3, &5));
25/// ```
26///
27/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
28#[tuplities_derive::impl_row]
29pub trait TupleRow<Idx>: TupleLen {
30    /// The type of the row tuple containing elements at index `Idx`.
31    type RowType: TupleRef + TupleLen<Len = <Self as TupleLen>::Len>;
32
33    /// Returns a tuple of references to the elements at index `Idx` in each inner tuple.
34    fn tuple_row(&self) -> <Self::RowType as TupleRef>::Ref<'_>;
35}
36
37/// A trait for mutable indexing rows in tuples of tuples.
38///
39/// This trait allows mutable access to elements at a specific index across all tuples in a tuple of tuples.
40/// For a tuple of tuples like `((A, B), (C, D))`, `TupleRowMut<U0>` would return `(&mut A, &mut C)` and `TupleRowMut<U1>` would return `(&mut B, &mut D)`.
41///
42/// Each inner tuple must implement `TupleIndexMut<Idx>`, and the returned row tuple implements `TupleMut`.
43///
44/// # Examples
45///
46/// ```
47/// use tuplities_row::TupleRowMut;
48/// use typenum::U0;
49///
50/// let mut matrix = ((1, 2), (3, 4), (5, 6));
51/// let first_row = TupleRowMut::<U0>::tuple_row_mut(&mut matrix);
52/// *first_row.0 = 10;
53/// *first_row.1 = 30;
54/// *first_row.2 = 50;
55/// assert_eq!(matrix, ((10, 2), (30, 4), (50, 6)));
56/// ```
57///
58/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
59#[tuplities_derive::impl_row_mut]
60pub trait TupleRowMut<Idx>: TupleRow<Idx, RowType: TupleMut> {
61    /// Returns a tuple of mutable references to the elements at index `Idx` in each inner tuple.
62    fn tuple_row_mut(&mut self) -> <Self::RowType as TupleMut>::Mut<'_>;
63}
64
65/// A convenience trait for accessing the first row (index 0) in tuples of tuples.
66///
67/// This trait is automatically implemented for any tuple of tuples where each inner tuple implements `TupleRefFront`.
68///
69/// # Examples
70///
71/// ```
72/// use tuplities_row::FirstTupleRow;
73///
74/// let matrix = ((1, 2), (3, 4), (5, 6));
75/// let first_row = matrix.first_tuple_row();
76/// assert_eq!(first_row, (&1, &3, &5));
77/// ```
78///
79/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
80#[tuplities_derive::impl_first_tuple_row]
81pub trait FirstTupleRow: TupleLen {
82    /// The type of the row tuple containing references to the first elements.
83    type FirstRowType: TupleRef + TupleLen<Len = <Self as TupleLen>::Len>;
84
85    /// Returns a tuple of references to the first element in each inner tuple.
86    fn first_tuple_row(&self) -> <Self::FirstRowType as TupleRef>::Ref<'_>;
87}
88
89/// A convenience trait for accessing the last row in tuples of tuples.
90///
91/// This trait is automatically implemented for tuples of tuples where the implementation
92/// depends on the length of the inner tuples. It provides access to the last element
93/// of each inner tuple.
94///
95/// # Examples
96///
97/// ```
98/// use tuplities_row::LastTupleRow;
99///
100/// let matrix = ((1, 2, 3), (4, 5, 6), (7, 8, 9));
101/// let last_row = matrix.last_tuple_row();
102/// assert_eq!(last_row, (&3, &6, &9));
103/// ```
104///
105/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
106#[tuplities_derive::impl_last_tuple_row]
107pub trait LastTupleRow: TupleLen {
108    /// The type of the row tuple containing references to the last elements.
109    type LastRowType: TupleRef + TupleLen<Len = <Self as TupleLen>::Len>;
110
111    /// Returns a tuple of references to the last element in each inner tuple.
112    fn last_tuple_row(&self) -> <Self::LastRowType as TupleRef>::Ref<'_>;
113}