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
//! Future types

use std::fmt;

use futures::{Future, Poll};

use crate::error::{Error, Overloaded};

/// Future for the `LoadShed` service.
pub struct ResponseFuture<F> {
    state: Result<F, ()>,
}

impl<F> ResponseFuture<F> {
    pub(crate) fn called(fut: F) -> Self {
        ResponseFuture { state: Ok(fut) }
    }

    pub(crate) fn overloaded() -> Self {
        ResponseFuture { state: Err(()) }
    }
}

impl<F> Future for ResponseFuture<F>
where
    F: Future,
    F::Error: Into<Error>,
{
    type Item = F::Item;
    type Error = Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.state {
            Ok(ref mut fut) => fut.poll().map_err(Into::into),
            Err(()) => Err(Overloaded::new().into()),
        }
    }
}

impl<F> fmt::Debug for ResponseFuture<F>
where
    // bounds for future-proofing...
    F: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("ResponseFuture")
    }
}