abi_stable/macros/
internal.rs

1macro_rules! deref_coerced_impl_cmp_traits {
2    (
3        $Self:ty;
4        coerce_to = $coerce_to:ty,
5        [$($Rhs:ty),* $(,)?]
6    ) => {
7        const _: () = {
8            use std::cmp::{PartialEq, PartialOrd, Ordering};
9
10            $(
11
12                impl PartialEq<$Rhs> for $Self {
13                    fn eq(&self, other: &$Rhs) -> bool {
14                        <$coerce_to as PartialEq>::eq(self, other)
15                    }
16                }
17
18                impl PartialOrd<$Rhs> for $Self {
19                    fn partial_cmp(&self, other: &$Rhs) -> Option<Ordering> {
20                        <$coerce_to as PartialOrd>::partial_cmp(self, other)
21                    }
22                }
23
24                impl PartialEq<$Self> for $Rhs {
25                    fn eq(&self, other: &$Self) -> bool {
26                        <$coerce_to as PartialEq>::eq(self, other)
27                    }
28                }
29
30                impl PartialOrd<$Self> for $Rhs {
31                    fn partial_cmp(&self, other: &$Self) -> Option<Ordering> {
32                        <$coerce_to as PartialOrd>::partial_cmp(self, other)
33                    }
34                }
35            )*
36        };
37    };
38}
39
40macro_rules! slice_like_impl_cmp_traits {
41    (
42        impl $impl_params:tt $Self:ty,
43        where $where:tt;
44        $($Rhs:ty),* $(,)?
45    ) => {
46        $(
47            slice_like_impl_cmp_traits!{
48                @inner
49                impl $impl_params $Self,
50                where $where;
51                $Rhs
52            }
53        )*
54    };
55    (@inner
56        impl[$($impl_params:tt)*] $Self:ty,
57        where[$($where:tt)*];
58        $Rhs:ty
59    ) => {
60        const _: () = {
61            use std::cmp::{PartialEq, PartialOrd, Ordering};
62
63            impl<T: PartialEq<U>, U, $($impl_params)*> PartialEq<$Rhs> for $Self
64            where $($where)*
65            {
66                fn eq(&self, other: &$Rhs) -> bool {
67                    <[T] as PartialEq<[U]>>::eq(self, other)
68                }
69            }
70
71            impl<T, U, $($impl_params)*> PartialOrd<$Rhs> for $Self
72            where
73                T: PartialOrd<U>,
74                [T]: PartialOrd<[U]>,
75                $($where)*
76            {
77                fn partial_cmp(&self, other: &$Rhs) -> Option<Ordering> {
78                    <[T] as PartialOrd<[U]>>::partial_cmp(self, other)
79                }
80            }
81
82            impl<U: PartialEq<T>, T, $($impl_params)*> PartialEq<$Self> for $Rhs
83            where $($where)*
84            {
85                fn eq(&self, other: &$Self) -> bool {
86                    <[U] as PartialEq<[T]>>::eq(self, other)
87                }
88            }
89
90            impl<U, T, $($impl_params)*> PartialOrd<$Self> for $Rhs
91            where
92                U: PartialOrd<T>,
93                [U]: PartialOrd<[T]>,
94                $($where)*
95            {
96                fn partial_cmp(&self, other: &$Self) -> Option<Ordering> {
97                    <[U] as PartialOrd<[T]>>::partial_cmp(self, other)
98                }
99            }
100        };
101    };
102}
103
104macro_rules! zst_assert {
105    ($Self:ty) => {{
106        ["Expected this to be Zero-sized"][(std::mem::size_of::<$Self>() != 0) as usize];
107        ["Expected this to be 1 aligned"][(std::mem::align_of::<$Self>() != 1) as usize];
108
109        ["Expected Tuple1<Self> to be Zero-sized"]
110            [(std::mem::size_of::<crate::std_types::Tuple1<$Self>>() != 0) as usize];
111
112        ["Expected Tuple1<Self> to be 1 aligned"]
113            [(std::mem::align_of::<crate::std_types::Tuple1<$Self>>() != 1) as usize];
114
115        ["Expected Tuple1<Self, Self> to be Zero-sized"]
116            [(std::mem::size_of::<crate::std_types::Tuple2<$Self, $Self>>() != 0) as usize];
117
118        ["Expected Tuple1<Self, Self> to be 1 aligned"]
119            [(std::mem::align_of::<crate::std_types::Tuple2<$Self, $Self>>() != 1) as usize];
120    }};
121}
122
123///////////////////////////////////////////////////////////////////////////////7
124
125macro_rules! conditionally_const_docs {
126    ($feature:literal) => {
127        concat!(
128            "# Conditional `const fn`\n",
129            "\n",
130            "This function requires the `",
131            $feature,
132            "` feature to be `const`-callable",
133        )
134    };
135}
136
137macro_rules! conditionally_const {
138    (
139        feature = $feature:literal
140        $( #[$meta:meta] )*
141        ;
142        $( #[$bottom_meta:meta] )*
143        $vis:vis
144        $(unsafe $(@$safety:tt)?)?
145        fn $fn_name:ident $([$($gen_args:tt)*])? ($($params:tt)*) -> $($rem:tt)*
146    ) => (
147        $(#[$meta])*
148        #[doc = conditionally_const_docs!($feature)]
149        $(#[$bottom_meta])*
150        #[cfg(feature = $feature)]
151        $vis const $(unsafe $($safety)?)?
152        fn $fn_name $(<$($gen_args)*>)? ($($params)*) -> $($rem)*
153
154        $(#[$meta])*
155        #[doc = conditionally_const_docs!($feature)]
156        $(#[$bottom_meta])*
157        #[cfg(not(feature = $feature))]
158        $vis $(unsafe $($safety)?)?
159        fn $fn_name $(<$($gen_args)*>)? ($($params)*) -> $($rem)*
160    )
161}