handy_async

Crates.io: handy_async Documentation Build Status License: MIT

This library provides miscellaneous functionalities to help asynchronous operations in Rust.

Documentation

handy_async uses futures to achieve asynchronous operations (mainly I/O related operations) and defines a lot of pattern objects to facilitate writing declarative code.

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

```rust extern crate handy_async; extern crate futures;

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

struct TcpHeader { sourceport: u16, destinationport: u16, sequencenumber: u32, acknowledgmentnumber: u32, dataoffset: u8, // 4 bits reserved: u8, // 6 bits flags: u8, // 6 bits window: u16, checksum: u16, urgentpointer: u16, option: Vec, }

fn readtcpheader(reader: R) -> BoxFuture { let pattern = (U16.be(), U16.be(), U32.be(), U32.be(), U16.be(), U16.be(), U16.be(), U16.be()) .andthen(|(srcport, dstport, seqnum, acknum, flags, window, checksum, urgent)| { let dataoffset = (flags & 0b1111) as u8; let header = TcpHeader { sourceport: srcport, destinationport: dstport, sequencenumber: seqnum, acknowledgmentnumber: acknum, dataoffset: dataoffset, 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).map_err(|e| e.into_error()).boxed()

} ```

Installation

Add following lines to your Cargo.toml:

toml [dependencies] handy_async = "0.2"