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
use crate::{
    alignment::{Aligned, Alignment, CombineAlignment},
    get_field_offset::{
        FieldOffsetWithVis, GetFieldOffset, ImplGetNestedFieldOffset, ImplsGetFieldOffset,
    },
    privacy::{CombinePrivacy, IsPublic, Privacy},
};

macro_rules! tuple_impl {
    (
        [$($field:ident)*],
        [$($tp:ident)*],
        [$($tp_trail:ident)*],
        $first:ident,
        $last:ident
    ) => {
        unsafe impl<T, $($field,)*>
            GetFieldOffset<($($field,)*)>
        for T
        where
            T: ImplsGetFieldOffset,
            ImplGetNestedFieldOffset<T>: GetFieldOffset<($($field,)*)>
        {
            type Type = <ImplGetNestedFieldOffset<T> as GetFieldOffset<($($field,)*)>>::Type;
            type Alignment = <ImplGetNestedFieldOffset<T> as GetFieldOffset<($($field,)*)>>::Alignment;
            type Privacy = <ImplGetNestedFieldOffset<T> as GetFieldOffset<($($field,)*)>>::Privacy;

            const OFFSET_WITH_VIS: FieldOffsetWithVis<
                Self,
                Self::Privacy,
                ($($field,)*),
                Self::Type,
                Self::Alignment,
            > = unsafe{
                <ImplGetNestedFieldOffset<T> as GetFieldOffset<($($field,)*)>>::OFFSET_WITH_VIS
                    .cast_struct()
            };
        }

        unsafe impl<$($tp,)* $($field,)* $last, CombAlign, CombPriv>
            GetFieldOffset<($($field,)*)>
        for ImplGetNestedFieldOffset<$first>
        where
            $first: ImplsGetFieldOffset,
            $(
                $tp: GetFieldOffset<$field, Type = $tp_trail>,
            )*
            ($($tp::Alignment,)*): CombineAlignment<Aligned, Output = CombAlign>,
            ($($tp::Privacy,)*): CombinePrivacy<IsPublic, Output = CombPriv>,
            CombAlign: Alignment,
            CombPriv: Privacy,
        {
            type Type = $last;
            type Alignment = CombAlign;
            type Privacy = CombPriv;

            const OFFSET_WITH_VIS: FieldOffsetWithVis<
                Self,
                Self::Privacy,
                ($($field,)*),
                $last,
                Self::Alignment,
            > = unsafe{
                let offset = {
                    0
                    $(
                        + <$tp as GetFieldOffset<$field>>::OFFSET_WITH_VIS
                            .private_field_offset()
                            .offset()
                    )*
                };

                FieldOffsetWithVis::new(offset)
            };
        }
    };
}

/*
fn main(){
    for len in 2..=8 {
        print!("tuple_impl! {{\n\t[");
        for i in 0..len {
            print!("F{} ", i)
        }
        print!("],\n\t[");
        for i in 0..len {
            print!("L{} ", i)
        }
        print!("],\n\t[");
        for i in 1..=len {
            print!("L{} ", i);
        }
        println!("],\n\tL{}, L{}\n}}", 0, len);
    }
}

*/

tuple_impl! {
    [F0 F1 ],
    [L0 L1 ],
    [L1 L2 ],
    L0, L2
}
tuple_impl! {
    [F0 F1 F2 ],
    [L0 L1 L2 ],
    [L1 L2 L3 ],
    L0, L3
}
tuple_impl! {
    [F0 F1 F2 F3 ],
    [L0 L1 L2 L3 ],
    [L1 L2 L3 L4 ],
    L0, L4
}
tuple_impl! {
    [F0 F1 F2 F3 F4 ],
    [L0 L1 L2 L3 L4 ],
    [L1 L2 L3 L4 L5 ],
    L0, L5
}
tuple_impl! {
    [F0 F1 F2 F3 F4 F5 ],
    [L0 L1 L2 L3 L4 L5 ],
    [L1 L2 L3 L4 L5 L6 ],
    L0, L6
}
tuple_impl! {
    [F0 F1 F2 F3 F4 F5 F6 ],
    [L0 L1 L2 L3 L4 L5 L6 ],
    [L1 L2 L3 L4 L5 L6 L7 ],
    L0, L7
}
tuple_impl! {
    [F0 F1 F2 F3 F4 F5 F6 F7 ],
    [L0 L1 L2 L3 L4 L5 L6 L7 ],
    [L1 L2 L3 L4 L5 L6 L7 L8 ],
    L0, L8
}