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
//! traits for composable async functions.
//!
//! # Examples
//! ```rust
//! use core::convert::Infallible;
//!
//! use xitca_service::{fn_service, Service, ServiceExt};
//! # async fn call() -> Result<(), Infallible> {
//!
//! // a middleware function that has ownership of the argument and output of S as Service
//! // trait implementor.
//! async fn middleware<S>(s: &S, req: String) -> Result<String, Infallible>
//! where
//! S: Service<String, Response = String, Error = Infallible>
//! {
//! let req2 = req.clone();
//! let mut res = s.call(req).await?;
//! assert_eq!(res, req2);
//! res.push_str("-dagongren");
//! Ok(res)
//! }
//!
//! // apply middleware to async function as service.
//! let builder = fn_service(|req: String| async { Ok::<_, Infallible>(req) })
//! .enclosed_fn(middleware);
//!
//! // build the composited service.
//! let service = builder.call(()).await?;
//!
//! // execute the service function with string argument.
//! let res = service.call("996".to_string()).await?;
//!
//! assert_eq!(res, "996-dagongren");
//!
//! # Ok(()) }
//! ```
#![no_std]
#![forbid(unsafe_code)]
mod async_closure;
mod service;
pub mod middleware;
pub mod pipeline;
pub mod ready;
pub use self::{
async_closure::AsyncClosure,
pipeline::{EnclosedBuilder, EnclosedFnBuilder, MapBuilder, MapErrorBuilder},
service::{fn_build, fn_service, FnService, Service, ServiceExt},
};
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
pub mod object;
#[cfg(feature = "alloc")]
/// boxed [core::future::Future] trait object with no extra auto trait bound(`!Send` and `!Sync`).
pub type BoxFuture<'a, Res, Err> =
core::pin::Pin<alloc::boxed::Box<dyn core::future::Future<Output = Result<Res, Err>> + 'a>>;
#[cfg(feature = "std")]
extern crate std;