Skip to content

Commit d1be66b

Browse files
Added helper method to convert an homogeneous tuple into a Vec
1 parent d2dab5f commit d1be66b

3 files changed

Lines changed: 105 additions & 2 deletions

File tree

tuplities-flatten-nest/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
//! [tuplities](https://github.com/lucacappelletti94/tuplities) suite crate providing traits for flattening and nesting tuples.
44
5+
extern crate alloc;
6+
57
mod flatten_nested;
68
mod matrix;
79
mod nest;
810
mod nested_chain;
911
mod nested_index;
12+
mod nested_into_vec;
1013
mod nested_option;
1114
mod nested_option_try_from;
1215
mod nested_push_pop;
@@ -24,6 +27,7 @@ pub use matrix::{
2427
pub use nest::{NestTuple, NestTupleMut, NestTupleRef};
2528
pub use nested_chain::NestedTupleChain;
2629
pub use nested_index::{NestedTupleIndex, NestedTupleIndexMut};
30+
pub use nested_into_vec::NestedTupleIntoVec;
2731
pub use nested_option::{
2832
IntoNestedTupleOption, NestedTupleFlattenOption, NestedTupleOption, NestedTupleOptionWith,
2933
};
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
extern crate alloc;
2+
3+
use alloc::vec;
4+
use alloc::vec::Vec;
5+
6+
/// A trait for converting a nested homogeneous tuple into a vector.
7+
///
8+
/// This trait handles nested tuples structured as cons-lists (e.g. `(head, (head2, (..., tail)))` or `(head, tail)`).
9+
/// All elements must be of the same type `T`.
10+
///
11+
/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
12+
pub trait NestedTupleIntoVec<T> {
13+
/// Converts the nested tuple into a `Vec<T>`.
14+
///
15+
/// # Examples
16+
///
17+
/// ```rust
18+
/// use tuplities_flatten_nest::NestedTupleIntoVec;
19+
///
20+
/// let nested = (1, (2, (3,)));
21+
/// let vec: Vec<i32> = nested.into_vec();
22+
/// assert_eq!(vec, vec![1, 2, 3]);
23+
/// ```
24+
fn into_vec(self) -> Vec<T>;
25+
26+
/// Appends the elements of the nested tuple to an existing vector.
27+
///
28+
/// This is used internally by `into_vec` for efficiency but can also be used directly.
29+
fn append_to_vec(self, buf: &mut Vec<T>);
30+
}
31+
32+
impl<T> NestedTupleIntoVec<T> for () {
33+
#[inline]
34+
fn into_vec(self) -> Vec<T> {
35+
Vec::new()
36+
}
37+
38+
#[inline]
39+
fn append_to_vec(self, _buf: &mut Vec<T>) {}
40+
}
41+
42+
impl<T> NestedTupleIntoVec<T> for (T,) {
43+
#[inline]
44+
fn into_vec(self) -> Vec<T> {
45+
let (t,) = self;
46+
vec![t]
47+
}
48+
49+
#[inline]
50+
fn append_to_vec(self, buf: &mut Vec<T>) {
51+
let (t,) = self;
52+
buf.push(t);
53+
}
54+
}
55+
56+
impl<T, Tail> NestedTupleIntoVec<T> for (T, Tail)
57+
where
58+
Tail: NestedTupleIntoVec<T>,
59+
{
60+
#[inline]
61+
fn into_vec(self) -> Vec<T> {
62+
let mut v = Vec::new();
63+
self.append_to_vec(&mut v);
64+
v
65+
}
66+
67+
#[inline]
68+
fn append_to_vec(self, buf: &mut Vec<T>) {
69+
let (head, tail) = self;
70+
buf.push(head);
71+
tail.append_to_vec(buf);
72+
}
73+
}
74+
75+
#[cfg(test)]
76+
mod tests {
77+
use super::*;
78+
79+
#[test]
80+
fn test_into_vec_empty() {
81+
let nested: () = ();
82+
let v: Vec<i32> = nested.into_vec();
83+
assert_eq!(v, Vec::<i32>::new());
84+
}
85+
86+
#[test]
87+
fn test_into_vec_single() {
88+
let nested = (1,);
89+
let v = nested.into_vec();
90+
assert_eq!(v, vec![1]);
91+
}
92+
93+
#[test]
94+
fn test_into_vec_nested() {
95+
let nested = (1, (2, (3,)));
96+
let v = nested.into_vec();
97+
assert_eq!(v, vec![1, 2, 3]);
98+
}
99+
}

tuplities/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ pub mod prelude {
1818
FlattenMatrixElements, FlattenNestedTuple, FlattenNestedTupleMatrix, IntoNestedTupleOption,
1919
NestMatrixElements, NestTuple, NestTupleMatrix, NestTupleMut, NestTupleRef,
2020
NestedTupleChain, NestedTupleFlattenOption, NestedTupleFrom, NestedTupleIndex,
21-
NestedTupleIndexMut, NestedTupleInto, NestedTupleMut, NestedTupleOption,
22-
NestedTupleOptionFrom, NestedTupleOptionInto, NestedTupleOptionTryFrom,
21+
NestedTupleIndexMut, NestedTupleInto, NestedTupleIntoVec, NestedTupleMut,
22+
NestedTupleOption, NestedTupleOptionFrom, NestedTupleOptionInto, NestedTupleOptionTryFrom,
2323
NestedTupleOptionTryInto, NestedTupleOptionWith, NestedTuplePopBack, NestedTuplePopFront,
2424
NestedTuplePushBack, NestedTuplePushFront, NestedTupleRef, NestedTupleReplicate,
2525
NestedTupleRow, NestedTupleRowMut, NestedTupleStartsWith, NestedTupleTryFrom,

0 commit comments

Comments
 (0)