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

lz_stream_io

分别针对流和通道实现AsyncRead和AsyncWrite

1个不稳定版本

使用旧的Rust 2015

0.1.0 2018年2月8日

#54 in #sink

MIT许可证

9KB
153

Lz Stream IO

此包为futures::Stream和futures::Sink类型提供IO抽象。

Build Status

文档

特性

  • 在futures::Sink上实现std::io::Write和tokio_io::AsyncWrite(见lz_stream_io::SinkWrite)。
  • 在futures::Stream上实现std::io::Read和tokio_io::Async(见lz_stream_io::StreamRead)。

许可证

本项目采用MIT许可证(LICENSEhttp://opensource.org/licenses/MIT)。


lib.rs:

此包为byte buffers上的Stream和Sink实例提供AsyncRead和AsyncWrite实现。

StreamRead和SinkWrite类型可以这样使用

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();
}

依赖

~605KB
~11K SLoC