use std::{
convert::Infallible,
future::{Future, Ready},
time::Duration,
};
pub trait OnRetry<E> {
type Future: Future<Output = ()> + Send + 'static;
fn on_retry(
&mut self,
attempt: u32,
next_delay: Option<Duration>,
previous_error: &E,
) -> Self::Future;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NoOnRetry {
cannot_exist: Infallible,
}
impl<E> OnRetry<E> for NoOnRetry {
type Future = Ready<()>;
#[inline]
fn on_retry(&mut self, _: u32, _: Option<Duration>, _: &E) -> Self::Future {
match self.cannot_exist {}
}
}
impl<F, E, FutureT> OnRetry<E> for F
where
F: Fn(u32, Option<Duration>, &E) -> FutureT,
FutureT: Future<Output = ()> + Send + 'static,
FutureT::Output: Send + 'static,
{
type Future = FutureT;
#[inline]
fn on_retry(
&mut self,
attempts: u32,
next_delay: Option<Duration>,
previous_error: &E,
) -> Self::Future {
self(attempts, next_delay, previous_error)
}
}