mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-05-09 11:14:30 +00:00
e43537939a
1. clippy code 2. add fmt and clippy check in ci
27 lines
496 B
Rust
27 lines
496 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) {
|
|
if let Some(f) = self.func.take() {
|
|
f()
|
|
}
|
|
}
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! defer {
|
|
( $($tt:tt)* ) => {
|
|
let _deferred = $crate::common::defer::Defer::new(|| { $($tt)* });
|
|
};
|
|
}
|