tuplities_split/
lib.rs

1#![no_std]
2
3//! [tuplities](https://github.com/lucacappelletti94/tuplities) suite crate providing the `TupleSplit` trait.
4
5/// A trait for splitting a tuple at a compile-time known index.
6///
7/// This trait allows splitting a tuple at a specific compile-time known index `Idx`,
8/// returning two tuples: the elements before the index and the elements at and after the index.
9///
10/// # Examples
11///
12/// ```rust
13/// use tuplities_split::TupleSplit;
14/// use typenum::U2;
15///
16/// let tuple = (1, 2, 3, 4);
17/// let (left, right) = TupleSplit::<U2>::split(tuple);
18/// assert_eq!(left, (1, 2));
19/// assert_eq!(right, (3, 4));
20/// ```
21///
22/// Part of the [`tuplities`](https://docs.rs/tuplities/latest/tuplities/) crate.
23#[tuplities_derive::impl_split]
24pub trait TupleSplit<Idx: typenum::Unsigned> {
25    /// The type of the tuple containing elements before the split index.
26    type Left;
27
28    /// The type of the tuple containing elements at and after the split index.
29    type Right;
30
31    /// Splits the tuple at index `Idx`, returning two tuples.
32    fn split(self) -> (Self::Left, Self::Right);
33}
34
35#[cfg(test)]
36mod tests {
37    use super::TupleSplit;
38    use typenum::{U0, U1, U2, U3};
39
40    #[test]
41    fn test_split_at_zero() {
42        let tuple = (1, 2, 3);
43        let (left, right) = TupleSplit::<U0>::split(tuple);
44        assert_eq!(left, ());
45        assert_eq!(right, (1, 2, 3));
46    }
47
48    #[test]
49    fn test_split_at_middle() {
50        let tuple = (1, 2, 3, 4);
51        let (left, right) = TupleSplit::<U2>::split(tuple);
52        assert_eq!(left, (1, 2));
53        assert_eq!(right, (3, 4));
54    }
55
56    #[test]
57    fn test_split_at_end() {
58        let tuple = (1, 2, 3);
59        let (left, right) = TupleSplit::<U3>::split(tuple);
60        assert_eq!(left, (1, 2, 3));
61        assert_eq!(right, ());
62    }
63
64    #[test]
65    fn test_split_empty_tuple() {
66        let tuple = ();
67        let (left, right) = TupleSplit::<U0>::split(tuple);
68        assert_eq!(left, ());
69        assert_eq!(right, ());
70    }
71
72    #[test]
73    fn test_split_single_element_at_zero() {
74        let tuple = (42,);
75        let (left, right) = TupleSplit::<U0>::split(tuple);
76        assert_eq!(left, ());
77        assert_eq!(right, (42,));
78    }
79
80    #[test]
81    fn test_split_single_element_at_one() {
82        let tuple = (42,);
83        let (left, right) = TupleSplit::<U1>::split(tuple);
84        assert_eq!(left, (42,));
85        assert_eq!(right, ());
86    }
87
88    #[test]
89    fn test_split_two_elements_at_zero() {
90        let tuple = (1, 2);
91        let (left, right) = TupleSplit::<U0>::split(tuple);
92        assert_eq!(left, ());
93        assert_eq!(right, (1, 2));
94    }
95
96    #[test]
97    fn test_split_two_elements_at_one() {
98        let tuple = (1, 2);
99        let (left, right) = TupleSplit::<U1>::split(tuple);
100        assert_eq!(left, (1,));
101        assert_eq!(right, (2,));
102    }
103
104    #[test]
105    fn test_split_two_elements_at_two() {
106        let tuple = (1, 2);
107        let (left, right) = TupleSplit::<U2>::split(tuple);
108        assert_eq!(left, (1, 2));
109        assert_eq!(right, ());
110    }
111}