I am trying to wrap AsyncRead
in another AsyncRead
(and do some data processing on it). However when trying to store the Future
of .read()
I get lifetime problem: self has an anonymous lifetime '_ but it needs to satisfy a 'static lifetime requirement
.
Code:
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
}
}
I am new to async and it's weird that there isn't some simple way to await
in poll
functions. Thank you.