1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::repeat::Repeat;
use crate::request::{BaseRequest, Request};
use crate::response::Response;

/// A request that can be sent just once.
pub trait OneshotRequest<C>: BaseRequest {
    /// The type of corresponding responses of this request.
    type Response: Response<Ok = Self::Ok, Error = Self::Error>;

    /// Send this request to the given client, by consuming itself.
    fn send_once(self, client: C) -> Self::Response;

    fn repeat(self) -> Repeat<Self>
    where
        Self: Clone,
    {
        Repeat::from(self)
    }
}

impl<R, C> OneshotRequest<C> for Box<R>
where
    R: OneshotRequest<C>,
{
    type Response = R::Response;
    fn send_once(self, client: C) -> Self::Response {
        let inner = *self;
        inner.send_once(client)
    }
}

/// An [`OneshotRequest`] adaptor for types that implements [`Request`].
#[derive(Clone)]
pub struct Oneshot<R> {
    inner: R,
}

impl<R> From<R> for Oneshot<R> {
    fn from(req: R) -> Self {
        Oneshot { inner: req }
    }
}

impl<R> BaseRequest for Oneshot<R>
where
    R: BaseRequest,
{
    type Ok = R::Ok;
    type Error = R::Error;
}

impl<R, C> OneshotRequest<C> for Oneshot<R>
where
    R: Request<C>,
{
    type Response = R::Response;

    fn send_once(self, client: C) -> Self::Response {
        self.inner.send(client)
    }
}

#[cfg(feature = "backoff")]
mod impl_retry {
    use std::time::Duration;

    use super::Oneshot;
    use crate::retry::RetriableRequest;

    impl<R> RetriableRequest for Oneshot<R>
    where
        R: RetriableRequest,
    {
        fn should_retry(&self, error: &Self::Error, next_interval: Duration) -> bool {
            self.inner.should_retry(error, next_interval)
        }
    }
}