#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

#2176 in Asynchronous

Download history 13/week @ 2024-07-20 19/week @ 2024-07-27 10/week @ 2024-08-03 17/week @ 2024-08-10 3/week @ 2024-08-17 17/week @ 2024-08-24 5/week @ 2024-08-31 8/week @ 2024-09-14 24/week @ 2024-09-21 12/week @ 2024-09-28 18/week @ 2024-10-05 43/week @ 2024-10-12 38/week @ 2024-10-19 8/week @ 2024-10-26 7/week @ 2024-11-02

102 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–7.5MB
~47K SLoC