rust - 在 Stream 中使用异步 fn 时的生命周期冲突
问题描述
我正在尝试Conn::event
在异步循环中使用以下异步 fn,但出现以下错误:
错误[E0495]:由于需求冲突,无法推断 autoref 的适当生命周期
从我对这个问题的有限理解来看,我认为这是在抱怨,因为对 self 的引用与脱糖Future
类型有关,它需要'static
一生,而try_for_each
需要更少的东西?我在这里不合群!
我怎样才能将此代码重构为...嗯...工作?
use futures::{Sink, SinkExt, TryFutureExt, TryStreamExt};
use std::marker::Unpin;
use tokio::io::{self, AsyncBufReadExt};
pub struct Conn<S> {
sink: S,
}
impl<S> Conn<S>
where
S: Sink<String>,
S: Unpin,
{
pub async fn event(&mut self, data: String) -> Result<(), ()> {
self.sink.send(data).await.map_err(|_| ())
}
pub async fn run(self) -> Result<(), ()> {
io::BufReader::new(io::stdin())
.lines()
.map_err(|_| ())
.try_for_each(|event| self.event(event))
.await
}
}
fn main() {}
解决方案
推荐阅读
- android - Google 64-bit requirement only works for some apps
- javascript - 需要在 Azuredevops 中使用 build.yaml 构建 javascript 应用程序
- python - 导入 CSV 和随机输出行 6 次
- javascript - 旧输入数组和动态字段 - Laravel - Blade
- wso2 - 在 Windows 10 上构建 WSO2 Microgateway 项目时出错
- google-apps-script - 如何从 Google 表单提交中触发表格脚本?
- connection - Pentaho“使转换数据库事务化”加上提交频率
- algorithm - 计算三种不同排列中相同有序对的数量
- python - 张量流(CPU)中的并行推理
- python - 如何测试单例 __del__() 方法?