From aacd65efbde95fce96a291426fdf03d1dc6ba938 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 6 Apr 2023 06:15:05 +0000 Subject: refactor(router): block macro --- src/router.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src') 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 -- cgit v1.2.3