特征“futures::Stream”没有为我的类型实现

the trait `futures::Stream` is not implemented for my type

提问人:Vana 提问时间:1/16/2023 最后编辑:Vana 更新时间:1/16/2023 访问量:229

问:

我正在创建一个更新系统。它在 2 个二进制文件之间创建补丁,压缩补丁和二进制文件并启动 Web 服务器。

我需要从闭包调用异步 fn。 所以闭包需要异步才能使用 fnawait

我愿意

let repair_stream = future::lazy(move |_| async {
        let mut failures = {
            let state = &mut *shared_state_r.borrow_mut();
            state.previous_failures = mem::take(&mut state.failures);
            state.previous_failures.clone()
        };
        if !failures.is_empty() {
            failures.sort();
            global_progression_r.lock().await.stage = UpdateStage::FindingRepairPath; // note the use of await
            Either::Left(
                update_internal(
                    update_options_r,
                    file_manager_r,
                    global_progression_r,
                    State::New, //< force to repair from no starting revision
                    shared_state_r,
                    repository,
                    goal_version_r,
                    UpdateFilter { failures },
                    UpdateStage::Repairing,
                )
                .try_flatten_stream(),
            )
        } else {
            // nothing to repair, everything went well
            Either::Right(stream::empty())
        }
    })
    .flatten_stream();

但是得到

error[E0277]: the trait bound `impl futures::Future<Output = futures::future::Either<TryFlattenStream<impl futures::Future<Output = std::result::Result<impl futures::Stream<Item = std::result::Result<SharedUpdateProgress, UpdateError>> + '_, UpdateError>>>, futures::stream::Empty<_>>>: futures::Stream` is not satisfied
   --> lib/src/workspace/updater.rs:342:25
    |
342 |       let repair_stream = future::lazy(move |_| async {
    |  _________________________^
343 | |         let mut failures = {
344 | |             let state = &mut *shared_state_r.borrow_mut();
345 | |             state.previous_failures = mem::take(&mut state.failures);
...   |
368 | |         }
369 | |     })
    | |______^ the trait `futures::Stream` is not implemented for `impl futures::Future<Output = futures::future::Either<TryFlattenStream<impl futures::Future<Output = std::result::Result<impl futures::Stream<Item = std::result::Result<SharedUpdateProgress, UpdateError>> + '_, UpdateError>>>, futures::stream::Empty<_>>>`
370 |       .flatten_stream();
    |        -------------- required by a bound introduced by this call
    |
    = help: the following other types implement trait `futures::Stream`:
              &mut S
              Abortable<St>
              ApplyStream
              AssertUnwindSafe<S>
              Box<S>
              Buffer<S, Item>
              BufferUnordered<St>
              Buffered<St>
            and 84 others
note: required by a bound in `flatten_stream`
   --> /home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.25/src/future/future/mod.rs:343:23
    |
343 |         Self::Output: Stream,
    |                       ^^^^^^ required by this bound in `flatten_stream`

锁定 fn 返回tokio::sync::MutexGuard

完整来源: https://github.com/Ludea/speedupdate-rs/blob/master/lib/src/workspace/updater.rs#L342

我不明白这个问题,也不知道如何解决它

asynchronous rust async-await 闭包

评论

0赞 cafce25 1/16/2023
问题是你调用的东西不是流。你能解释一下这段代码试图实现什么吗?您可以在“如何提问”中找到有关如何改进帖子的更多提示。flatten_stream
0赞 cafce25 1/16/2023
一般来说,你应该编辑你的帖子,而不是在评论中回答,而且完整源代码的链接也不是“解释这个代码试图做什么”,在你把你的代码扔给我们之前放一个简短的段落,这样我们就不必猜测了。代码,无论是片段还是完整代码,都没有这样做,因为它不是有效的代码。
0赞 Vana 1/16/2023
第一篇文章已更新!
1赞 cafce25 1/16/2023
Future不是,也不是你为什么要尝试在最后一行调用,在我看来,你在这两种情况下得到的任何流首先都是“平坦”的‽StreamEitherflatten_stream
0赞 Vana 1/16/2023
在闭包不是异步的之前,闭包返回一个 Lazy。我刚刚同步到关闭

答: 暂无答案