I'm trying to consume the below async fn Conn::event
in an async loop, but getting the following error:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
From my limited understanding of the issue, I assume it's complaining because the reference to self is tied up in the desugared Future
type, which needs a 'static
lifetime, while try_for_each
needs something less? I'm out of my league here!
How can I refactor this code to...um...work?
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() {}