tower_util/optional/
mod.rs

1//! Contains `OptionService` and related types and functions.
2//!
3//! See `OptionService` documentation for more details.
4//!
5
6/// Error types for `OptionalService`.
7pub mod error;
8/// Future types for `OptionalService`.
9pub mod future;
10
11use self::{error::Error, future::ResponseFuture};
12use std::task::{Context, Poll};
13use tower_service::Service;
14
15/// Optionally forwards requests to an inner service.
16///
17/// If the inner service is `None`, `Error::None` is returned as the response.
18#[derive(Debug)]
19pub struct Optional<T> {
20    inner: Option<T>,
21}
22
23impl<T> Optional<T> {
24    /// Create a new `OptionService`
25    pub fn new<Request>(inner: Option<T>) -> Optional<T>
26    where
27        T: Service<Request>,
28        T::Error: Into<Error>,
29    {
30        Optional { inner }
31    }
32}
33
34impl<T, Request> Service<Request> for Optional<T>
35where
36    T: Service<Request>,
37    T::Error: Into<Error>,
38{
39    type Response = T::Response;
40    type Error = Error;
41    type Future = ResponseFuture<T::Future>;
42
43    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
44        match self.inner {
45            Some(ref mut inner) => match inner.poll_ready(cx) {
46                Poll::Ready(r) => Poll::Ready(r.map_err(Into::into)),
47                Poll::Pending => Poll::Pending,
48            },
49            // None services are always ready
50            None => Poll::Ready(Ok(())),
51        }
52    }
53
54    fn call(&mut self, request: Request) -> Self::Future {
55        let inner = self.inner.as_mut().map(|i| i.call(request));
56        ResponseFuture::new(inner)
57    }
58}