Skip to content

Commit 8f6b966

Browse files
Added helper traits for indexing
1 parent 228d298 commit 8f6b966

6 files changed

Lines changed: 71 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
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
2323
- `FirstTupleRow` and `LastTupleRow` convenience traits for accessing the first and last rows in tuples of tuples
24+
- `FirstTupleIndex` and `LastTupleIndex` convenience traits for accessing the first and last elements in tuples
2425

2526
## [0.1.0] - 2025-12-05
2627

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ The library provides several traits for working with tuples:
4949
- [`PairTuple`](https://docs.rs/tuplities-len/latest/tuplities_len/trait.PairTuple.html): A marker trait implemented for two-element tuples `(T1, T2)` with `TupleLen<Len = U2>`.
5050
- [`TupleIndex<Idx>`](https://docs.rs/tuplities-index/latest/tuplities_index/trait.TupleIndex.html): Provides an [`index()`](https://docs.rs/tuplities-index/latest/tuplities_index/trait.TupleIndex.html#tymethod.index) method to access the element at the specified index [`typenum`](https://docs.rs/typenum/latest/typenum/)'s `Idx` of the tuple.
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.
52+
- [`FirstTupleIndex`](https://docs.rs/tuplities-index/latest/tuplities_index/trait.FirstTupleIndex.html): A convenience trait providing a [`first_tuple_index()`](https://docs.rs/tuplities-index/latest/tuplities_index/trait.FirstTupleIndex.html#tymethod.first_tuple_index) method to access the first element of a tuple.
53+
- [`LastTupleIndex`](https://docs.rs/tuplities-index/latest/tuplities_index/trait.LastTupleIndex.html): A convenience trait providing a [`last_tuple_index()`](https://docs.rs/tuplities-index/latest/tuplities_index/trait.LastTupleIndex.html#tymethod.last_tuple_index) method to access the last element of a tuple.
5254
- [`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).
5355
- [`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).
5456
- [`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.

tuplities-index/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ homepage.workspace = true
1515
[dependencies]
1616
typenum = { workspace = true }
1717
tuplities-derive = { workspace = true }
18+
tuplities-len = { workspace = true }
1819

1920
[features]
2021
default = []

tuplities-index/src/lib.rs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
66
#![no_std]
77

8+
use tuplities_len::TupleLen;
9+
810
/// A trait for indexing into tuples at compile-time known positions.
911
///
1012
/// This trait allows accessing elements at specific indices `Idx`
@@ -23,7 +25,7 @@
2325
///
2426
/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
2527
#[tuplities_derive::impl_tuple_index]
26-
pub trait TupleIndex<Idx: typenum::Unsigned> {
28+
pub trait TupleIndex<Idx: typenum::Unsigned>: TupleLen {
2729
/// The type of the element at index `Idx`.
2830
type Type;
2931

@@ -53,3 +55,65 @@ pub trait TupleIndexMut<Idx: typenum::Unsigned>: TupleIndex<Idx> {
5355
/// Returns a mutable reference to the element at index `Idx`.
5456
fn tuple_index_mut(&mut self) -> &mut Self::Type;
5557
}
58+
59+
/// A convenience trait for accessing the first element (index 0) in tuples.
60+
///
61+
/// This trait is automatically implemented for any tuple that implements `TupleIndex<U0>`.
62+
///
63+
/// # Examples
64+
///
65+
/// ```
66+
/// use tuplities_index::FirstTupleIndex;
67+
///
68+
/// let tuple = (1, "hello", 3.14);
69+
/// let first = tuple.first_tuple_index();
70+
/// assert_eq!(*first, 1);
71+
/// ```
72+
///
73+
/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
74+
pub trait FirstTupleIndex: TupleIndex<typenum::U0> {
75+
/// Returns a reference to the first element in the tuple.
76+
fn first_tuple_index(&self) -> &Self::Type {
77+
self.tuple_index()
78+
}
79+
}
80+
81+
impl<T: TupleIndex<typenum::U0>> FirstTupleIndex for T {}
82+
83+
/// A convenience trait for accessing the last element in tuples.
84+
///
85+
/// This trait is automatically implemented for any tuple that implements `TupleIndex<LastIndex>`
86+
/// where `LastIndex` is calculated as `TupleLen::Len - 1`.
87+
///
88+
/// # Examples
89+
///
90+
/// ```
91+
/// use tuplities_index::LastTupleIndex;
92+
///
93+
/// let tuple = (1, "hello", 3.14);
94+
/// let last = tuple.last_tuple_index();
95+
/// assert_eq!(*last, 3.14);
96+
/// ```
97+
///
98+
/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
99+
pub trait LastTupleIndex:
100+
TupleIndex<
101+
<<Self as TupleLen>::Len as core::ops::Sub<typenum::U1>>::Output,
102+
Len: core::ops::Sub<typenum::U1, Output: typenum::Unsigned>,
103+
>
104+
{
105+
/// Returns a reference to the last element in the tuple.
106+
fn last_tuple_index(&self) -> &Self::Type;
107+
}
108+
109+
impl<T> LastTupleIndex for T
110+
where
111+
T: TupleIndex<
112+
<<T as TupleLen>::Len as core::ops::Sub<typenum::U1>>::Output,
113+
Len: core::ops::Sub<typenum::U1, Output: typenum::Unsigned>,
114+
>,
115+
{
116+
fn last_tuple_index(&self) -> &Self::Type {
117+
self.tuple_index()
118+
}
119+
}

tuplities/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub mod prelude {
1010
pub use tuplities_eq::TupleEq;
1111
pub use tuplities_from::{TupleFrom, TupleInto};
1212
pub use tuplities_hash::TupleHash;
13-
pub use tuplities_index::{TupleIndex, TupleIndexMut};
13+
pub use tuplities_index::{FirstTupleIndex, LastTupleIndex, TupleIndex, TupleIndexMut};
1414
pub use tuplities_insert::TupleInsert;
1515
pub use tuplities_len::{PairTuple, SingletonTuple, TupleLen, UnitTuple};
1616
pub use tuplities_mut::TupleMut;

0 commit comments

Comments
 (0)