const_format/
equality.rs

1#![allow(missing_docs, unused_variables)]
2
3use crate::wrapper_types::PWrapper;
4
5use core::{
6    cmp::Ordering,
7    marker::{PhantomData, PhantomPinned},
8    num::{
9        NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
10        NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
11    },
12    ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive},
13    sync::atomic::Ordering as AtomicOrdering,
14};
15
16////////////////////////////////////////////////////////////////////////////////
17
18macro_rules! slice_of_const_eq {($($elem:ty),* $(,)?) => (
19    $(
20        impl PWrapper<&[$elem]> {
21            /// This method is only available with the "assert" feature.
22            pub const fn const_eq(&self, other: &[$elem]) -> bool {
23                if self.0.len() != other.len() {
24                    return false;
25                }
26
27                __for_range!{i in 0..self.0.len() =>
28                    if !PWrapper(self.0[i]).const_eq(&other[i]) {
29                        return false
30                    }
31                }
32                true
33            }
34        }
35    )*
36)}
37
38slice_of_const_eq! {
39    &str,
40}
41
42macro_rules! slice_of_equal_op_impl {($($elem:ty),* $(,)?) => (
43    $(
44        impl PWrapper<&[$elem]> {
45            /// This method is only available with the "assert" feature.
46            pub const fn const_eq(&self, other: &[$elem]) -> bool {
47                if self.0.len() != other.len() {
48                    return false;
49                }
50
51                __for_range!{i in 0..self.0.len() =>
52                    if self.0[i] != other[i] {
53                        return false
54                    }
55                }
56                true
57            }
58        }
59    )*
60)}
61
62slice_of_equal_op_impl! {
63    bool,
64    char,
65    u8, i8,
66    u16, i16,
67    u32, i32,
68    u64, i64,
69    u128, i128,
70    usize, isize,
71}
72
73////////////////////////////////////////////////////////////////////////////////
74
75macro_rules! impl_eq_for_option_prim {
76    (
77        (l=$l:ident, r=$r:ident)
78        $( impl[$($impl_:tt)*] $type:ty = $comparison:expr; )*
79    ) => (
80        $(
81            impl<$($impl_)*> PWrapper<Option<$type>> {
82                /// This method is only available with the "assert" feature.
83                pub const fn const_eq(&self, other:&Option<$type>) -> bool {
84                    match (self.0, other) {
85                        (Some($l), Some($r)) => $comparison,
86                        (None, None) => true,
87                        _ => false,
88                    }
89                }
90            }
91        )*
92    )
93}
94
95impl_eq_for_option_prim! {
96    (l=l, r=r)
97    impl[] u8 = l == *r;
98    impl[] i8 = l == *r;
99    impl[] u16 = l == *r;
100    impl[] i16 = l == *r;
101    impl[] u32 = l == *r;
102    impl[] i32 = l == *r;
103    impl[] u64 = l == *r;
104    impl[] i64 = l == *r;
105    impl[] u128 = l == *r;
106    impl[] i128 = l == *r;
107    impl[] usize = l == *r;
108    impl[] isize = l == *r;
109    impl[] bool = l == *r;
110    impl[] char = l == *r;
111    impl[] &str = crate::slice_cmp::str_eq(l, r);
112}
113
114macro_rules! impl_eq_for_option {
115    (
116        (l=$l:ident, r=$r:ident)
117        $( impl[$($impl_:tt)*] $type:ty = $comparison:expr; )*
118    ) => (
119        $(
120            impl<$($impl_)*> PWrapper<$type> {
121                /// This method is only available with the "assert" feature.
122                pub const fn const_eq(&self, $r:&$type) -> bool {
123                    let $l = self.0;
124                    $comparison
125                }
126            }
127        )*
128
129        impl_eq_for_option_prim! {
130            (l=$l, r=$r)
131            $( impl[$($impl_)*] $type = $comparison; )*
132        }
133    )
134}
135
136impl_eq_for_option! {
137    (l=l, r=r)
138
139    impl[] NonZeroU8 = l.get() == r.get();
140    impl[] NonZeroI8 = l.get() == r.get();
141    impl[] NonZeroU16 = l.get() == r.get();
142    impl[] NonZeroI16 = l.get() == r.get();
143    impl[] NonZeroU32 = l.get() == r.get();
144    impl[] NonZeroI32 = l.get() == r.get();
145    impl[] NonZeroU64 = l.get() == r.get();
146    impl[] NonZeroI64 = l.get() == r.get();
147    impl[] NonZeroU128 = l.get() == r.get();
148    impl[] NonZeroI128 = l.get() == r.get();
149    impl[] NonZeroUsize = l.get() == r.get();
150    impl[] NonZeroIsize = l.get() == r.get();
151}
152
153macro_rules! impl_equality {
154    (
155        (l=$l:ident, r=$r:ident)
156
157        $( impl[$($impl_:tt)*] $type:ty = $comparison:expr ;)*
158    ) => (
159        $(
160            impl<$($impl_)*> PWrapper<$type> {
161                /// This method is only available with the "assert" feature.
162                #[inline(always)]
163                pub const fn const_eq(&self, $r: &$type) -> bool {
164                    let $l = &self.0;
165                    $comparison
166                }
167            }
168        )*
169    )
170}
171
172impl_equality! {
173    (l=l, r=r)
174
175    impl[T: ?Sized,] PhantomData<T> = true;
176    impl[] PhantomPinned = true;
177    impl[] () = true;
178
179    impl[] Ordering = *l as u8 == *r as u8;
180    impl[] AtomicOrdering = *l as u8 == *r as u8;
181
182    impl[] Range<usize>            = l.start == r.start && l.end == r.end;
183    impl[] RangeInclusive<usize>   = *l.start() == *r.start() && *l.end() == *r.end();
184    impl[] RangeFrom<usize>        = l.start == r.start;
185    impl[] RangeFull               = true;
186    impl[] RangeTo<usize>          = l.end == r.end;
187    impl[] RangeToInclusive<usize> = l.end == r.end;
188}