Crate xitca_service

Crate xitca_service 

Source
Expand description

traits for composable async functions.

§Examples

use core::convert::Infallible;

use xitca_service::{fn_service, Service, ServiceExt};
// apply middleware to async function as service.
let builder = fn_service(|req: String| async { Ok::<_, Infallible>(req) })
    // a middleware function that has ownership of the argument and output of S as Service
    // trait implementor.
    .enclosed_fn(async |service, req| {
        service.call(req).await.map(|mut res| {
            res.push_str("-dagongren");
            res
        })
    });

// 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");

Re-exports§

pub use self::middleware::EnclosedBuilder;
pub use self::middleware::EnclosedFnBuilder;
pub use self::middleware::MapBuilder;
pub use self::middleware::MapErrorBuilder;

Modules§

middleware
utility middlewares that can be used together with ServiceExt::enclosed.
pipeline
specialized struct and enum for handling branching of parent-child/first-second related two types.
ready
trait and types for backpressure handling.

Structs§

FnService
new type for implementing Service trait to a Fn(Req) -> impl Future<Output = Result<Res, Err>> type.

Traits§

Service
Trait for simulate Fn<(&Self, Arg)> -> impl Future<Output = Result<T, E>> + '_. The function call come from stateful type that can be referenced within returned opaque future.
ServiceExt
extend trait for Service providing combinator functionalities.

Functions§

fn_build
Shortcut for transform a given Fn into type impl Service trait.
fn_service
Shortcut for transform a given Fn into type impl Service trait.