1#[macro_export]
7macro_rules! impl_from {
8 ($a:ident < $c:lifetime > , $b:ident:: $enum_type:ident) => {
10 impl<$c> From<$a<$c>> for $b<$c> {
11 fn from(e: $a<$c>) -> Self {
12 $b::$enum_type(e)
13 }
14 }
15 };
16
17 ($a:ident, $b:ident:: $enum_type:ident) => {
19 impl From<$a> for $b {
20 fn from(e: $a) -> Self {
21 $b::$enum_type(e)
22 }
23 }
24 };
25}
26
27#[macro_export]
29macro_rules! impl_display {
30 ($enum:ident<$lt:lifetime>, {$($variant:pat => $fmt_string:expr),+$(,)* }) => {
32
33 impl<$lt> ::core::fmt::Display for $enum<$lt> {
34 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
35 use self::$enum::*;
36 match &self {
37 $(
38 $variant => write!(f, "{}", $fmt_string),
39 )+
40 }
41 }
42 }
43
44 };
45
46 ($enum:ident, {$($variant:pat => $fmt_string:expr),+$(,)* }) => {
48
49 impl ::core::fmt::Display for $enum {
50 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
51 use self::$enum::*;
52 match &self {
53 $(
54 $variant => write!(f, "{}", $fmt_string),
55 )+
56 }
57 }
58 }
59
60 };
61}
62
63#[macro_export]
67macro_rules! impl_callback_traits {
68 ($callback_value:ident) => {
69 impl ::core::fmt::Display for $callback_value {
70 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
71 write!(f, "{:?}", self)
72 }
73 }
74
75 impl ::core::fmt::Debug for $callback_value {
76 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
77 let callback = stringify!($callback_value);
78 write!(f, "{} @ 0x{:x}", callback, self.cb as *const () as usize)
79 }
80 }
81
82 impl ::core::hash::Hash for $callback_value {
83 fn hash<H>(&self, state: &mut H)
84 where
85 H: ::core::hash::Hasher,
86 {
87 state.write_usize(self.cb as *const () as usize);
88 }
89 }
90
91 impl PartialEq for $callback_value {
92 fn eq(&self, rhs: &Self) -> bool {
93 self.cb as *const () as usize == rhs.cb as usize
94 }
95 }
96
97 impl PartialOrd for $callback_value {
98 fn partial_cmp(&self, other: &Self) -> Option<::core::cmp::Ordering> {
99 Some((self.cb as *const () as usize).cmp(&(other.cb as *const () as usize)))
100 }
101 }
102
103 impl Ord for $callback_value {
104 fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
105 (self.cb as *const () as usize).cmp(&(other.cb as *const () as usize))
106 }
107 }
108
109 impl Eq for $callback_value {}
110 };
111}
112
113#[macro_export]
121macro_rules! impl_callback {
122 ($callback_value:ident, $callback_ty:ty) => {
124 $crate::impl_callback_traits!($callback_value);
125
126 impl Clone for $callback_value {
127 fn clone(&self) -> Self {
128 $callback_value {
129 cb: self.cb.clone(),
130 ctx: self.ctx.clone(),
131 }
132 }
133 }
134
135 impl From<$callback_ty> for $callback_value {
138 fn from(cb: $callback_ty) -> Self {
139 $callback_value {
140 cb,
141 ctx: $crate::refany::OptionRefAny::None,
142 }
143 }
144 }
145 };
146}
147
148#[macro_export]
153macro_rules! impl_callback_simple {
154 ($callback_value:ident) => {
155 $crate::impl_callback_traits!($callback_value);
156
157 impl Clone for $callback_value {
158 fn clone(&self) -> Self {
159 $callback_value {
160 cb: self.cb.clone(),
161 }
162 }
163 }
164
165 impl Copy for $callback_value {}
166 };
167}