Skip to content

Commit 7491c83

Browse files
Added NestedTupleFrom and NestedTupleInto
1 parent b3cf116 commit 7491c83

5 files changed

Lines changed: 103 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4040
- `NestTupleMatrix` trait for converting flat tuples of flat tuples (like `((A, B), (C, D))`) into nested tuples of nested tuples (like `((A, (B,)), ((C, (D,)),))`)
4141
- `FlattenNestedTupleMatrix` trait for converting nested tuples of nested tuples back into flat tuples of flat tuples
4242
- `NestedTupleTryFrom<T, E>` and `NestedTupleTryInto<T, E>` traits for fallible, element-wise conversions between nested tuple types using `TryFrom` conversions and proper error propagation
43+
- `NestedTupleTryFrom<T, E>` and `NestedTupleTryInto<T, E>` traits for fallible, element-wise conversions between nested tuple types using `TryFrom` conversions and proper error propagation
44+
- `NestedTupleFrom<T>` and `NestedTupleInto<T>` traits for infallible, element-wise conversions using `From` implementations
4345
- `NestTupleMatrix` trait for converting flat tuples of flat tuples (like `((A, B), (C, D))`) into nested tuples of nested tuples (like `((A, (B,)), ((C, (D,)),))`)
4446
- `FlattenNestedTupleMatrix` trait for converting nested tuples of nested tuples back into flat tuples of flat tuples
4547
- `NestMatrixElements` and `FlattenMatrixElements` helper traits for recursive element processing in matrix operations

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ The library provides several traits for working with tuples:
3434
- [`NestedTupleOptionWith<H>`](https://docs.rs/tuplities-flatten-nest/latest/tuplities_flatten_nest/trait.NestedTupleOptionWith.html): Provides `first_none_with`, `first_some_with`, and `transpose_or` helpers which operate with a parallel homogeneous nested tuple of `H` used for result/error mapping.
3535
- [`NestedTupleTryFrom<T, E>`](https://docs.rs/tuplities-flatten-nest/latest/tuplities_flatten_nest/trait.NestedTupleTryFrom.html): Provides a [`nested_tuple_try_from()`](https://docs.rs/tuplities-flatten-nest/latest/tuplities_flatten_nest/trait.NestedTupleTryFrom.html#tymethod.nested_tuple_try_from) method to fallibly convert nested tuple types element-wise using `TryFrom` conversions.
3636
- [`NestedTupleTryInto<T, E>`](https://docs.rs/tuplities-flatten-nest/latest/tuplities_flatten_nest/trait.NestedTupleTryInto.html): Provides a [`nested_tuple_try_into()`](https://docs.rs/tuplities-flatten-nest/latest/tuplities_flatten_nest/trait.NestedTupleTryInto.html#tymethod.nested_tuple_try_into) convenience method to perform fallible conversions using `NestedTupleTryFrom`.
37+
- [`NestedTupleFrom<T>`](https://docs.rs/tuplities-flatten-nest/latest/tuplities_flatten_nest/trait.NestedTupleFrom.html): Provides an infallible [`nested_tuple_from()`](https://docs.rs/tuplities-flatten-nest/latest/tuplities_flatten_nest/trait.NestedTupleFrom.html#tymethod.nested_tuple_from) method to convert nested tuples using `From` element-wise.
38+
- [`NestedTupleInto<T>`](https://docs.rs/tuplities-flatten-nest/latest/tuplities_flatten_nest/trait.NestedTupleInto.html): Provides an infallible [`nested_tuple_into()`](https://docs.rs/tuplities-flatten-nest/latest/tuplities_flatten_nest/trait.NestedTupleInto.html#tymethod.nested_tuple_into) convenience method to perform conversions using `NestedTupleFrom`.
3739
- [`NestedTupleReplicate<T>`](https://docs.rs/tuplities-flatten-nest/latest/tuplities_flatten_nest/trait.NestedTupleReplicate.html): Provides a [`nested_tuple_replicate(value)`](https://docs.rs/tuplities-flatten-nest/latest/tuplities_flatten_nest/trait.NestedTupleReplicate.html#tymethod.nested_tuple_replicate) method to create nested tuples by replicating a single value across all positions.
3840
- [`TupleRef`](https://docs.rs/tuplities-ref/latest/tuplities_ref/trait.TupleRef.html): Provides a [`tuple_ref()`](https://docs.rs/tuplities-ref/latest/tuplities_ref/trait.TupleRef.html#tymethod.tuple_ref) method to get references to each element in the tuple.
3941
- [`TupleMut`](https://docs.rs/tuplities-mut/latest/tuplities_mut/trait.TupleMut.html): Provides a [`tuple_mut()`](https://docs.rs/tuplities-mut/latest/tuplities_mut/trait.TupleMut.html#tymethod.tuple_mut) method to get mutable references to each element in the tuple.

tuplities-flatten-nest/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ pub use nested_push_pop::{
2727
pub use nested_ref::{NestedTupleMut, NestedTupleRef};
2828
pub use nested_replicate::NestedTupleReplicate;
2929
pub use nested_row::{NestedTupleRow, NestedTupleRowMut};
30+
pub use nested_try_from::{NestedTupleFrom, NestedTupleInto};
3031
pub use nested_try_from::{NestedTupleTryFrom, NestedTupleTryInto};

tuplities-flatten-nest/src/nested_try_from.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,64 @@ where
4242
}
4343
}
4444

45+
/// A trait for infallibly converting from one nested tuple type to another.
46+
///
47+
/// This mirrors `From` but works element-wise across nested tuple structures.
48+
///
49+
/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
50+
pub trait NestedTupleFrom<T>: Sized {
51+
/// Converts `other` into `Self` by applying `From` element-wise.
52+
fn nested_tuple_from(other: T) -> Self;
53+
}
54+
55+
/// A trait for infallibly converting into another nested tuple type.
56+
///
57+
/// Automatically implemented for any type that implements `NestedTupleFrom`.
58+
pub trait NestedTupleInto<T>: Sized {
59+
/// Converts `self` into `T` by applying `From` element-wise.
60+
fn nested_tuple_into(self) -> T;
61+
}
62+
63+
impl<T, U> NestedTupleInto<U> for T
64+
where
65+
U: NestedTupleFrom<T>,
66+
{
67+
#[inline]
68+
fn nested_tuple_into(self) -> U {
69+
U::nested_tuple_from(self)
70+
}
71+
}
72+
73+
impl NestedTupleFrom<()> for () {
74+
#[inline]
75+
fn nested_tuple_from(_other: ()) -> Self {}
76+
}
77+
78+
impl<Head, OtherHead> NestedTupleFrom<(OtherHead,)> for (Head,)
79+
where
80+
Head: From<OtherHead>,
81+
{
82+
#[inline]
83+
fn nested_tuple_from(other: (OtherHead,)) -> Self {
84+
let (other_head,) = other;
85+
(Head::from(other_head),)
86+
}
87+
}
88+
89+
impl<Head, Tail, OtherHead, OtherTail> NestedTupleFrom<(OtherHead, OtherTail)> for (Head, Tail)
90+
where
91+
Head: From<OtherHead>,
92+
Tail: NestedTupleFrom<OtherTail>,
93+
{
94+
#[inline]
95+
fn nested_tuple_from(other: (OtherHead, OtherTail)) -> Self {
96+
let (other_head, other_tail) = other;
97+
let head = Head::from(other_head);
98+
let tail = Tail::nested_tuple_from(other_tail);
99+
(head, tail)
100+
}
101+
}
102+
45103
impl<E> NestedTupleTryFrom<(), E> for () {
46104
#[inline]
47105
fn nested_tuple_try_from(_other: ()) -> Result<Self, E> {
@@ -116,6 +174,41 @@ mod tests {
116174
assert_eq!(target, Ok((1u16, (2u16,))));
117175
}
118176

177+
#[test]
178+
fn test_nested_tuple_from_single() {
179+
let source = (1u8,);
180+
let target: (u16,) = NestedTupleFrom::nested_tuple_from(source);
181+
assert_eq!(target, (1u16,));
182+
}
183+
184+
#[test]
185+
fn test_nested_tuple_from_nested() {
186+
let source = (1u8, (2u8, (3u8,)));
187+
let target: (u16, (u16, (u16,))) = NestedTupleFrom::nested_tuple_from(source);
188+
assert_eq!(target, (1u16, (2u16, (3u16,))));
189+
}
190+
191+
#[test]
192+
fn test_nested_tuple_into() {
193+
let source = (1u8, (2u8,));
194+
let target: (u16, (u16,)) = source.nested_tuple_into();
195+
assert_eq!(target, (1u16, (2u16,)));
196+
}
197+
198+
#[test]
199+
fn test_nested_tuple_from_empty() {
200+
let source = ();
201+
let target: () = NestedTupleFrom::nested_tuple_from(source);
202+
assert_eq!(target, ());
203+
}
204+
205+
#[test]
206+
fn test_nested_tuple_into_empty() {
207+
let source = ();
208+
let target: () = source.nested_tuple_into();
209+
assert_eq!(target, ());
210+
}
211+
119212
// Note: We avoid using `TryFromIntError` in these tests to improve portability
120213
// and to focus on custom error types that do not implement `From<Infallible>`.
121214

tuplities/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ pub mod prelude {
1616
#[cfg(feature = "flatten-nest")]
1717
pub use tuplities_flatten_nest::{
1818
FlattenMatrixElements, FlattenNestedTuple, FlattenNestedTupleMatrix, IntoNestedTupleOption,
19-
NestMatrixElements, NestTuple, NestTupleMatrix, NestedTupleIndex, NestedTupleIndexMut,
20-
NestedTupleMut, NestedTupleOption, NestedTupleOptionWith, NestedTuplePopBack,
21-
NestedTuplePopFront, NestedTuplePushBack, NestedTuplePushFront, NestedTupleRef,
22-
NestedTupleReplicate, NestedTupleRow, NestedTupleRowMut, NestedTupleTryFrom,
23-
NestedTupleTryInto,
19+
NestMatrixElements, NestTuple, NestTupleMatrix, NestedTupleFrom, NestedTupleIndex,
20+
NestedTupleIndexMut, NestedTupleInto, NestedTupleMut, NestedTupleOption,
21+
NestedTupleOptionWith, NestedTuplePopBack, NestedTuplePopFront, NestedTuplePushBack,
22+
NestedTuplePushFront, NestedTupleRef, NestedTupleReplicate, NestedTupleRow,
23+
NestedTupleRowMut, NestedTupleTryFrom, NestedTupleTryInto,
2424
};
2525
#[cfg(feature = "from")]
2626
pub use tuplities_from::{TupleFrom, TupleInto};

0 commit comments

Comments
 (0)