bitcoin_internals/
macros.rs1#[macro_export]
8macro_rules! impl_array_newtype {
9 ($thing:ident, $ty:ty, $len:literal) => {
10 impl $thing {
11 #[inline]
13 pub fn as_ptr(&self) -> *const $ty {
14 let &$thing(ref dat) = self;
15 dat.as_ptr()
16 }
17
18 #[inline]
20 pub fn as_mut_ptr(&mut self) -> *mut $ty {
21 let &mut $thing(ref mut dat) = self;
22 dat.as_mut_ptr()
23 }
24
25 #[inline]
27 pub fn len(&self) -> usize { $len }
28
29 #[inline]
31 pub fn is_empty(&self) -> bool { false }
32 }
33
34 impl<'a> core::convert::From<[$ty; $len]> for $thing {
35 fn from(data: [$ty; $len]) -> Self { $thing(data) }
36 }
37
38 impl<'a> core::convert::From<&'a [$ty; $len]> for $thing {
39 fn from(data: &'a [$ty; $len]) -> Self { $thing(*data) }
40 }
41
42 impl<'a> core::convert::TryFrom<&'a [$ty]> for $thing {
43 type Error = core::array::TryFromSliceError;
44
45 fn try_from(data: &'a [$ty]) -> Result<Self, Self::Error> {
46 use core::convert::TryInto;
47
48 Ok($thing(data.try_into()?))
49 }
50 }
51
52 impl AsRef<[$ty; $len]> for $thing {
53 fn as_ref(&self) -> &[$ty; $len] { &self.0 }
54 }
55
56 impl AsMut<[$ty; $len]> for $thing {
57 fn as_mut(&mut self) -> &mut [$ty; $len] { &mut self.0 }
58 }
59
60 impl AsRef<[$ty]> for $thing {
61 fn as_ref(&self) -> &[$ty] { &self.0 }
62 }
63
64 impl AsMut<[$ty]> for $thing {
65 fn as_mut(&mut self) -> &mut [$ty] { &mut self.0 }
66 }
67
68 impl core::borrow::Borrow<[$ty; $len]> for $thing {
69 fn borrow(&self) -> &[$ty; $len] { &self.0 }
70 }
71
72 impl core::borrow::BorrowMut<[$ty; $len]> for $thing {
73 fn borrow_mut(&mut self) -> &mut [$ty; $len] { &mut self.0 }
74 }
75
76 impl core::borrow::Borrow<[$ty]> for $thing {
78 fn borrow(&self) -> &[$ty] { &self.0 }
79 }
80
81 impl core::borrow::BorrowMut<[$ty]> for $thing {
82 fn borrow_mut(&mut self) -> &mut [$ty] { &mut self.0 }
83 }
84
85 impl<I> core::ops::Index<I> for $thing
86 where
87 [$ty]: core::ops::Index<I>,
88 {
89 type Output = <[$ty] as core::ops::Index<I>>::Output;
90
91 #[inline]
92 fn index(&self, index: I) -> &Self::Output { &self.0[index] }
93 }
94 };
95}
96
97#[macro_export]
99macro_rules! debug_from_display {
100 ($thing:ident) => {
101 impl core::fmt::Debug for $thing {
102 fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
103 core::fmt::Display::fmt(self, f)
104 }
105 }
106 };
107}
108
109#[macro_export]
111macro_rules! const_assert {
112 ($x:expr) => {{
113 const _: [(); 0 - !$x as usize] = [];
114 }};
115}