Fix ErrorPolicy by implementing trait on all Fn() -> Fut by-hands (and some related small fixes)

This commit is contained in:
Waffle 2019-11-02 15:55:54 +03:00
parent ea2a2c83d8
commit 08ebb3cc01
2 changed files with 25 additions and 23 deletions

View file

@ -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,
Fut: Future<Output = ()> + Send,
E: Send,
where
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 })
}
}

View file

@ -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);