Skip to content

Commit 228d298

Browse files
Added helper traits
1 parent 3dcb339 commit 228d298

5 files changed

Lines changed: 81 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- `TupleFrom<T>` and `TupleInto<U>` traits for infallible conversions between tuples where elements implement `From`/`Into`
2121
- Support for tuple conversions with proper error handling and type safety
2222
- `TupleRow<Idx>` and `TupleRowMut<Idx>` traits for indexing rows in tuples of tuples
23-
24-
### Changed in Unreleased
25-
26-
- Renamed `Row` and `RowMut` traits to `TupleRow` and `TupleRowMut` respectively to avoid naming collisions
27-
- Renamed `row()` and `row_mut()` methods to `tuple_row()` and `tuple_row_mut()` for consistency
28-
- Renamed `TupleLen::Idx` associated type to `TupleLen::Len` for better naming consistency
23+
- `FirstTupleRow` and `LastTupleRow` convenience traits for accessing the first and last rows in tuples of tuples
2924

3025
## [0.1.0] - 2025-12-05
3126

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ The library provides several traits for working with tuples:
5151
- [`TupleIndexMut<Idx>`](https://docs.rs/tuplities-index/latest/tuplities_index/trait.TupleIndexMut.html): Provides an [`index_mut()`](https://docs.rs/tuplities-index/latest/tuplities_index/trait.TupleIndexMut.html#tymethod.index_mut) method to access a mutable reference to the element at the specified index [`typenum`](https://docs.rs/typenum/latest/typenum/)'s `Idx` of the tuple.
5252
- [`TupleRow<Idx>`](https://docs.rs/tuplities-row/latest/tuplities_row/trait.TupleRow.html): Provides a [`tuple_row()`](https://docs.rs/tuplities-row/latest/tuplities_row/trait.TupleRow.html#tymethod.tuple_row) method to access elements at the specified index across all tuples in a tuple of tuples (row-wise indexing).
5353
- [`TupleRowMut<Idx>`](https://docs.rs/tuplities-row/latest/tuplities_row/trait.TupleRowMut.html): Provides a [`tuple_row_mut()`](https://docs.rs/tuplities-row/latest/tuplities_row/trait.TupleRowMut.html#tymethod.tuple_row_mut) method to access mutable elements at the specified index across all tuples in a tuple of tuples (mutable row-wise indexing).
54+
- [`FirstTupleRow`](https://docs.rs/tuplities-row/latest/tuplities_row/trait.FirstTupleRow.html): A convenience trait providing a [`first_tuple_row()`](https://docs.rs/tuplities-row/latest/tuplities_row/trait.FirstTupleRow.html#tymethod.first_tuple_row) method to access the first element of each tuple in a tuple of tuples.
55+
- [`LastTupleRow`](https://docs.rs/tuplities-row/latest/tuplities_row/trait.LastTupleRow.html): A convenience trait providing a [`last_tuple_row()`](https://docs.rs/tuplities-row/latest/tuplities_row/trait.LastTupleRow.html#tymethod.last_tuple_row) method to access the last element of each tuple in a tuple of tuples.
5456

5557
## Features
5658

tuplities-replicate/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ pub trait TupleReplicate<T> {
2222
///
2323
/// let tuple: (i32, i32, i32) = TupleReplicate::tuple_replicate(42);
2424
/// assert_eq!(tuple, (42, 42, 42));
25+
/// let _tuple_empty: () = TupleReplicate::tuple_replicate(42);
26+
/// let tuple_single: (i32,) = TupleReplicate::tuple_replicate(42);
27+
/// assert_eq!(tuple_single, (42,));
28+
/// let tuple_two: (i32, i32) = TupleReplicate::tuple_replicate(42);
29+
/// assert_eq!(tuple_two, (42, 42));
30+
///
31+
/// let tuple_refs: (&str, &str, &str) = TupleReplicate::tuple_replicate("hello");
32+
/// assert_eq!(tuple_refs, ("hello", "hello", "hello"));
33+
///
34+
/// let tuple_strings: (String, String) = TupleReplicate::tuple_replicate(String::from("world"));
35+
/// assert_eq!(tuple_strings, (String::from("world"), String::from("world")));
36+
///
37+
/// let tuple_refs2: (&i32, &i32) = TupleReplicate::tuple_replicate(&100);
38+
/// assert_eq!(tuple_refs2, (&100, &100));
2539
/// ```
2640
fn tuple_replicate(value: T) -> Self;
2741
}

tuplities-row/src/lib.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,66 @@ pub trait TupleRowMut<Idx: typenum::Unsigned>: TupleRow<Idx, RowType: TupleMut>
6161
/// Returns a tuple of mutable references to the elements at index `Idx` in each inner tuple.
6262
fn tuple_row_mut(&mut self) -> <Self::RowType as TupleMut>::Mut<'_>;
6363
}
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 that implements `TupleRow<U0>`.
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+
pub trait FirstTupleRow: TupleRow<typenum::U0> {
81+
/// Returns a tuple of references to the first element in each inner tuple.
82+
fn first_tuple_row(&self) -> <Self::RowType as TupleRef>::Ref<'_> {
83+
self.tuple_row()
84+
}
85+
}
86+
87+
impl<T: TupleRow<typenum::U0>> FirstTupleRow for T {}
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+
pub trait LastTupleRow:
107+
TupleRow<
108+
<<Self as TupleLen>::Len as core::ops::Sub<typenum::U1>>::Output,
109+
Len: core::ops::Sub<typenum::U1, Output: typenum::Unsigned>,
110+
>
111+
{
112+
/// Returns a tuple of references to the last element in each inner tuple.
113+
fn last_tuple_row(&self) -> <Self::RowType as TupleRef>::Ref<'_>;
114+
}
115+
116+
impl<T> LastTupleRow for T
117+
where
118+
T: TupleRow<
119+
<<T as TupleLen>::Len as core::ops::Sub<typenum::U1>>::Output,
120+
Len: core::ops::Sub<typenum::U1, Output: typenum::Unsigned>,
121+
>,
122+
{
123+
fn last_tuple_row(&self) -> <Self::RowType as TupleRef>::Ref<'_> {
124+
self.tuple_row()
125+
}
126+
}

tuplities/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub mod prelude {
2626
pub use tuplities_remove::TupleRemove;
2727
pub use tuplities_replicate::TupleReplicate;
2828
pub use tuplities_reverse::TupleReverse;
29-
pub use tuplities_row::{TupleRow, TupleRowMut};
29+
pub use tuplities_row::{FirstTupleRow, LastTupleRow, TupleRow, TupleRowMut};
3030
pub use tuplities_split::TupleSplit;
3131
pub use tuplities_try_from::{TupleTryFrom, TupleTryInto};
3232
}

0 commit comments

Comments
 (0)