diff options
| author | Fuwn <[email protected]> | 2023-04-06 06:15:05 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-04-06 06:15:05 +0000 |
| commit | aacd65efbde95fce96a291426fdf03d1dc6ba938 (patch) | |
| tree | 13c697236e262d42cee3417540aaeb147249570a | |
| parent | feat(macros): async response macros (diff) | |
| download | windmark-aacd65efbde95fce96a291426fdf03d1dc6ba938.tar.xz windmark-aacd65efbde95fce96a291426fdf03d1dc6ba938.zip | |
refactor(router): block macro
| -rw-r--r-- | src/router.rs | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/router.rs b/src/router.rs index f0ba97e..4b13f8d 100644 --- a/src/router.rs +++ b/src/router.rs @@ -45,6 +45,14 @@ use crate::{ response::Response, }; +macro_rules! block { + ($body:expr) => { + tokio::task::block_in_place(|| { + tokio::runtime::Handle::current().block_on(async { $body }); + }); + }; +} + macro_rules! or_error { ($stream:ident, $operation:expr, $error_format:literal) => { match $operation { @@ -740,12 +748,10 @@ impl Router { &mut self, mut module: impl AsyncModule + 'static + Send, ) -> &mut Self { - tokio::task::block_in_place(|| { - tokio::runtime::Handle::current().block_on(async { - module.on_attach(self).await; + block!({ + module.on_attach(self).await; - (*self.async_modules.lock().await).push(Box::new(module)); - }); + (*self.async_modules.lock().await).push(Box::new(module)); }); self |