handy_io

Crates.iohandy_io
lib.rshandy_io
version0.1.2
sourcesrc
created_at2016-11-27 06:17:29.679748
updated_at2016-11-29 19:09:33.944799
descriptionA handy pattern and futures based asynchronous I/O library
homepagehttps://github.com/sile/handy_io
repositoryhttps://github.com/sile/handy_io
max_upload_size
id7376
size107,473
Takeru Ohta (sile)

documentation

https://docs.rs/handy_io

README

handy_io

Crates.io: handy_io Build Status License: MIT

This library provides miscellaneous functionalities to help I / O operations in Rust.

handy_io uses futures to achieve asynchronous I/O and defines a lot of pattern objects to facilitate writing I/O related codes declaratively.

For example, you can write a function to read a TCP header defined in RFC-793 asynchronously as following.

extern crate handy_io;
extern crate futures;

use std::io::{Read, Error};
use futures::{Future, BoxFuture};
use handy_io::io::ReadFrom;
use handy_io::pattern::{Pattern, Endian};
use handy_io::pattern::read::{U16, U32};

struct TcpHeader {
    source_port: u16,
    destination_port: u16,
    sequence_number: u32,
    acknowledgment_number: u32,
    data_offset: u8, // 4 bits
    reserved: u8, // 6 bits
    flags: u8, // 6 bits
    window: u16,
    checksum: u16,
    urgent_pointer: u16,
    option: Vec<u8>,
}

fn read_tcp_header<R: Read + Send + 'static>(reader: R) -> BoxFuture<TcpHeader, Error> {
    let pattern = (U16.be(), U16.be(), U32.be(), U32.be(), U16.be(), U16.be(), U16.be(), U16.be())
        .and_then(|(src_port, dst_port, seq_num, ack_num, flags, window, checksum, urgent)| {
            let data_offset = (flags & 0b1111) as u8;
            let header = TcpHeader {
                source_port: src_port,
                destination_port: dst_port,
                sequence_number: seq_num,
                acknowledgment_number: ack_num,
                data_offset: data_offset,
                reserved: ((flags >> 4) & 0b111111) as u8,
                flags: (flags >> 10) as u8,
                window: window,
                checksum: checksum,
                urgent_pointer: urgent,
                option: Vec::new(),
            };

            let option_size = (data_offset as usize - 5) * 4;
            (Ok(header), vec![0; option_size]) // Reads additional option bytes
        })
        .map(|(mut header, option)| {
            header.option = option;
            header
        });
    pattern.read_from(reader).map(|(reader, header)| header).boxed()
}

Documentation

See here.

The documentation includes some examples.

Installation

Add following lines to your Cargo.toml:

[dependencies]
handy_io = "0.1"
Commit count: 98

cargo fmt