xitca_service/lib.rs
1//! traits for composable async functions.
2//!
3//! # Examples
4//! ```rust
5//! use core::convert::Infallible;
6//!
7//! use xitca_service::{fn_service, Service, ServiceExt};
8//! # async fn call() -> Result<(), Infallible> {
9//! // apply middleware to async function as service.
10//! let builder = fn_service(|req: String| async { Ok::<_, Infallible>(req) })
11//! // a middleware function that has ownership of the argument and output of S as Service
12//! // trait implementor.
13//! .enclosed_fn(async |service, req| {
14//! service.call(req).await.map(|mut res| {
15//! res.push_str("-dagongren");
16//! res
17//! })
18//! });
19//!
20//! // build the composited service.
21//! let service = builder.call(()).await?;
22//!
23//! // execute the service function with string argument.
24//! let res = service.call("996".to_string()).await?;
25//!
26//! assert_eq!(res, "996-dagongren");
27//!
28//! # Ok(()) }
29//! ```
30#![no_std]
31#![forbid(unsafe_code)]
32
33mod service;
34
35pub mod middleware;
36pub mod pipeline;
37pub mod ready;
38
39pub use self::{
40 middleware::{EnclosedBuilder, EnclosedFnBuilder, MapBuilder, MapErrorBuilder},
41 service::{FnService, Service, ServiceExt, fn_build, fn_service},
42};
43
44#[cfg(feature = "alloc")]
45extern crate alloc;
46
47#[cfg(feature = "alloc")]
48pub mod object;
49
50#[cfg(feature = "alloc")]
51/// boxed [core::future::Future] trait object with no extra auto trait bound(`!Send` and `!Sync`).
52pub type BoxFuture<'a, Res, Err> =
53 core::pin::Pin<alloc::boxed::Box<dyn core::future::Future<Output = Result<Res, Err>> + 'a>>;