Files
Easytier/easytier/src/common/defer.rs
T
Sijie.Sun abf9d23d52 improve hole punching and stun test (#124)
* implement new stun test algorithm, do test faster and provide more info
* support punching for symmetric
2024-06-02 07:20:57 +08:00

25 lines
465 B
Rust

#[doc(hidden)]
pub struct Defer<F: FnOnce()> {
// internal struct used by defer! macro
func: Option<F>,
}
impl<F: FnOnce()> Defer<F> {
pub fn new(func: F) -> Self {
Self { func: Some(func) }
}
}
impl<F: FnOnce()> Drop for Defer<F> {
fn drop(&mut self) {
self.func.take().map(|f| f());
}
}
#[macro_export]
macro_rules! defer {
( $($tt:tt)* ) => {
let _deferred = $crate::common::defer::Defer::new(|| { $($tt)* });
};
}