Crate xitca_service

source ·
Expand description

traits for composable async functions.

Examples

use core::convert::Infallible;

use xitca_service::{fn_service, Service, ServiceExt};

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

Re-exports

Modules

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

Structs

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

Traits

  • Same as std::ops::Fn trait but for async output.
  • 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.
  • extend trait for Service providing combinator functionalities.

Functions