tokio-rs / async-stream

Asynchronous streams for Rust using async & await notation
Other
604 stars 32 forks source link

Cannot use "yield" and "?" on the same line #65

Closed TheRealMintd closed 2 years ago

TheRealMintd commented 2 years ago

This is my first time using this library, and I'm currently facing this issue. The following code fails to compile:

use async_stream::try_stream;
use futures_core::Stream;

fn test() -> impl Stream<Item = Result<u32, ()>> {
    try_stream! {
        yield Err(())?;
    }
}

With the compiler showing this error:

    |
5   | /     try_stream! {
6   | |         yield Err(())?;
    | |                      ^ cannot use the `?` operator in an async block that returns `()`
7   | |     }
    | |_____- this function should return `Result` or `Option` to accept `?`
    |
    = help: the trait `FromResidual<Result<Infallible, ()>>` is not implemented for `()`

While this code works:

use async_stream::try_stream;
use futures_core::Stream;

fn test() -> impl Stream<Item = Result<u32, ()>> {
    try_stream! {
        let temp = Err(())?;
        yield temp;
    }
}