tower_util/
lib.rs

1#![doc(html_root_url = "https://docs.rs/tower-util/0.3.1")]
2#![warn(
3    missing_debug_implementations,
4    missing_docs,
5    rust_2018_idioms,
6    unreachable_pub
7)]
8#![allow(elided_lifetimes_in_paths)]
9
10//! Various utility types and functions that are generally with Tower.
11
12mod boxed;
13#[cfg(feature = "call-all")]
14mod call_all;
15mod either;
16mod oneshot;
17mod optional;
18mod ready;
19mod service_fn;
20
21pub use crate::{
22    boxed::{BoxService, UnsyncBoxService},
23    either::Either,
24    oneshot::Oneshot,
25    optional::Optional,
26    ready::{Ready, ReadyAnd, ReadyOneshot},
27    service_fn::{service_fn, ServiceFn},
28};
29
30#[cfg(feature = "call-all")]
31pub use crate::call_all::{CallAll, CallAllUnordered};
32
33#[doc(hidden)]
34pub type Error = Box<dyn std::error::Error + Send + Sync>;
35
36pub mod error {
37    //! Error types
38
39    pub use crate::optional::error as optional;
40}
41
42pub mod future {
43    //! Future types
44
45    pub use crate::optional::future as optional;
46}
47
48/// An extension trait for `Service`s that provides a variety of convenient
49/// adapters
50pub trait ServiceExt<Request>: tower_service::Service<Request> {
51    /// Resolves when the service is ready to accept a request.
52    #[deprecated(since = "0.3.1", note = "prefer `ready_and` which yields the service")]
53    fn ready(&mut self) -> Ready<'_, Self, Request>
54    where
55        Self: Sized,
56    {
57        #[allow(deprecated)]
58        Ready::new(self)
59    }
60
61    /// Yields a mutable reference to the service when it is ready to accept a request.
62    fn ready_and(&mut self) -> ReadyAnd<'_, Self, Request>
63    where
64        Self: Sized,
65    {
66        ReadyAnd::new(self)
67    }
68
69    /// Yields the service when it is ready to accept a request.
70    fn ready_oneshot(self) -> ReadyOneshot<Self, Request>
71    where
72        Self: Sized,
73    {
74        ReadyOneshot::new(self)
75    }
76
77    /// Consume this `Service`, calling with the providing request once it is ready.
78    fn oneshot(self, req: Request) -> Oneshot<Self, Request>
79    where
80        Self: Sized,
81    {
82        Oneshot::new(self, req)
83    }
84
85    /// Process all requests from the given `Stream`, and produce a `Stream` of their responses.
86    ///
87    /// This is essentially `Stream<Item = Request>` + `Self` => `Stream<Item = Response>`. See the
88    /// documentation for [`CallAll`](struct.CallAll.html) for details.
89    #[cfg(feature = "call-all")]
90    fn call_all<S>(self, reqs: S) -> CallAll<Self, S>
91    where
92        Self: Sized,
93        Self::Error: Into<Error>,
94        S: futures_core::Stream<Item = Request>,
95    {
96        CallAll::new(self, reqs)
97    }
98}
99
100impl<T: ?Sized, Request> ServiceExt<Request> for T where T: tower_service::Service<Request> {}