open_coroutine_core/common/
macros.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/// Constructs an event at the trace level.
#[allow(unused_macros)]
#[macro_export]
macro_rules! trace {
    ($( $args:expr ),*) => {
        #[cfg(all(debug_assertions, feature = "log"))]
        tracing::trace!( $( $args ),* );
    }
}

/// Constructs an event at the info level.
#[allow(unused_macros)]
#[macro_export]
macro_rules! info {
    ($( $args:expr ),*) => {
        #[cfg(feature = "log")]
        tracing::info!( $( $args ),* );
    }
}

/// Constructs an event at the warn level.
#[allow(unused_macros)]
#[macro_export]
macro_rules! warn {
    ($( $args:expr ),*) => {
        #[cfg(feature = "log")]
        tracing::warn!( $( $args ),* );
    }
}

/// Constructs an event at the error level.
#[allow(unused_macros)]
#[macro_export]
macro_rules! error {
    ($( $args:expr ),*) => {
        #[cfg(feature = "log")]
        tracing::error!( $( $args ),* );
    }
}

/// Catch panic.
#[macro_export]
macro_rules! catch {
    ($f:expr, $msg:expr, $arg:expr) => {
        std::panic::catch_unwind(std::panic::AssertUnwindSafe($f)).map_err(|e| {
            let message = if let Some(msg) = e.downcast_ref::<&'static str>() {
                *msg
            } else {
                $msg.leak()
            };
            $crate::error!("{} failed with error:{}", $arg, message);
            message
        })
    };
}

/// Fast impl `Display` trait for `Debug` types.
#[allow(unused_macros)]
#[macro_export]
macro_rules! impl_display_by_debug {
    ($struct_name:ident$(<$($generic1:tt $( : $trait_tt1: tt $( + $trait_tt2: tt)*)?),+>)?
        $(where $(
            $generic2:tt $( : $trait_tt3: tt $( + $trait_tt4: tt)*)?
        ),+)?
    ) => {
        impl$(<$($generic1 $( : $trait_tt1 $( + $trait_tt2)*)?),+>)? std::fmt::Display
            for $struct_name$(<$($generic1),+>)?
        where
            $($($generic2 $( : $trait_tt3 $( + $trait_tt4)*)?),+,)?
            $struct_name$(<$($generic1),+>)?: std::fmt::Debug,
        {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                std::fmt::Debug::fmt(self, f)
            }
        }
    };
}

/// Fast impl `Current` for a type.
/// This crate use `std` cause `#![no_std]` not support `thread_local!`.
#[allow(unused_macros)]
#[macro_export]
macro_rules! impl_current_for {
    (
        $name:ident,
        $struct_name:ident$(<$($generic1:tt $( : $trait_tt1: tt $( + $trait_tt2: tt)*)?),+>)?
        $(where $(
            $generic2:tt $( : $trait_tt3: tt $( + $trait_tt4: tt)*)?
        ),+)?
    ) => {
        thread_local! {
            static $name: std::cell::RefCell<std::collections::VecDeque<*const std::ffi::c_void>> = const { std::cell::RefCell::new(std::collections::VecDeque::new()) };
        }

        impl$(<$($generic1 $( : $trait_tt1 $( + $trait_tt2)*)?),+>)? $struct_name$(<$($generic1),+>)?
            $(where $($generic2 $( : $trait_tt3 $( + $trait_tt4)*)?),+)?
        {
            /// Init the current.
            pub(crate) fn init_current(current: &Self) {
                $name.with(|s| {
                    s.borrow_mut()
                        .push_front(core::ptr::from_ref(current).cast::<std::ffi::c_void>());
                });
            }

            /// Get the current if has.
            #[must_use]
            #[allow(unreachable_pub)]
            pub fn current<'current>() -> Option<&'current Self> {
                $name.with(|s| {
                    s.borrow()
                        .front()
                        .map(|ptr| unsafe { &*(*ptr).cast::<Self>() })
                })
            }

            /// Clean the current.
            pub(crate) fn clean_current() {
                $name.with(|s| _ = s.borrow_mut().pop_front());
            }
        }
    };
}

/// Fast impl common traits for `Named` types.
/// Check <https://www.rustwiki.org.cn/en/reference/introduction.html> for help information.
#[macro_export]
macro_rules! impl_for_named {
    ($struct_name:ident$(<$($generic1:tt $( : $trait_tt1: tt $( + $trait_tt2: tt)*)?),+>)?
        $(where $(
            $generic2:tt $( : $trait_tt3: tt $( + $trait_tt4: tt)*)?
        ),+)?
    ) => {
        impl$(<$($generic1 $( : $trait_tt1 $( + $trait_tt2)*)?),+>)? Eq
            for $struct_name$(<$($generic1),+>)?
        {
        }

        impl$(<$($generic1 $( : $trait_tt1 $( + $trait_tt2)*)?),+>)? PartialEq<Self>
            for $struct_name$(<$($generic1),+>)?
        {
            fn eq(&self, other: &Self) -> bool {
                self.name().eq(other.name())
            }
        }

        impl$(<$($generic1 $( : $trait_tt1 $( + $trait_tt2)*)?),+>)? Ord
            for $struct_name$(<$($generic1),+>)?
        {
            fn cmp(&self, other: &Self) -> std::cmp::Ordering {
                self.name().cmp(other.name())
            }
        }

        impl$(<$($generic1 $( : $trait_tt1 $( + $trait_tt2)*)?),+>)? PartialOrd<Self>
            for $struct_name$(<$($generic1),+>)?
        {
            fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
                Some(self.cmp(other))
            }
        }

        impl$(<$($generic1 $( : $trait_tt1 $( + $trait_tt2)*)?),+>)? std::hash::Hash
            for $struct_name$(<$($generic1),+>)?
        {
            fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
                self.name().hash(state)
            }
        }
    };
}