1 unstable release

0.1.0 Dec 29, 2023

#6 in #infallible

Download history 54/week @ 2023-12-26 75/week @ 2024-01-02 23/week @ 2024-01-09 113/week @ 2024-01-16 32/week @ 2024-01-23 37/week @ 2024-01-30 77/week @ 2024-02-06 66/week @ 2024-02-13 34/week @ 2024-02-20 102/week @ 2024-02-27 67/week @ 2024-03-05 38/week @ 2024-03-12 17/week @ 2024-03-19 183/week @ 2024-03-26 174/week @ 2024-04-02

425 downloads per month
Used in 4 crates (3 directly)

Apache-2.0

33KB
847 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