【发布时间】:2021-04-24 15:08:15
【问题描述】:
我正在尝试将AsyncRead 包装在另一个AsyncRead 中(并对其进行一些数据处理)。但是,当尝试存储 .read() 的 Future 时,我遇到了终身问题:self has an anonymous lifetime '_ but it needs to satisfy a 'static lifetime requirement。
代码:
pub struct AsyncReadWrap {
input: Pin<Box<dyn AsyncRead + 'static>>,
future: Option<Pin<Box<dyn Future<Output = std::io::Result<usize>>>>>
}
impl AsyncReadWrap {
pub fn new(input: impl AsyncRead + Unpin + 'static) -> AsyncReadWrap {
AsyncReadWrap {
input: Box::pin(input),
future: None
}
}
}
impl AsyncRead for AsyncReadWrap {
fn poll_read(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<std::io::Result<()>> {
let mut buffer = [0u8; 2048];
let future = self.input.as_mut().read(&mut buffer);
self.future = Some(Box::pin(future));
Poll::Pending
}
}
我是异步的新手,奇怪的是在 poll 函数中没有一些简单的方法来 await。谢谢。
【问题讨论】:
标签: asynchronous rust