2 unstable releases

0.2.0 Jul 24, 2024
0.1.0 Dec 29, 2023

#648 in HTTP server

Download history 241/week @ 2024-07-22 102/week @ 2024-07-29 197/week @ 2024-08-05 126/week @ 2024-08-12 346/week @ 2024-08-19 425/week @ 2024-08-26 485/week @ 2024-09-02 372/week @ 2024-09-09 286/week @ 2024-09-16 396/week @ 2024-09-23 264/week @ 2024-09-30 27/week @ 2024-10-07 62/week @ 2024-10-14 42/week @ 2024-10-21 51/week @ 2024-10-28 54/week @ 2024-11-04

211 downloads per month
Used in 4 crates

Apache-2.0

32KB
858 lines

async traits for xitca


lib.rs:

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

No runtime deps