repr_offset/get_field_offset/
tuple_impls.rs

1use crate::{
2    alignment::{Aligned, Alignment, CombineAlignment},
3    get_field_offset::{
4        FieldOffsetWithVis, GetFieldOffset, ImplGetNestedFieldOffset, ImplsGetFieldOffset,
5    },
6    privacy::{CombinePrivacy, IsPublic, Privacy},
7};
8
9macro_rules! tuple_impl {
10    (
11        [$($field:ident)*],
12        [$($tp:ident)*],
13        [$($tp_trail:ident)*],
14        $first:ident,
15        $last:ident
16    ) => {
17        unsafe impl<T, $($field,)*>
18            GetFieldOffset<($($field,)*)>
19        for T
20        where
21            T: ImplsGetFieldOffset,
22            ImplGetNestedFieldOffset<T>: GetFieldOffset<($($field,)*)>
23        {
24            type Type = <ImplGetNestedFieldOffset<T> as GetFieldOffset<($($field,)*)>>::Type;
25            type Alignment = <ImplGetNestedFieldOffset<T> as GetFieldOffset<($($field,)*)>>::Alignment;
26            type Privacy = <ImplGetNestedFieldOffset<T> as GetFieldOffset<($($field,)*)>>::Privacy;
27
28            const OFFSET_WITH_VIS: FieldOffsetWithVis<
29                Self,
30                Self::Privacy,
31                ($($field,)*),
32                Self::Type,
33                Self::Alignment,
34            > = unsafe{
35                <ImplGetNestedFieldOffset<T> as GetFieldOffset<($($field,)*)>>::OFFSET_WITH_VIS
36                    .cast_struct()
37            };
38        }
39
40        unsafe impl<$($tp,)* $($field,)* $last, CombAlign, CombPriv>
41            GetFieldOffset<($($field,)*)>
42        for ImplGetNestedFieldOffset<$first>
43        where
44            $first: ImplsGetFieldOffset,
45            $(
46                $tp: GetFieldOffset<$field, Type = $tp_trail>,
47            )*
48            ($($tp::Alignment,)*): CombineAlignment<Aligned, Output = CombAlign>,
49            ($($tp::Privacy,)*): CombinePrivacy<IsPublic, Output = CombPriv>,
50            CombAlign: Alignment,
51            CombPriv: Privacy,
52        {
53            type Type = $last;
54            type Alignment = CombAlign;
55            type Privacy = CombPriv;
56
57            const OFFSET_WITH_VIS: FieldOffsetWithVis<
58                Self,
59                Self::Privacy,
60                ($($field,)*),
61                $last,
62                Self::Alignment,
63            > = unsafe{
64                let offset = {
65                    0
66                    $(
67                        + <$tp as GetFieldOffset<$field>>::OFFSET_WITH_VIS
68                            .private_field_offset()
69                            .offset()
70                    )*
71                };
72
73                FieldOffsetWithVis::new(offset)
74            };
75        }
76    };
77}
78
79/*
80fn main(){
81    for len in 2..=8 {
82        print!("tuple_impl! {{\n\t[");
83        for i in 0..len {
84            print!("F{} ", i)
85        }
86        print!("],\n\t[");
87        for i in 0..len {
88            print!("L{} ", i)
89        }
90        print!("],\n\t[");
91        for i in 1..=len {
92            print!("L{} ", i);
93        }
94        println!("],\n\tL{}, L{}\n}}", 0, len);
95    }
96}
97
98*/
99
100tuple_impl! {
101    [F0 F1 ],
102    [L0 L1 ],
103    [L1 L2 ],
104    L0, L2
105}
106tuple_impl! {
107    [F0 F1 F2 ],
108    [L0 L1 L2 ],
109    [L1 L2 L3 ],
110    L0, L3
111}
112tuple_impl! {
113    [F0 F1 F2 F3 ],
114    [L0 L1 L2 L3 ],
115    [L1 L2 L3 L4 ],
116    L0, L4
117}
118tuple_impl! {
119    [F0 F1 F2 F3 F4 ],
120    [L0 L1 L2 L3 L4 ],
121    [L1 L2 L3 L4 L5 ],
122    L0, L5
123}
124tuple_impl! {
125    [F0 F1 F2 F3 F4 F5 ],
126    [L0 L1 L2 L3 L4 L5 ],
127    [L1 L2 L3 L4 L5 L6 ],
128    L0, L6
129}
130tuple_impl! {
131    [F0 F1 F2 F3 F4 F5 F6 ],
132    [L0 L1 L2 L3 L4 L5 L6 ],
133    [L1 L2 L3 L4 L5 L6 L7 ],
134    L0, L7
135}
136tuple_impl! {
137    [F0 F1 F2 F3 F4 F5 F6 F7 ],
138    [L0 L1 L2 L3 L4 L5 L6 L7 ],
139    [L1 L2 L3 L4 L5 L6 L7 L8 ],
140    L0, L8
141}