From 809aaef9b1da80d76bc21244f6592a08e3f99e93 Mon Sep 17 00:00:00 2001 From: Temirkhan Myrzamadi Date: Mon, 10 Feb 2020 00:29:30 +0600 Subject: [PATCH] Winning the compiler... --- src/dispatching/dispatcher.rs | 42 +++++++++++++++++++++-------------- src/dispatching/mod.rs | 3 ++- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/dispatching/dispatcher.rs b/src/dispatching/dispatcher.rs index 4edcef5c..61795838 100644 --- a/src/dispatching/dispatcher.rs +++ b/src/dispatching/dispatcher.rs @@ -83,7 +83,7 @@ where } #[must_use] - pub fn message_handler(mut self, h: H) -> Self + pub fn message_handler(mut self, h: &'a H) -> Self where H: CtxHandler, I> + 'a, I: Into> + 'a, @@ -93,7 +93,7 @@ where } #[must_use] - pub fn edited_message_handler(mut self, h: H) -> Self + pub fn edited_message_handler(mut self, h: &'a H) -> Self where H: CtxHandler, I> + 'a, I: Into> + 'a, @@ -104,7 +104,7 @@ where } #[must_use] - pub fn channel_post_handler(mut self, h: H) -> Self + pub fn channel_post_handler(mut self, h: &'a H) -> Self where H: CtxHandler, I> + 'a, I: Into> + 'a, @@ -115,7 +115,7 @@ where } #[must_use] - pub fn edited_channel_post_handler(mut self, h: H) -> Self + pub fn edited_channel_post_handler(mut self, h: &'a H) -> Self where H: CtxHandler, I> + 'a, I: Into> + 'a, @@ -126,7 +126,7 @@ where } #[must_use] - pub fn inline_query_handler(mut self, h: H) -> Self + pub fn inline_query_handler(mut self, h: &'a H) -> Self where H: CtxHandler, I> + 'a, I: Into> + 'a, @@ -137,7 +137,7 @@ where } #[must_use] - pub fn chosen_inline_result_handler(mut self, h: H) -> Self + pub fn chosen_inline_result_handler(mut self, h: &'a H) -> Self where H: CtxHandler, I> + 'a, I: Into> + 'a, @@ -148,7 +148,7 @@ where } #[must_use] - pub fn callback_query_handler(mut self, h: H) -> Self + pub fn callback_query_handler(mut self, h: &'a H) -> Self where H: CtxHandler, I> + 'a, I: Into> + 'a, @@ -159,7 +159,7 @@ where } #[must_use] - pub fn shipping_query_handler(mut self, h: H) -> Self + pub fn shipping_query_handler(mut self, h: &'a H) -> Self where H: CtxHandler, I> + 'a, I: Into> + 'a, @@ -170,7 +170,7 @@ where } #[must_use] - pub fn pre_checkout_query_handler(mut self, h: H) -> Self + pub fn pre_checkout_query_handler(mut self, h: &'a H) -> Self where H: CtxHandler, I> + 'a, I: Into> + 'a, @@ -181,7 +181,7 @@ where } #[must_use] - pub fn poll_handler(mut self, h: H) -> Self + pub fn poll_handler(mut self, h: &'a H) -> Self where H: CtxHandler, I> + 'a, I: Into> + 'a, @@ -191,7 +191,7 @@ where } #[must_use] - pub fn poll_answer_handler(mut self, h: H) -> Self + pub fn poll_answer_handler(mut self, h: &'a H) -> Self where H: CtxHandler, I> + 'a, I: Into> + 'a, @@ -323,8 +323,8 @@ where } } -// Transforms Future into Future by applying an Into -// conversion. +/// Transforms Future into Future by applying an Into +/// conversion. async fn intermediate_fut0(fut: impl Future) -> U where T: Into, @@ -332,12 +332,18 @@ where fut.await.into() } +/// Transforms CtxHandler with Into> as a return +/// value into CtxHandler with DispatcherHandlerResult return value. fn intermediate_fut1<'a, Upd, HandlerE, H, I>( - h: H, -) -> impl CtxHandler, DispatcherHandlerResult> + h: &'a H, +) -> impl CtxHandler< + DispatcherHandlerCtx, + DispatcherHandlerResult, +> + 'a where H: CtxHandler, I> + 'a, I: Into> + 'a, + Upd: 'a, { move |ctx| intermediate_fut0(h.handle_ctx(ctx)) } @@ -345,12 +351,14 @@ where /// Registers a single handler. fn register_handler<'a, Upd, H, I, HandlerE>( mut handlers: Handlers<'a, Upd, HandlerE>, - h: H, + h: &'a H, ) -> Handlers<'a, Upd, HandlerE> where H: CtxHandler, I> + 'a, I: Into> + 'a, + HandlerE: 'a, + Upd: 'a, { - // handlers.push(Box::new()); + handlers.push(Box::new(intermediate_fut1(h))); handlers } diff --git a/src/dispatching/mod.rs b/src/dispatching/mod.rs index 373c33ce..35ab0c65 100644 --- a/src/dispatching/mod.rs +++ b/src/dispatching/mod.rs @@ -26,8 +26,9 @@ //! // Setup logging here... //! //! Dispatcher::new(Bot::new("MyAwesomeToken")) -//! .message_handler(|ctx: DispatcherHandlerCtx| async move { +//! .message_handler(&|ctx: DispatcherHandlerCtx| async move { //! ctx.answer("pong").send().await?; +//! Ok(()) //! }) //! .dispatch() //! .await;