std_traits/
tuple.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::{array::Array, primitive::Primitive};

pub trait Tuple: Primitive {
    const N: usize;
}

pub trait HomogeneousTuple<Item>: Tuple {
    type Array: Array<Item = Item>;

    fn into_array(self) -> Self::Array
    where
        Self: Sized;
    fn from_array(array: Self::Array) -> Self;
}

impl Primitive for () {}
impl Tuple for () {
    const N: usize = 0;
}
impl<T> HomogeneousTuple<T> for () {
    type Array = [T; 0];

    fn into_array(self) -> Self::Array
    where
        Self: Sized,
    {
        []
    }

    fn from_array(_array: Self::Array) -> Self {
        #[allow(clippy::unused_unit)]
        ()
    }
}

#[cfg_attr(docsrs, doc(fake_variadic))]
#[cfg_attr(
    docsrs,
    doc = "This trait is implemented for tuples up to 12 items long."
)]
impl<T1: ?Sized> Primitive for (T1,) {}

#[cfg_attr(docsrs, doc(fake_variadic))]
#[cfg_attr(
    docsrs,
    doc = "This trait is implemented for tuples up to 12 items long."
)]
impl<T1: ?Sized> Tuple for (T1,) {
    const N: usize = 1;
}

#[cfg_attr(docsrs, doc(fake_variadic))]
#[cfg_attr(
    docsrs,
    doc = "This trait is implemented for tuples up to 12 items long."
)]
impl<T> HomogeneousTuple<T> for (T,) {
    type Array = [T; 1];

    fn into_array(self) -> Self::Array
    where
        Self: Sized,
    {
        self.into()
    }

    fn from_array(array: Self::Array) -> Self {
        array.into()
    }
}

macro_rules! replace_expr {
    ($_t:tt $sub:tt) => {
        $sub
    };
}

macro_rules! homogeneous_tuple {
    ($($types:tt),*) => {
        (
            $(
                replace_expr!(
                    ($types)
                    T
                ),
            )*
        )
    };
}

macro_rules! impl_tuple {
    ($n:expr => $($types:tt $i:tt),*; $last:tt $last_i:tt) => {
        #[cfg_attr(docsrs, doc(hidden))]
        impl<$($types,)* $last: ?Sized> Primitive for ($($types,)* $last,) {}
        #[cfg_attr(docsrs, doc(hidden))]
        impl<$($types,)* $last: ?Sized> Tuple for ($($types,)* $last,) {
            const N: usize = $n;
        }
        #[cfg_attr(docsrs, doc(hidden))]
        impl<T> HomogeneousTuple<T> for homogeneous_tuple!($($types,)* $last) {
            type Array = [T; $n];

            fn into_array(self) -> Self::Array
            where
                Self: Sized
            {
                self.into()
            }

            fn from_array(array: Self::Array) -> Self {
                array.into()
            }
        }
    }
}

/*
for n in range(2, 13):
    print(f"impl_tuple!({n} => {', '.join(f'T{i} {i - 1}' for i in range(1, n))}; T{n} {n - 1});")
*/
impl_tuple!(2 => T1 0; T2 1);
impl_tuple!(3 => T1 0, T2 1; T3 2);
impl_tuple!(4 => T1 0, T2 1, T3 2; T4 3);
impl_tuple!(5 => T1 0, T2 1, T3 2, T4 3; T5 4);
impl_tuple!(6 => T1 0, T2 1, T3 2, T4 3, T5 4; T6 5);
impl_tuple!(7 => T1 0, T2 1, T3 2, T4 3, T5 4, T6 5; T7 6);
impl_tuple!(8 => T1 0, T2 1, T3 2, T4 3, T5 4, T6 5, T7 6; T8 7);
impl_tuple!(9 => T1 0, T2 1, T3 2, T4 3, T5 4, T6 5, T7 6, T8 7; T9 8);
impl_tuple!(10 => T1 0, T2 1, T3 2, T4 3, T5 4, T6 5, T7 6, T8 7, T9 8; T10 9);
impl_tuple!(11 => T1 0, T2 1, T3 2, T4 3, T5 4, T6 5, T7 6, T8 7, T9 8, T10 9; T11 10);
impl_tuple!(12 => T1 0, T2 1, T3 2, T4 3, T5 4, T6 5, T7 6, T8 7, T9 8, T10 9, T11 10; T12 11);

#[cfg(test)]
mod test {
    use super::*;
    extern crate std;
    use std::{format, prelude::rust_2021::*, println};

    macro_rules! test_from_array {
        ($($types:tt),+) => {
            let arr  = std::array::from_fn(|i| format!("{i}"));
            println!("{arr:?}");
            let tuple = <($($types,)+)>::from_array(arr.clone());
            let arr2 = tuple.into_array();
            println!("{arr2:?}");
            assert_eq!(arr2, arr);
        }
    }

    #[test]
    fn test_from_array_0() {
        let arr: [String; 0] = [];
        #[allow(clippy::let_unit_value)]
        let tuple = <()>::from_array(arr);
        let _: [String; 0] = tuple.into_array();
    }

    #[test]
    fn test_into_array_change_type_0_elements() {
        let arr: [String; 0] = [];
        #[allow(clippy::let_unit_value)]
        let tuple = <()>::from_array(arr);
        let _: [u8; 0] = tuple.into_array();
    }

    #[test]
    fn test_from_array_1() {
        test_from_array!(String);
    }

    #[test]
    fn test_from_array_2() {
        test_from_array!(String, String);
    }

    #[test]
    fn test_from_array_3() {
        test_from_array!(String, String, String);
    }

    #[test]
    fn test_from_array_12() {
        test_from_array!(
            String, String, String, String, String, String, String, String, String, String, String,
            String
        );
    }
}