首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Warp:单路工作,多个.or()不起作用

Warp:单路工作,多个.or()不起作用
EN

Stack Overflow用户
提问于 2022-01-10 18:47:07
回答 1查看 295关注 0票数 2

希望有人能帮助我理解为什么使用这样一条路径运行warp会很好地编译:

代码语言:javascript
复制
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // GET /stats
    let stats = warp::get()
        .and(warp::path("stats"))
        .map(|| {
            let mut sys = System::new_all();
            sys.refresh_all();

            let local = LocalSystem::from_sys(&sys);
            warp::reply::json(&local);
        });

    // GET /
    let index = warp::get()
        .and(warp::path::end())
        .map(|| warp::reply::json(&last_ten_logs()));

    warp::serve(index).run(([127, 0, 0, 1], 4000)).await;

    Ok(())
}

但是,更改warp::serve()行以提供两条路由就像回购中的例子一样会导致编译错误:

代码语言:javascript
复制
error[E0277]: the trait bound `(): Reply` is not satisfied
   --> src/main.rs:140:17
    |
140 |     warp::serve(stats.or(index)).run(([127, 0, 0, 1], 4000)).await;
    |     ----------- ^^^^^^^^^^^^^^^ the trait `Reply` is not implemented for `()`
    |     |
    |     required by a bound introduced by this call
    |
    = note: required because of the requirements on the impl of `Reply` for `((),)`
    = note: 2 redundant requirements hidden
    = note: required because of the requirements on the impl of `Reply` for `(warp::generic::Either<((),), (Json,)>,)`

我不明白编译器要求我改变什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-10 18:50:12

错误是明确的:

属性Reply不是为()实现的

问题是,您的stats端点不返回任何内容,只需删除最后一个;,以便将其作为闭包中的最后一个表达式返回:

代码语言:javascript
复制
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // GET /stats
    let stats = warp::get()
        .and(warp::path("stats"))
        .map(|| {
            let mut sys = System::new_all();
            sys.refresh_all();

            let local = LocalSystem::from_sys(&sys);
            warp::reply::json(&local)
        });
    ...
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70657467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档