1
//! Utility macros for implementing common trait patterns on callback types
2
//! and enum conversions (`From`, `Display`). Used by the `core`, `layout`,
3
//! and `css` crates.
4

            
5
/// Implement the `From` trait for any type.
6
#[macro_export]
7
macro_rules! impl_from {
8
    // From a type with a lifetime to a type which also has a lifetime
9
    ($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
    // From a type without a lifetime to a type which also does not have a lifetime
18
    ($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
/// Implement `Display` for an enum.
28
#[macro_export]
29
macro_rules! impl_display {
30
    // For a type with a lifetime
31
    ($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
    // For a type without a lifetime
47
    ($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
/// Helper macro implementing the shared trait impls (`Display`, `Debug`, `Hash`,
64
/// `PartialEq`, `Eq`, `PartialOrd`, `Ord`) for callback types.
65
/// Used internally by [`impl_callback!`] and [`impl_callback_simple!`].
66
#[macro_export]
67
macro_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
/// Implements `Display, Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord`
114
/// for a callback struct with `.cb` (function pointer) and `.ctx` (`OptionRefAny`) fields.
115
/// Also implements `From<$callback_ty>` to create a callback from a raw function pointer.
116
///
117
/// For callbacks with only a `.cb` field (no `.ctx`), use [`impl_callback_simple!`] instead.
118
///
119
/// This is necessary to work around for https://github.com/rust-lang/rust/issues/54508
120
#[macro_export]
121
macro_rules! impl_callback {
122
    // Version with callable field (for UI callbacks that need FFI support)
123
    ($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
        /// Allow creating callback from a raw function pointer
136
        /// Sets callable to None (for native Rust/C usage)
137
        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 to implement callback traits for simple system callbacks (no callable field)
149
///
150
/// Use this for destructor callbacks, system callbacks, and other internal callbacks
151
/// that don't need FFI callable support.
152
#[macro_export]
153
macro_rules! impl_callback_simple {
154
    ($callback_value:ident) => {
155
        $crate::impl_callback_traits!($callback_value);
156

            
157
        impl Clone for $callback_value {
158
156612
            fn clone(&self) -> Self {
159
156612
                $callback_value {
160
156612
                    cb: self.cb.clone(),
161
156612
                }
162
156612
            }
163
        }
164

            
165
        impl Copy for $callback_value {}
166
    };
167
}