arbe
arbe

Reputation: 87

Rust futures -- adapting a function as a Sink

I have something similar to the tokio connect example with a method that accepts a sink:

pub async fn connect(
        addr: &SocketAddr,
        mut stdin: impl Stream<Item = Result<Request, io::Error>> + Unpin,
        mut stdout: impl Sink<Response, Error = io::Error> + Unpin,
    ) -> Result<(), Box<dyn Error>> {

Is there a standard/easy way to adapt a function to a sink for printing and/or transformation?

eg. something like:

connect(.., .., sink::from_function(|r| match r {
    Ok(response) => println!("received a response: {:?}", response),
    Err(e) => println!("error! {:?}", e);
})
.await;

Upvotes: 0

Views: 752

Answers (1)

Freyja
Freyja

Reputation: 40794

You can use the drain() function (which creates a sink that just discards all items) chained with the .with() method (which maps the inputs of a sink) to create a sink from a function:

use futures::prelude::*;
use futures::sink::drain;

let sink = drain().with(|value| async move { // <-- note async block
    // do something with the input...

    // then return a result
    Ok(())
});

You can also use .with() to inspect or transform an existing stream, you just need to ensure that the success type you return from the closure is the same as the input of the stream you're transforming.

Playground example

Upvotes: 2

Related Questions