secp256k1_sys/
macros.rs

1// SPDX-License-Identifier: CC0-1.0
2
3/// Implement methods and traits for types that contain an inner array.
4#[macro_export]
5macro_rules! impl_array_newtype {
6    ($thing:ident, $ty:ty, $len:expr) => {
7        impl $thing {
8            /// Like `cmp::Ord` but faster and with no guarantees across library versions.
9            ///
10            /// The inner byte array of `Self` is passed across the FFI boundry, as such there are
11            /// no guarantees on its layout and it is subject to change across library versions,
12            /// even minor versions. For this reason comparison function implementations (e.g.
13            /// `Ord`, `PartialEq`) take measures to ensure the data will remain constant (e.g., by
14            /// serializing it to a guaranteed format). This means they may be slow, this function
15            /// provides a faster comparison if you know that your types come from the same library
16            /// version.
17            pub fn cmp_fast_unstable(&self, other: &Self) -> core::cmp::Ordering {
18                self[..].cmp(&other[..])
19            }
20
21            /// Like `cmp::Eq` but faster and with no guarantees across library versions.
22            ///
23            /// The inner byte array of `Self` is passed across the FFI boundry, as such there are
24            /// no guarantees on its layout and it is subject to change across library versions,
25            /// even minor versions. For this reason comparison function implementations (e.g.
26            /// `Ord`, `PartialEq`) take measures to ensure the data will remain constant (e.g., by
27            /// serializing it to a guaranteed format). This means they may be slow, this function
28            /// provides a faster equality check if you know that your types come from the same
29            /// library version.
30            pub fn eq_fast_unstable(&self, other: &Self) -> bool {
31                self[..].eq(&other[..])
32            }
33        }
34
35        impl AsRef<[$ty; $len]> for $thing {
36            #[inline]
37            /// Gets a reference to the underlying array
38            fn as_ref(&self) -> &[$ty; $len] {
39                let &$thing(ref dat) = self;
40                dat
41            }
42        }
43
44        impl<I> core::ops::Index<I> for $thing
45        where
46            [$ty]: core::ops::Index<I>,
47        {
48            type Output = <[$ty] as core::ops::Index<I>>::Output;
49
50            #[inline]
51            fn index(&self, index: I) -> &Self::Output { &self.0[index] }
52        }
53
54        impl $crate::CPtr for $thing {
55            type Target = $ty;
56
57            fn as_c_ptr(&self) -> *const Self::Target {
58                let &$thing(ref dat) = self;
59                dat.as_ptr()
60            }
61
62            fn as_mut_c_ptr(&mut self) -> *mut Self::Target {
63                let &mut $thing(ref mut dat) = self;
64                dat.as_mut_ptr()
65            }
66        }
67    }
68}
69
70#[macro_export]
71macro_rules! impl_raw_debug {
72    ($thing:ident) => {
73        impl core::fmt::Debug for $thing {
74            fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
75                for i in self[..].iter().cloned() {
76                    write!(f, "{:02x}", i)?;
77                }
78                Ok(())
79            }
80        }
81     }
82}