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;
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: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