1use std::{
2 convert::Infallible,
3 future::{Future, Ready},
4 time::Duration,
5};
6
7pub trait OnRetry<E> {
13 type Future: Future<Output = ()> + Send + 'static;
15
16 fn on_retry(
20 &mut self,
21 attempt: u32,
22 next_delay: Option<Duration>,
23 previous_error: &E,
24 ) -> Self::Future;
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub struct NoOnRetry {
30 cannot_exist: Infallible,
31}
32
33impl<E> OnRetry<E> for NoOnRetry {
34 type Future = Ready<()>;
35
36 #[inline]
37 fn on_retry(&mut self, _: u32, _: Option<Duration>, _: &E) -> Self::Future {
38 match self.cannot_exist {}
39 }
40}
41
42impl<F, E, FutureT> OnRetry<E> for F
43where
44 F: Fn(u32, Option<Duration>, &E) -> FutureT,
45 FutureT: Future<Output = ()> + Send + 'static,
46 FutureT::Output: Send + 'static,
47{
48 type Future = FutureT;
49
50 #[inline]
51 fn on_retry(
52 &mut self,
53 attempts: u32,
54 next_delay: Option<Duration>,
55 previous_error: &E,
56 ) -> Self::Future {
57 self(attempts, next_delay, previous_error)
58 }
59}