tower_util/optional/
mod.rs

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
//! Contains `OptionService` and related types and functions.
//!
//! See `OptionService` documentation for more details.
//!

/// Error types for `OptionalService`.
pub mod error;
/// Future types for `OptionalService`.
pub mod future;

use self::{error::Error, future::ResponseFuture};
use std::task::{Context, Poll};
use tower_service::Service;

/// Optionally forwards requests to an inner service.
///
/// If the inner service is `None`, `Error::None` is returned as the response.
#[derive(Debug)]
pub struct Optional<T> {
    inner: Option<T>,
}

impl<T> Optional<T> {
    /// Create a new `OptionService`
    pub fn new<Request>(inner: Option<T>) -> Optional<T>
    where
        T: Service<Request>,
        T::Error: Into<Error>,
    {
        Optional { inner }
    }
}

impl<T, Request> Service<Request> for Optional<T>
where
    T: Service<Request>,
    T::Error: Into<Error>,
{
    type Response = T::Response;
    type Error = Error;
    type Future = ResponseFuture<T::Future>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        match self.inner {
            Some(ref mut inner) => match inner.poll_ready(cx) {
                Poll::Ready(r) => Poll::Ready(r.map_err(Into::into)),
                Poll::Pending => Poll::Pending,
            },
            // None services are always ready
            None => Poll::Ready(Ok(())),
        }
    }

    fn call(&mut self, request: Request) -> Self::Future {
        let inner = self.inner.as_mut().map(|i| i.call(request));
        ResponseFuture::new(inner)
    }
}