frunk_core/
tuples.rs

1//! This module is held separate to put generated variadic generics for tuples
2//! at the end of the documentation so as to not disturb the reader when reading
3//! documentation.
4//!
5//! ```
6//! # use frunk_core::hlist::*;
7//! # use frunk_core::{hlist, HList};
8//! # fn main() {
9//! let h = hlist![ 42f32, true, "hello" ];
10//! let t: (f32, bool, &str) = h.into();
11//! assert_eq!(t, (42f32, true, "hello"));
12//!
13//! let t2 = (999, false, "world");
14//! let h2: HList![ isize, bool, &str ] = t2.into();
15//! assert_eq!(h2, hlist![ 999, false, "world" ]);
16//! # }
17//! ```
18
19use crate::generic::Generic;
20
21macro_rules! tup_def {
22    ( $($dtype: ident),* ; ; ) => {};
23    ( $($dtype: ident),* ;
24      $fone: ident $(, $ftype: ident)*, ;
25      $sone: ident $(, $stype: ident)*,
26    ) => {
27        tup_def!( $($dtype,)* $sone ; $($ftype,)* ; $($stype,)* );
28
29        impl< $($dtype: Default,)*
30                $fone , $sone:  From<$fone> ,
31              $($ftype, $stype: From<$ftype>,)*
32        > From< ( $fone, $( $ftype, )* ) >
33        for HList![$( $dtype, )* $sone, $( $stype, )* ] {
34            fn from(f: ( $fone, $( $ftype, )* )) -> Self {
35                #[allow(non_snake_case)]
36                let ( $fone, $( $ftype, )* ) = f;
37                hlist![$($dtype::default(),)* $fone.into(), $($ftype.into(),)*]
38            }
39        }
40    };
41}
42
43macro_rules! tup_iso {
44    ( $t: ident ) => {
45        impl<$t> Generic for ($t,) {
46            type Repr = HList![$t];
47            fn into(self) -> Self::Repr { hlist![self.0] }
48            fn from(r: Self::Repr) -> Self { (r.head,) }
49        }
50
51        impl<$t> From<($t,)> for HList![$t] {
52            fn from(tup: ($t,)) -> Self { Generic::into(tup) }
53        }
54
55        #[allow(clippy::from_over_into)]
56        impl<$t> Into<($t,)> for HList![$t] {
57            fn into(self) -> ($t,) { Generic::from(self) }
58        }
59    };
60
61    ( $type1: ident, $( $type: ident ),* ) => {
62        tup_iso!($( $type ),*);
63
64        impl<$type1, $($type),*> Generic for ($type1, $($type),*,) {
65            type Repr = HList![$type1, $($type),*,];
66
67            fn into(self) -> Self::Repr {
68                #[allow(non_snake_case)]
69                let ($type1, $($type),*,) = self;
70                hlist![$type1, $($type),*,]
71            }
72
73            fn from(r: Self::Repr) -> Self {
74                #[allow(non_snake_case)]
75                let hlist_pat![$type1, $($type),*,] = r;
76                ($type1, $($type),*,)
77            }
78        }
79
80        impl<$type1, $($type),*> From< ( $type1, $($type),*, ) >
81        for HList![ $type1, $($type),*, ] {
82            fn from(tup: ( $type1, $( $type ),*, ) ) -> Self {
83                Generic::into(tup)
84            }
85        }
86
87        #[allow(clippy::from_over_into)]
88        impl< $type1, $($type),*> Into< ( $type1, $($type),*, ) >
89        for HList![ $type1, $($type),*, ] {
90            fn into(self) -> ( $type1, $( $type ),*, ) {
91                Generic::from(self)
92            }
93        }
94    };
95}
96
97impl Generic for () {
98    type Repr = HList![];
99    fn into(self) -> Self::Repr {
100        hlist![]
101    }
102    fn from(_: Self::Repr) -> Self {}
103}
104
105impl From<()> for HList![] {
106    fn from(_: ()) -> Self {
107        hlist![]
108    }
109}
110
111tup_def!( T0 ; F1, ; T1, );
112tup_def!( T0 ; F1, F2, ;
113               T1, T2, );
114tup_def!( T0 ; F1, F2, F3, ;
115               T1, T2, T3, );
116tup_def!( T0 ; F1, F2, F3, F4, ;
117               T1, T2, T3, T4, );
118tup_def!( T0 ; F1, F2, F3, F4, F5, ;
119               T1, T2, T3, T4, T5, );
120tup_def!( T0 ; F1, F2, F3, F4, F5, F6, ;
121               T1, T2, T3, T4, T5, T6, );
122tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, ;
123               T1, T2, T3, T4, T5, T6, T7, );
124tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, ;
125               T1, T2, T3, T4, T5, T6, T7, T8, );
126tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, ;
127               T1, T2, T3, T4, T5, T6, T7, T8, T9, );
128tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, ;
129               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, );
130tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, ;
131               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, );
132tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, ;
133               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, );
134tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, ;
135               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,);
136tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, ;
137               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, );
138tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15,;
139               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, );
140tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, ;
141               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16,);
142tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, ;
143               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, );
144tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, ;
145               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18,);
146tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, ;
147               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19,);
148tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, ;
149               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,);
150tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, ;
151               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, );
152tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, ;
153               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, );
154tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23, ;
155               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, );
156
157tup_iso!(
158    T23, T22, T21, T20, T19, T18, T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4,
159    T3, T2, T1, T0
160);