abi_stable/macros/
internal.rs

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
macro_rules! deref_coerced_impl_cmp_traits {
    (
        $Self:ty;
        coerce_to = $coerce_to:ty,
        [$($Rhs:ty),* $(,)?]
    ) => {
        const _: () = {
            use std::cmp::{PartialEq, PartialOrd, Ordering};

            $(

                impl PartialEq<$Rhs> for $Self {
                    fn eq(&self, other: &$Rhs) -> bool {
                        <$coerce_to as PartialEq>::eq(self, other)
                    }
                }

                impl PartialOrd<$Rhs> for $Self {
                    fn partial_cmp(&self, other: &$Rhs) -> Option<Ordering> {
                        <$coerce_to as PartialOrd>::partial_cmp(self, other)
                    }
                }

                impl PartialEq<$Self> for $Rhs {
                    fn eq(&self, other: &$Self) -> bool {
                        <$coerce_to as PartialEq>::eq(self, other)
                    }
                }

                impl PartialOrd<$Self> for $Rhs {
                    fn partial_cmp(&self, other: &$Self) -> Option<Ordering> {
                        <$coerce_to as PartialOrd>::partial_cmp(self, other)
                    }
                }
            )*
        };
    };
}

macro_rules! slice_like_impl_cmp_traits {
    (
        impl $impl_params:tt $Self:ty,
        where $where:tt;
        $($Rhs:ty),* $(,)?
    ) => {
        $(
            slice_like_impl_cmp_traits!{
                @inner
                impl $impl_params $Self,
                where $where;
                $Rhs
            }
        )*
    };
    (@inner
        impl[$($impl_params:tt)*] $Self:ty,
        where[$($where:tt)*];
        $Rhs:ty
    ) => {
        const _: () = {
            use std::cmp::{PartialEq, PartialOrd, Ordering};

            impl<T: PartialEq<U>, U, $($impl_params)*> PartialEq<$Rhs> for $Self
            where $($where)*
            {
                fn eq(&self, other: &$Rhs) -> bool {
                    <[T] as PartialEq<[U]>>::eq(self, other)
                }
            }

            impl<T, U, $($impl_params)*> PartialOrd<$Rhs> for $Self
            where
                T: PartialOrd<U>,
                [T]: PartialOrd<[U]>,
                $($where)*
            {
                fn partial_cmp(&self, other: &$Rhs) -> Option<Ordering> {
                    <[T] as PartialOrd<[U]>>::partial_cmp(self, other)
                }
            }

            impl<U: PartialEq<T>, T, $($impl_params)*> PartialEq<$Self> for $Rhs
            where $($where)*
            {
                fn eq(&self, other: &$Self) -> bool {
                    <[U] as PartialEq<[T]>>::eq(self, other)
                }
            }

            impl<U, T, $($impl_params)*> PartialOrd<$Self> for $Rhs
            where
                U: PartialOrd<T>,
                [U]: PartialOrd<[T]>,
                $($where)*
            {
                fn partial_cmp(&self, other: &$Self) -> Option<Ordering> {
                    <[U] as PartialOrd<[T]>>::partial_cmp(self, other)
                }
            }
        };
    };
}

macro_rules! zst_assert {
    ($Self:ty) => {{
        ["Expected this to be Zero-sized"][(std::mem::size_of::<$Self>() != 0) as usize];
        ["Expected this to be 1 aligned"][(std::mem::align_of::<$Self>() != 1) as usize];

        ["Expected Tuple1<Self> to be Zero-sized"]
            [(std::mem::size_of::<crate::std_types::Tuple1<$Self>>() != 0) as usize];

        ["Expected Tuple1<Self> to be 1 aligned"]
            [(std::mem::align_of::<crate::std_types::Tuple1<$Self>>() != 1) as usize];

        ["Expected Tuple1<Self, Self> to be Zero-sized"]
            [(std::mem::size_of::<crate::std_types::Tuple2<$Self, $Self>>() != 0) as usize];

        ["Expected Tuple1<Self, Self> to be 1 aligned"]
            [(std::mem::align_of::<crate::std_types::Tuple2<$Self, $Self>>() != 1) as usize];
    }};
}

///////////////////////////////////////////////////////////////////////////////7

macro_rules! conditionally_const_docs {
    ($feature:literal) => {
        concat!(
            "# Conditional `const fn`\n",
            "\n",
            "This function requires the `",
            $feature,
            "` feature to be `const`-callable",
        )
    };
}

macro_rules! conditionally_const {
    (
        feature = $feature:literal
        $( #[$meta:meta] )*
        ;
        $( #[$bottom_meta:meta] )*
        $vis:vis
        $(unsafe $(@$safety:tt)?)?
        fn $fn_name:ident $([$($gen_args:tt)*])? ($($params:tt)*) -> $($rem:tt)*
    ) => (
        $(#[$meta])*
        #[doc = conditionally_const_docs!($feature)]
        $(#[$bottom_meta])*
        #[cfg(feature = $feature)]
        $vis const $(unsafe $($safety)?)?
        fn $fn_name $(<$($gen_args)*>)? ($($params)*) -> $($rem)*

        $(#[$meta])*
        #[doc = conditionally_const_docs!($feature)]
        $(#[$bottom_meta])*
        #[cfg(not(feature = $feature))]
        $vis $(unsafe $($safety)?)?
        fn $fn_name $(<$($gen_args)*>)? ($($params)*) -> $($rem)*
    )
}