#io-read #io-write #stream #io #sink #async-io #write

lz_stream_io

AsyncRead and AsyncWrite implementations for Streams and Sinks respectively

1 unstable release

Uses old Rust 2015

0.1.0 Feb 8, 2018

#50 in #sink

MIT license

9KB
153 lines

Lz Stream IO

This crate provides IO abstractions over the futures::Stream and futures::Sink types.

Build Status

Documentation

Features

  • A std::io::Write and tokio_io::AsyncWrite implementation over a futures::Sink (see lz_stream_io::SinkWrite).
  • A std::io::Read and tokio_io::Async implementation over a futures::Stream (see lz_stream_io::StreamRead).

License

This project is licensed under the MIT License (LICENSE or http://opensource.org/licenses/MIT).


lib.rs:

This crate provides AsyncRead and AsyncWrite implementations for Stream and Sink instances over byte buffers.

The StreamRead and SinkWrite types may be used like so:

extern crate bytes;
extern crate futures;
extern crate tokio_io;
extern crate lz_stream_io;

use futures::{Future, Stream, Sink};
use futures::unsync::mpsc;
use std::io::{Result as IoResult, ErrorKind as IoErrorKind};
use bytes::Bytes;
use tokio_io::io as async_io;
use lz_stream_io::{SinkWrite, StreamRead};

fn main() {
    // The sink item type must implement From<&[u8]>
    // The stream item type must implement AsRef<[u8]>
    let (sink, stream) = mpsc::unbounded::<Bytes>();

    // Both sink and stream must have an error type which std::io::Error
    // can be created from
    let sink = sink.sink_map_err(|_| IoErrorKind::InvalidData);
    let stream = stream.map_err(|_| IoErrorKind::InvalidData);

    let write = SinkWrite::new(sink);
    let read = StreamRead::new(stream);

    async_io::write_all(write, b"hello")
        .and_then(|(write, _)| async_io::write_all(write, b" world"))
        .and_then(|_| async_io::read_to_end(read, vec![]))
        .and_then(|(_, bytes)| {
            assert_eq!(bytes, b"hello world");
            Ok(())
        }).wait().unwrap();
}

Dependencies

~605KB
~11K SLoC