Skip to content

Latest commit

 

History

History
42 lines (33 loc) · 1.51 KB

expected_async_block_found_a_different_async_block.md

File metadata and controls

42 lines (33 loc) · 1.51 KB

两个或多个 stream, async block/fn 明明是一样的返回值但是会被编译报错类型不一致

expected async block, found a different async block

Ok(1).map(|n| async { add(n).await }).unwrap_or_else(|_| async { 0 }).await

由于每个 async block 编译器认为生成的 generator 类型不同,业界/社区的主流用法是通过 boxed 去规避

async fn f() -> i32{
    Ok(1)
        .map(|_| async { 1 })
        .unwrap_or_else(|_| async { 0 }).await
}

/// similar to StreamExt::left_stream
async fn f1() -> i32 {
    Ok::<_, Infallible>(1)
        .map(|_| async { 1 }.left_future())
        .unwrap_or_else(|_| async { 0 }.right_future()).await
}

async fn f2() -> i32 {
    use tokio_util::either::Either;
    Ok::<_, Infallible>(1)
        .map(|_| Either::Right(async { 1 }))
        .unwrap_or_else(|_| Either::Left(async { 0 })).await
}

async fn f3() -> i32 {
    Ok::<_, Infallible>(1)
        .map(|_| async { 1 }.boxed())
        .unwrap_or_else(|_| async { 0 }.boxed()).await
}