diff --git a/src/dispatching/dispatchers/filter/error_policy.rs b/src/dispatching/dispatchers/filter/error_policy.rs index db52e05a..91bd322b 100644 --- a/src/dispatching/dispatchers/filter/error_policy.rs +++ b/src/dispatching/dispatchers/filter/error_policy.rs @@ -1,21 +1,28 @@ use std::future::Future; use async_trait::async_trait; +use std::pin::Pin; /// Implementors of this trait are treated as error-handlers. #[async_trait] pub trait ErrorPolicy<E> { - async fn handle_error(&mut self, error: E); + async fn handle_error(&self, error: E) + where + E: 'async_trait; } -#[async_trait] impl<E, F, Fut> ErrorPolicy<E> for F where - F: FnMut(E) -> Fut + Send, + F: Fn(E) -> Fut + Sync, Fut: Future<Output = ()> + Send, E: Send, { - async fn handle_error(&mut self, error: E) { - self(error).await; + fn handle_error<'s, 'async_trait>(&'s self, error: E) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>> + where + 's: 'async_trait, + Self: 'async_trait, + E: 'async_trait + { + Box::pin(async move { self(error).await }) } } diff --git a/src/dispatching/dispatchers/filter/mod.rs b/src/dispatching/dispatchers/filter/mod.rs index c4203747..5d1b0a89 100644 --- a/src/dispatching/dispatchers/filter/mod.rs +++ b/src/dispatching/dispatchers/filter/mod.rs @@ -32,11 +32,12 @@ type Handlers<'a, T, E> = /// /// Simplest example: /// ```no_run -/// # use telebofr::Bot; -/// use telebofr::types::Message; -/// async fn run() { +/// # async fn run() { /// use std::convert::Infallible; +/// /// use telebofr::{ +/// Bot, +/// types::Message, /// dispatching::{ /// dispatchers::filter::{error_policy::ErrorPolicy, FilterDispatcher}, /// updater::polling, @@ -51,7 +52,7 @@ type Handlers<'a, T, E> = /// /// // create dispatching which handlers can't fail /// // with error policy that just ignores all errors (that can't ever happen) -/// let mut dp = FilterDispatcher::<Infallible>::new(ErrorPolicy::Ignore) +/// let mut dp = FilterDispatcher::<Infallible, _>::new(|_| async { () }) /// // Add 'handler' that will handle all messages sent to the bot /// .message_handler(true, |mes: Message| { /// async move { println!("New message: {:?}", mes) } @@ -66,9 +67,8 @@ type Handlers<'a, T, E> = /// ``` /// /// [`std::fmt::Debug`]: std::fmt::Debug -/// [Custom error policy]: -/// crate::dispatching::filter::error_policy::ErrorPolicy::Custom [updater]: -/// crate::dispatching::updater +/// [Custom error policy]: crate::dispatching::filter::error_policy::ErrorPolicy::Custom +/// [updater]: crate::dispatching::updater pub struct FilterDispatcher<'a, E, Ep> { message_handlers: Handlers<'a, Message, E>, edited_message_handlers: Handlers<'a, Message, E>, @@ -238,7 +238,7 @@ where .await; } - async fn handle<T>(&mut self, update: T, handlers: &Handlers<'a, T, E>) + async fn handle<T>(&self, update: T, handlers: &Handlers<'a, T, E>) where T: std::fmt::Debug, { @@ -287,9 +287,7 @@ mod tests { use crate::{ dispatching::{ - dispatchers::filter::{ - error_policy::FnErrorPolicy, FilterDispatcher, - }, + dispatchers::filter::FilterDispatcher, updater::StreamUpdater, }, types::{ @@ -303,10 +301,7 @@ mod tests { let counter = &AtomicI32::new(0); let counter2 = &AtomicI32::new(0); - let mut dp = - FilterDispatcher::<Infallible, _>::new(FnErrorPolicy(|_| { - async { () } - })) + let mut dp = FilterDispatcher::<Infallible, _>::new(|_| async { () } ) .message_handler(true, |_mes: Message| { async move { counter.fetch_add(1, Ordering::SeqCst);