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;
20use crate::{hlist, HList};
21
22macro_rules! tup_def {
23    ( $($dtype: ident),* ; ; ) => {};
24    ( $($dtype: ident),* ;
25      $fone: ident $(, $ftype: ident)*, ;
26      $sone: ident $(, $stype: ident)*,
27    ) => {
28        tup_def!( $($dtype,)* $sone ; $($ftype,)* ; $($stype,)* );
29
30        impl< $($dtype: Default,)*
31                $fone , $sone:  From<$fone> ,
32              $($ftype, $stype: From<$ftype>,)*
33        > From< ( $fone, $( $ftype, )* ) >
34        for HList![$( $dtype, )* $sone, $( $stype, )* ] {
35            fn from(f: ( $fone, $( $ftype, )* )) -> Self {
36                #[allow(non_snake_case)]
37                let ( $fone, $( $ftype, )* ) = f;
38                hlist![$($dtype::default(),)* $fone.into(), $($ftype.into(),)*]
39            }
40        }
41    };
42}
43
44macro_rules! tup_iso {
45    ( $t: ident ) => {
46        impl<$t> Generic for ($t,) {
47            type Repr = HList![$t];
48            fn into(self) -> Self::Repr { hlist![self.0] }
49            fn from(r: Self::Repr) -> Self { (r.head,) }
50        }
51
52        impl<$t> From<($t,)> for HList![$t] {
53            fn from(tup: ($t,)) -> Self { Generic::into(tup) }
54        }
55
56        #[allow(clippy::from_over_into)]
57        impl<$t> Into<($t,)> for HList![$t] {
58            fn into(self) -> ($t,) { Generic::from(self) }
59        }
60    };
61
62    ( $type1: ident, $( $type: ident ),* ) => {
63        tup_iso!($( $type ),*);
64
65        impl<$type1, $($type),*> Generic for ($type1, $($type),*,) {
66            type Repr = HList![$type1, $($type),*,];
67
68            fn into(self) -> Self::Repr {
69                #[allow(non_snake_case)]
70                let ($type1, $($type),*,) = self;
71                hlist![$type1, $($type),*,]
72            }
73
74            fn from(r: Self::Repr) -> Self {
75                #[allow(non_snake_case)]
76                let hlist_pat![$type1, $($type),*,] = r;
77                ($type1, $($type),*,)
78            }
79        }
80
81        impl<$type1, $($type),*> From< ( $type1, $($type),*, ) >
82        for HList![ $type1, $($type),*, ] {
83            fn from(tup: ( $type1, $( $type ),*, ) ) -> Self {
84                Generic::into(tup)
85            }
86        }
87
88        #[allow(clippy::from_over_into)]
89        impl< $type1, $($type),*> Into< ( $type1, $($type),*, ) >
90        for HList![ $type1, $($type),*, ] {
91            fn into(self) -> ( $type1, $( $type ),*, ) {
92                Generic::from(self)
93            }
94        }
95    };
96}
97
98impl Generic for () {
99    type Repr = HList![];
100    fn into(self) -> Self::Repr {
101        hlist![]
102    }
103    fn from(_: Self::Repr) -> Self {}
104}
105
106impl From<()> for HList![] {
107    fn from(_: ()) -> Self {
108        hlist![]
109    }
110}
111
112tup_def!( T0 ; F1, ; T1, );
113tup_def!( T0 ; F1, F2, ;
114               T1, T2, );
115tup_def!( T0 ; F1, F2, F3, ;
116               T1, T2, T3, );
117tup_def!( T0 ; F1, F2, F3, F4, ;
118               T1, T2, T3, T4, );
119tup_def!( T0 ; F1, F2, F3, F4, F5, ;
120               T1, T2, T3, T4, T5, );
121tup_def!( T0 ; F1, F2, F3, F4, F5, F6, ;
122               T1, T2, T3, T4, T5, T6, );
123tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, ;
124               T1, T2, T3, T4, T5, T6, T7, );
125tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, ;
126               T1, T2, T3, T4, T5, T6, T7, T8, );
127tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, ;
128               T1, T2, T3, T4, T5, T6, T7, T8, T9, );
129tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, ;
130               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, );
131tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, ;
132               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, );
133tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, ;
134               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, );
135tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, ;
136               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,);
137tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, ;
138               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, );
139tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15,;
140               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, );
141tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, ;
142               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16,);
143tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, ;
144               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, );
145tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, ;
146               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18,);
147tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, ;
148               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19,);
149tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, ;
150               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20,);
151tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, ;
152               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, );
153tup_def!( T0 ; F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, ;
154               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, );
155tup_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, ;
156               T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, );
157
158tup_iso!(
159    T23, T22, T21, T20, T19, T18, T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4,
160    T3, T2, T1, T0
161);