From ba277991c9d0dcecbb4f06b59e0b82a9d48f8983 Mon Sep 17 00:00:00 2001 From: Hirrolot Date: Mon, 7 Feb 2022 15:53:52 +0600 Subject: [PATCH 1/3] Make the default and error handlers `Send + Sync` --- src/dispatching2/dispatcher.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/dispatching2/dispatcher.rs b/src/dispatching2/dispatcher.rs index 5e885101..b5268a9a 100644 --- a/src/dispatching2/dispatcher.rs +++ b/src/dispatching2/dispatcher.rs @@ -22,7 +22,7 @@ pub struct DispatcherBuilder { dependencies: DependencyMap, handler: UpdateHandler, default_handler: DefaultHandler, - error_handler: Arc>, + error_handler: Arc + Send + Sync>, } impl DispatcherBuilder @@ -36,7 +36,7 @@ where #[must_use] pub fn default_handler(self, handler: H) -> Self where - H: Fn(Arc) -> Fut + 'static, + H: Fn(Arc) -> Fut + Send + Sync + 'static, Fut: Future + Send + 'static, { let handler = Arc::new(handler); @@ -54,7 +54,7 @@ where /// /// By default, it is [`LoggingErrorHandler`]. #[must_use] - pub fn error_handler(self, handler: Arc>) -> Self { + pub fn error_handler(self, handler: Arc + Send + Sync>) -> Self { Self { error_handler: handler, ..self } } @@ -90,7 +90,7 @@ pub struct Dispatcher { handler: UpdateHandler, default_handler: DefaultHandler, - error_handler: Arc>, + error_handler: Arc + Send + Sync>, // TODO: respect allowed_udpates allowed_updates: HashSet, @@ -103,7 +103,7 @@ pub struct Dispatcher { /// A handler that processes updates from Telegram. pub type UpdateHandler = dptree::Handler<'static, DependencyMap, Result<(), Err>>; -type DefaultHandler = Box) -> BoxFuture<'static, ()>>; +type DefaultHandler = Box) -> BoxFuture<'static, ()> + Send + Sync>; impl Dispatcher where From 62da2cacb843bf8c39b5242e6ef2ee7d9d445c2c Mon Sep 17 00:00:00 2001 From: Hirrolot Date: Mon, 7 Feb 2022 15:58:34 +0600 Subject: [PATCH 2/3] Update the changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60f13485..e47ff9cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## unreleased +### Changed + +- Make `DispatcherBuilder::{default_handler, error_handler}` accept a handler that implements `Send + Sync` ([PR 517](https://github.com/teloxide/teloxide/pull/517)). + ## 0.6.1 - 2022-02-06 ### Fixed From 3ab5763afbd6cd4fb450e35bc9511cbd2adeb182 Mon Sep 17 00:00:00 2001 From: Hirrolot Date: Tue, 8 Feb 2022 03:54:30 +0600 Subject: [PATCH 3/3] Add `test_tokio_spawn` --- src/dispatching2/dispatcher.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/dispatching2/dispatcher.rs b/src/dispatching2/dispatcher.rs index b5268a9a..369fd1a2 100644 --- a/src/dispatching2/dispatcher.rs +++ b/src/dispatching2/dispatcher.rs @@ -272,3 +272,27 @@ where self.state.clone() } } + +#[cfg(test)] +mod tests { + use std::convert::Infallible; + + use teloxide_core::Bot; + + use super::*; + + #[tokio::test] + async fn test_tokio_spawn() { + tokio::spawn(async { + // Just check that this code compiles. + if false { + Dispatcher::<_, Infallible>::builder(Bot::new(""), dptree::entry()) + .build() + .dispatch() + .await; + } + }) + .await + .unwrap(); + } +}