0

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?

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6d5222afe9cc7d9bbe41f7ac0a29d72a

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() {}
Pete
  • 437
  • 4
  • 11
  • 1
    Here is the [solution](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=9ffde423d08eae8bc2057abb7ba881bc), you can use `try_fold` instead `try_for_each`. You can find the reason from [here](https://stackoverflow.com/questions/28521637/how-can-i-move-a-captured-variable-into-a-closure-within-a-closure) – Ömer Erden Apr 07 '20 at 12:40
  • 1
    Legend! Cheers mate. – Pete Apr 07 '20 at 20:38

0 Answers0