#sink #tokio #future #async-write #io #byte #convert

tokio-into-sink

convert a tokio::io::AsyncWrite into a futures::Sink for bytes

1 unstable release

0.1.0 Sep 15, 2023

#1332 in Asynchronous

Download history 18/week @ 2024-03-29 3/week @ 2024-04-05 27/week @ 2024-04-12 50/week @ 2024-05-24 33/week @ 2024-05-31 42/week @ 2024-06-07 8/week @ 2024-06-14 9/week @ 2024-06-21 1/week @ 2024-06-28

68 downloads per month

MIT/Apache

7KB
118 lines

Use an AsyncWrite as a Sink<Item: AsRef<[u8]>.

This adapter produces a sink that will write each value passed to it into the underlying writer. Note that this function consumes the given writer, returning a wrapped version.

use tokio_into_sink::IntoSinkExt as _;
use futures::{stream, StreamExt as _};
use std::io;

let stream = stream::iter(["hello", "world"]).map(io::Result::Ok);
let write = tokio::fs::File::create("/dev/null").await.unwrap();
let sink = write.into_sink();
stream.forward(sink).await.unwrap();

Ported from futures::io::AsyncWriteExt::into_sink.


lib.rs:

Use an AsyncWrite as a Sink<Item: AsRef<[u8]>.

This adapter produces a sink that will write each value passed to it into the underlying writer. Note that this function consumes the given writer, returning a wrapped version.

use tokio_into_sink::IntoSinkExt as _;
use futures::{stream, StreamExt as _};
use std::io;

let stream = stream::iter(["hello", "world"]).map(io::Result::Ok);
let write = tokio::fs::File::create("/dev/null").await.unwrap();
let sink = write.into_sink();
stream.forward(sink).await.unwrap();

Ported from futures::io::AsyncWriteExt::into_sink.

Dependencies

~2–3MB
~47K SLoC