2个不稳定版本

0.2.0 2024年7月24日
0.1.0 2023年12月29日

#922Rust模式

Download history 29/week @ 2024-04-14 12/week @ 2024-04-21 41/week @ 2024-04-28 16/week @ 2024-05-05 3/week @ 2024-05-12 246/week @ 2024-05-19 111/week @ 2024-05-26 32/week @ 2024-06-02 18/week @ 2024-06-09 32/week @ 2024-06-16 39/week @ 2024-06-23 36/week @ 2024-06-30 80/week @ 2024-07-07 85/week @ 2024-07-14 244/week @ 2024-07-21 105/week @ 2024-07-28

515 每月下载量
用于 4 crates

Apache-2.0

32KB
858 代码行

xitca的异步特性行为


lib.rs:

组合异步函数的特性行为。

示例

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

无运行时依赖