summaryrefslogtreecommitdiff
path: root/ecs/src/tuple.rs
blob: def25a08506e0b5893a65f1e1a9a2eb9f35b0cc0 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use std::any::TypeId;

use paste::paste;
use seq_macro::seq;
use util_macros::sub;

pub trait Tuple: sealed::Sealed
{
    /// `Self` with the given type added as the last element.
    ///
    /// `(String, i32, u8)::WithElementAtEnd<Path> = (String, i32, u8, Path)`
    ///
    /// # Important note
    /// If `Self` has 16 elements, this will be `()`. The reason for this is that the
    /// `Tuple` trait is only implemented for tuples with up to and including 16 elements.
    type WithElementAtEnd<NewElem>: Tuple;

    /// `Self` without the last element.
    ///
    /// `(u16, AtomicU8)::WithoutLastElement = (u16,)`
    type WithoutLastElement: Tuple;

    /// The last element of `Self`.
    type LastElement;

    /// Self with all elements wrapped in [`Option`].
    type InOptions: Tuple;

    /// Pops the last element from this tuple, returning the new tuple and the popped
    /// element.
    fn pop_last(self) -> (Self::WithoutLastElement, Self::LastElement);

    /// Converts this tuple so that all elements are wrapped in [`Option`].
    fn into_in_options(self) -> Self::InOptions;
}

/// A tuple with element types that all have the lifetime `'static`.
pub trait WithAllElemLtStatic: Tuple + sealed::Sealed
{
    /// Returns the element at the given index.
    fn get_mut<Element: 'static>(&mut self, index: usize) -> Option<&mut Element>;
}

/// Using the type system, reduces the elements of a tuple to a single one. Each element
/// determines itself how it is handled.
pub trait Reduce<Operation>
{
    type Out;
}

pub trait ReduceElement<Accumulator, Operation>
{
    type Return;
}

macro_rules! tuple_reduce_elem_tuple {
    (overflow) => {
        ()
    };

    ($index: tt) => {
        paste! {
            [<Elem $index>]::Return
        }
    };
}

macro_rules! elem_type_by_index {
    (overflow) => {
        ()
    };

    ($index: tt) => {
        paste! {
            [<Elem $index>]
        }
    };
}

macro_rules! elem_by_index {
    (overflow) => {
        ()
    };

    ($index: tt, $self: ident) => {
        $self.$index
    };
}

macro_rules! all_except_last {
    (start $($rest: tt)*) => {
        all_except_last!(@[] $($rest)*)
    };

    (@[$($included_elem: ident,)*] $elem: ident $($rest: tt)+) => {
        all_except_last!(@[$($included_elem,)* $elem,] $($rest)*)
    };

    (@[$($included_elem: expr,)*] ($elem: expr) $($rest: tt)+) => {
        all_except_last!(@[$($included_elem,)* $elem,] $($rest)*)
    };

    (@[$($included_elem: ident,)*] $elem: ident) => {
        ($($included_elem,)*)
    };

    (@[$($included_elem: expr,)*] $elem: expr) => {
        ($($included_elem,)*)
    };

    (@[]) => {
        ()
    };
}

macro_rules! impl_tuple_traits {
    ($cnt: tt) => {
        seq!(I in 0..$cnt {
            impl<#(Elem~I,)*> Tuple for (#(Elem~I,)*)
            {
                type WithElementAtEnd<NewElem> = (#(Elem~I,)* NewElem,);

                type WithoutLastElement = all_except_last!(start #(Elem~I)*);

                type LastElement = sub!($cnt - 1, elem_type_by_index);

                type InOptions = (#(Option<Elem~I>,)*);

                fn pop_last(self) -> (Self::WithoutLastElement, Self::LastElement)
                {
                    (
                        all_except_last!(start #((self.I))*),
                        sub!($cnt - 1, elem_by_index, (self))
                    )
                }

                fn into_in_options(self) -> Self::InOptions
                {
                    #![allow(clippy::unused_unit)]
                    (#(Some(self.I),)*)
                }
            }

            impl<#(Elem~I: 'static,)*> WithAllElemLtStatic for (#(Elem~I,)*)
            {
                fn get_mut<Element: 'static>(&mut self, index: usize) -> Option<&mut Element>
                {
                    match index {
                        #(
                            I => {
                                assert!(TypeId::of::<Element>() == TypeId::of::<Elem~I>());

                                // SAFETY: It is checked above that the type is correct
                                Some(unsafe { &mut *(&raw mut self.I).cast::<Element>() })
                            }
                        )*
                        _ => None
                    }
                }
            }

            impl<#(Elem~I,)*> sealed::Sealed for (#(Elem~I,)*)
            {
            }

            paste! {
                impl<Operation, #(Elem~I,)*> Reduce<Operation> for (#(Elem~I,)*)
                where
                    #(
                        Elem~I: ReduceElement<
                            sub!(I - 1, tuple_reduce_elem_tuple), Operation
                        >,
                    )*
                {
                    type Out = sub!($cnt - 1, tuple_reduce_elem_tuple);
                }
            }
        });
    };
}

seq!(N in 0..16 {
    impl_tuple_traits!(N);
});

seq!(I in 0..16 {
    impl<#(Elem~I,)*> Tuple for (#(Elem~I,)*)
    {
        type WithElementAtEnd<NewElem> = ();

        type WithoutLastElement = all_except_last!(start #(Elem~I)*);

        type LastElement = Elem15;

        type InOptions = (#(Option<Elem~I>,)*);

        fn pop_last(self) -> (Self::WithoutLastElement, Self::LastElement)
        {
            (
                all_except_last!(start #((self.I))*),
                self.15
            )
        }

        fn into_in_options(self) -> Self::InOptions
        {
            #![allow(clippy::unused_unit)]
            (#(Some(self.I),)*)
        }
    }

    impl<#(Elem~I: 'static,)*> WithAllElemLtStatic for (#(Elem~I,)*)
    {
        fn get_mut<Element: 'static>(&mut self, index: usize) -> Option<&mut Element>
        {
            match index {
                #(
                    I => {
                        assert!(TypeId::of::<Element>() == TypeId::of::<Elem~I>());

                        // SAFETY: It is checked above that the type is correct
                        Some(unsafe { &mut *(&raw mut self.I).cast::<Element>() })
                    }
                )*
                _ => None
            }
        }
    }

    impl<#(Elem~I,)*> sealed::Sealed for (#(Elem~I,)*)
    {
    }
});

mod sealed
{
    pub trait Sealed {}
}