|
5 | 5 |
|
6 | 6 | #![no_std] |
7 | 7 |
|
| 8 | +use tuplities_len::TupleLen; |
| 9 | + |
8 | 10 | /// A trait for indexing into tuples at compile-time known positions. |
9 | 11 | /// |
10 | 12 | /// This trait allows accessing elements at specific indices `Idx` |
|
23 | 25 | /// |
24 | 26 | /// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate. |
25 | 27 | #[tuplities_derive::impl_tuple_index] |
26 | | -pub trait TupleIndex<Idx: typenum::Unsigned> { |
| 28 | +pub trait TupleIndex<Idx: typenum::Unsigned>: TupleLen { |
27 | 29 | /// The type of the element at index `Idx`. |
28 | 30 | type Type; |
29 | 31 |
|
@@ -53,3 +55,65 @@ pub trait TupleIndexMut<Idx: typenum::Unsigned>: TupleIndex<Idx> { |
53 | 55 | /// Returns a mutable reference to the element at index `Idx`. |
54 | 56 | fn tuple_index_mut(&mut self) -> &mut Self::Type; |
55 | 57 | } |
| 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 | +} |
0 commit comments