async-stream+anyhowisnotSend
创始人
2024-09-21 00:01:25
0

这个问题可能会出现在使用异步流和anyhow库时。在Rust中,Send trait用于确保类型可以安全地在不同线程之间转移所有权。由于async-stream和anyhow库没有实现Send trait,因此会出现“async-stream + anyhow is not Send”错误。

解决方法是在实现异步流时使用tokio::spawn或async_std::task::spawn函数来运行异步任务,以确保任何错误都是在异步任务的上下文中捕获并处理。示例代码如下:

使用tokio实现异步流:

use tokio::{stream::Stream, task};

async fn my_stream() -> impl Stream> {
    // ... implementation of the stream ...
}

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
    let stream = my_stream();
    let handle = tokio::task::spawn(async move {
        stream.for_each(|item| async move {
            match item {
                Ok(item) => { /* ... do something ... */ },
                Err(err) => { /* ... handle the error ... */ },
            }
        })
        .await
    });

    handle.await??;
    Ok(())
}

使用async-std实现异步流:

use async_std::{prelude::*, task::spawn};

async fn my_stream() -> impl Stream> {
    // ... implementation of the stream ...
}

fn main() -> Result<(), anyhow::Error> {
    let stream = my_stream();
    let task = spawn(async move {
        stream.for_each(|item| async move {
            match item {
                Ok(item) => { /* ... do something ... */ },
                Err(err) => { /* ... handle the error ... */ },
            }
        })
        .await
    });

    // ... handle the task result ...
    Ok(())
}

相关内容

热门资讯

程序员教你!hhpoker德州... 程序员教你!hhpoker德州机器人,hhpoker德州辅助,技巧教程(一贯真的是有挂)-哔哩哔哩;...
玩家必看教程!wpk是真的还是... 玩家必看教程!wpk是真的还是假的,wepoker透视脚本,透视教程(一贯确实有挂)-哔哩哔哩;亲,...
发现一款!wepokerplu... 发现一款!wepokerplus开挂,wpk官网下载链接,辅助教程(一直真的是有挂)-哔哩哔哩;1、...
揭秘几款!hhpoker可以控... 揭秘几款!hhpoker可以控制牌吗,wepoker透视脚本是什么,攻略教程!(其实是有挂的)-哔哩...
一分钟快速了解!wejoker... 一分钟快速了解!wejoker透视方法,wepoker线上大神,必备教程(果然确实有挂)-哔哩哔哩;...
一分钟了解!hhpoker免费... 一分钟了解!hhpoker免费透视脚本,pokemmo辅助器,透牌教程(一贯确实有挂)-哔哩哔哩;1...
透视规律!wepoker怎么买... 透视规律!wepoker怎么买辅助,wepoker分析,透牌教程(原来有挂)-哔哩哔哩;1.wepo...
我来向大家传授!wejoker... 我来向大家传授!wejoker开挂,竞技联盟辅助插件,分享教程(原来真的是有挂)-哔哩哔哩;竞技联盟...
重大发现!wepoker免费脚... 重大发现!wepoker免费脚本,wepoker免费辅助器,解说技巧(一直是有挂)-哔哩哔哩;1.w...
热点推荐!wepoker透视脚... 热点推荐!wepoker透视脚本安卓,德扑HHpoker有挂吗,存在挂教程(原来真的是有挂)-哔哩哔...