tuplities_remove/lib.rs
1//! A trait for removing elements at specific indices from tuples.
2//!
3//! This crate provides the `TupleRemove<Idx>` trait, which allows removing an element
4//! at a compile-time known index from a tuple, returning the element and the
5//! remaining tuple.
6
7#![no_std]
8
9/// A trait for removing an element at a specific index from a tuple.
10///
11/// This trait allows removing an element at compile-time known index `Idx`
12/// from a tuple, returning the element and the remaining tuple.
13///
14/// # Examples
15///
16/// ```
17/// use tuplities_remove::TupleRemove;
18/// use typenum::U1;
19///
20/// let tuple = (1, "hello", 3.14);
21/// let (removed, remainder) = TupleRemove::<U1>::remove(tuple);
22/// assert_eq!(removed, "hello");
23/// assert_eq!(remainder, (1, 3.14));
24/// ```
25#[tuplities_derive::impl_remove]
26pub trait TupleRemove<Idx: typenum::Unsigned> {
27 /// The type of the element being removed.
28 type Type;
29
30 /// The type of the remaining tuple after removing.
31 type Remainder;
32
33 /// Removes the element at index `Idx` from the tuple.
34 ///
35 /// Returns a tuple containing the removed element and the remaining tuple.
36 fn remove(self) -> (Self::Type, Self::Remainder);
37}
38
39#[cfg(test)]
40mod tests {
41 use super::TupleRemove;
42 use typenum::{U0, U1};
43
44 #[test]
45 fn test_remove_single_element_tuple() {
46 let tuple = (42,);
47 let (removed, _remainder): (i32, ()) = TupleRemove::<U0>::remove(tuple);
48 // Check types: (i32,) remove U0 -> Type = i32, Remainder = ()
49 let expected_removed: i32 = 42;
50 assert_eq!(removed, expected_removed);
51 }
52
53 #[test]
54 fn test_remove_two_element_tuple() {
55 // Remove at U0
56 let tuple = (1, 2);
57 let (removed0, remainder0) = TupleRemove::<U0>::remove(tuple);
58 let expected_removed0: i32 = 1;
59 let expected_remainder0: (i32,) = (2,);
60 assert_eq!(removed0, expected_removed0);
61 assert_eq!(remainder0, expected_remainder0);
62
63 // Remove at U1
64 let tuple = (1, 2);
65 let (removed1, remainder1) = TupleRemove::<U1>::remove(tuple);
66 let expected_removed1: i32 = 2;
67 let expected_remainder1: (i32,) = (1,);
68 assert_eq!(removed1, expected_removed1);
69 assert_eq!(remainder1, expected_remainder1);
70 }
71}