Skip to main content

type_sets/
generate_sets.rs

1#![allow(unused)]
2use super::*;
3use std::{any::TypeId, marker::PhantomData, sync::OnceLock};
4
5macro_rules! generate_sets {
6    ($(
7        for<$($el:ident),*> $n:literal $($true:literal)? = {
8            pub struct $struct:ident;
9
10            trait $contains_n:ident:
11                $sub_contains:ident <$($sub_el:ident),*> +
12                $add_contains:ident <$add_el:ident>
13            {}
14        }
15    )*) => {
16        // Trait definitions
17        pub(crate) mod _priv {
18            use super::*;
19            $(
20                /// A private trait that defines the set of types contained in a type set.
21                pub trait $contains_n<$($el),*>:
22                    $sub_contains <$($sub_el),*>
23                    + $add_contains <$add_el>
24                {}
25            )*
26        }
27        pub(crate) use _priv::*;
28
29        $(
30            // Subset implementations for `dyn ContainsN`
31            #[diagnostic::do_not_recommend]
32            impl<S: ?Sized, $($el),*> Subset<S> for dyn $contains_n<$($el),*>
33                // where S: $contains_n<$($el),*>
34                where S: $(
35                    Contains1<$el> +
36                )*
37            {}
38
39            // Members implementations for `dyn ContainsN`
40            #[diagnostic::do_not_recommend]
41            impl<$($el),*> Members for dyn $contains_n<$($el),*>
42                where Self: 'static
43            {
44                fn members() -> &'static [TypeId]
45                where
46                    Self: 'static,
47                {
48                    static MEMBERS: OnceLock<[TypeId; $n]> = OnceLock::new();
49                    MEMBERS.get_or_init(|| [$(
50                        TypeId::of::<$el>()
51                    ),*])
52                }
53            }
54
55            // AsTypeSet implementations for tuples
56            #[diagnostic::do_not_recommend]
57            impl<$($el),*> AsTypeSet for ($($el,)*)
58            {
59                type Set = dyn $contains_n<$($el),*>;
60            }
61
62            // Contains implementations for `impl AsTypeSet`
63            #[diagnostic::do_not_recommend]
64            impl<S: ?Sized, $($el),*> $contains_n<$($el),*> for S
65            where
66                S: AsTypeSet<Set: $contains_n<$($el),*>>
67            {}
68
69            // AddMember implementations for tuples
70            #[diagnostic::do_not_recommend]
71            impl<$($el),*> Push for ($($el,)*) {
72                type Output<E> = ($($el,)* E);
73            }
74
75            // // Shrink implementations for tuples
76            // #[diagnostic::do_not_recommend]
77            // impl<$($el),*> Shrink for ($($el,)*) {
78            //     type Output = ($($sub_el,)*);
79            //     type Popped = $add_el;
80            // }
81        )*
82    };
83}
84
85mod _priv_0 {
86    use super::*;
87
88    pub trait Contains0 {}
89
90    impl AsTypeSet for () {
91        type Set = dyn Contains0;
92    }
93
94    #[diagnostic::do_not_recommend]
95    impl<S: ?Sized> Subset<S> for dyn Contains0 {}
96
97    #[diagnostic::do_not_recommend]
98    impl<S: ?Sized> Contains0 for S {}
99
100    impl Push for () {
101        type Output<E> = (E,);
102    }
103
104    // impl Shrink for () {
105    //     type Output = ();
106    //     type Popped =
107    // }
108
109    pub trait Contains1<E>: Contains0 {}
110
111    #[diagnostic::do_not_recommend]
112    impl<E1> AsTypeSet for (E1,) {
113        type Set = dyn Contains1<E1>;
114    }
115
116    impl<S: ?Sized, E1> Contains1<E1> for S where S: AsTypeSet<Set: Contains1<E1>> {}
117
118    #[diagnostic::do_not_recommend]
119    impl<S: ?Sized, E1> Subset<S> for dyn Contains1<E1> where S: Contains1<E1> {}
120
121    #[diagnostic::do_not_recommend]
122    impl<E1: 'static> Members for dyn Contains1<E1> {
123        fn members() -> &'static [TypeId] {
124            static MEMBERS: OnceLock<[TypeId; 1]> = OnceLock::new();
125            MEMBERS.get_or_init(|| [TypeId::of::<E1>()])
126        }
127    }
128
129    impl<E1> Push for (E1,) {
130        type Output<E> = (E1, E);
131    }
132
133    // impl<E1> Shrink for (E1,) {
134    //     type Output = ();
135    //     type Popped = E1;
136    // }
137}
138pub(crate) use _priv_0::*;
139
140// #[diagnostic::do_not_recommend]
141// impl<S: ?Sized, E1> Contains1<E1> for S where S: Contains0 + Contains1<E1> {}
142
143impl AsTypeSet for Infallible {
144    type Set = dyn Contains0;
145}
146
147#[cfg(test)]
148mod test {
149    use super::*;
150
151    fn is_superset<T: ?Sized, R: ?Sized>()
152    where
153        T: Superset<R>,
154    {
155    }
156
157    fn is_subset<T: ?Sized, R: ?Sized>()
158    where
159        T: Subset<R>,
160    {
161    }
162
163    fn test() {
164        is_subset::<dyn Contains3<u8, u16, u32>, dyn Contains4<u8, u16, u64, u32>>();
165
166        is_subset::<(u8, u32), (u8, u16, u64, u32)>();
167    }
168}
169
170generate_sets! {
171    // Set0 / Contains0 is implemented manually below, because it has no constraints
172
173    // for<E1> 1 = {
174    //     pub struct Set1;
175    //     trait Contains1: Contains0<> + Contains0<> {}
176    // }
177
178    for<E1, E2> 2 = {
179        pub struct Set2;
180        trait Contains2: Contains1<E1> + Contains1<E2> {}
181    }
182
183    for<E1, E2, E3> 3 = {
184        pub struct Set3;
185        trait Contains3: Contains2<E1, E2> + Contains1<E3> {}
186    }
187
188    for<E1, E2, E3, E4> 4 = {
189        pub struct Set4;
190        trait Contains4: Contains3<E1, E2, E3> + Contains1<E4> {}
191    }
192
193    for<E1, E2, E3, E4, E5> 5 = {
194        pub struct Set5;
195        trait Contains5: Contains4<E1, E2, E3, E4> + Contains1<E5> {}
196    }
197
198    for<E1, E2, E3, E4, E5, E6> 6 = {
199        pub struct Set6;
200        trait Contains6: Contains5<E1, E2, E3, E4, E5> + Contains1<E6> {}
201    }
202
203    for<E1, E2, E3, E4, E5, E6, E7> 7 = {
204        pub struct Set7;
205        trait Contains7: Contains6<E1, E2, E3, E4, E5, E6> + Contains1<E7> {}
206    }
207
208    for<E1, E2, E3, E4, E5, E6, E7, E8> 8 = {
209        pub struct Set8;
210        trait Contains8: Contains7<E1, E2, E3, E4, E5, E6, E7> + Contains1<E8> {}
211    }
212
213    for<E1, E2, E3, E4, E5, E6, E7, E8, E9> 9 = {
214        pub struct Set9;
215        trait Contains9: Contains8<E1, E2, E3, E4, E5, E6, E7, E8> + Contains1<E9> {}
216    }
217
218    for<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10> 10 = {
219        pub struct Set10;
220        trait Contains10: Contains9<E1, E2, E3, E4, E5, E6, E7, E8, E9> + Contains1<E10> {}
221    }
222
223    for<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11> 11 = {
224        pub struct Set11;
225        trait Contains11: Contains10<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10> + Contains1<E11> {}
226    }
227
228    for<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12> 12 = {
229        pub struct Set12;
230        trait Contains12: Contains11<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11> + Contains1<E12> {}
231    }
232
233    for<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13> 13 = {
234        pub struct Set13;
235        trait Contains13: Contains12<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12> + Contains1<E13> {}
236    }
237
238    for<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14> 14 = {
239        pub struct Set14;
240        trait Contains14: Contains13<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13> + Contains1<E14> {}
241    }
242
243    for<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15> 15 = {
244        pub struct Set15;
245        trait Contains15: Contains14<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14> + Contains1<E15> {}
246    }
247
248    for<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16> 16 = {
249        pub struct Set16;
250        trait Contains16: Contains15<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15> + Contains1<E16> {}
251    }
252
253    for<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17> 17 = {
254        pub struct Set17;
255        trait Contains17: Contains16<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16> + Contains1<E17> {}
256    }
257
258    for<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18> 18 = {
259        pub struct Set18;
260        trait Contains18: Contains17<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17> + Contains1<E18> {}
261    }
262
263    for<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18, E19> 19 = {
264        pub struct Set19;
265        trait Contains19: Contains18<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18> + Contains1<E19> {}
266    }
267
268    for<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18, E19, E20> 20 = {
269        pub struct Set20;
270        trait Contains20: Contains19<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18, E19> + Contains1<E20> {}
271    }
272
273    for<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18, E19, E20, E21> 21 = {
274        pub struct Set21;
275        trait Contains21: Contains20<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18, E19, E20> + Contains1<E21> {}
276    }
277
278    for<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18, E19, E20, E21, E22> 22 = {
279        pub struct Set22;
280        trait Contains22: Contains21<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18, E19, E20, E21> + Contains1<E22> {}
281    }
282
283    for<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18, E19, E20, E21, E22, E23> 23 = {
284        pub struct Set23;
285        trait Contains23: Contains22<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18, E19, E20, E21, E22> + Contains1<E23> {}
286    }
287
288    for<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18, E19, E20, E21, E22, E23, E24> 24 = {
289        pub struct Set24;
290        trait Contains24: Contains23<E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, E18, E19, E20, E21, E22, E23> + Contains1<E24> {}
291    }
292}