#async-io #pattern #future #async #io #io-read #futures

handy_async

A handy library for describing asynchronous code declaratively

14 releases

Uses old Rust 2015

0.2.13 Nov 8, 2017
0.2.12 Aug 27, 2017
0.2.11 Mar 17, 2017
0.2.9 Feb 22, 2017
0.2.5 Dec 18, 2016

#799 in Asynchronous

Download history 27/week @ 2023-11-20 26/week @ 2023-11-27 21/week @ 2023-12-04 28/week @ 2023-12-11 20/week @ 2023-12-18 12/week @ 2023-12-25 4/week @ 2024-01-01 16/week @ 2024-01-08 23/week @ 2024-01-15 7/week @ 2024-01-22 5/week @ 2024-01-29 12/week @ 2024-02-05 21/week @ 2024-02-12 26/week @ 2024-02-19 32/week @ 2024-02-26 38/week @ 2024-03-04

118 downloads per month
Used in 4 crates (3 directly)

MIT license

180KB
4K SLoC

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.

extern crate handy_async;
extern crate futures;

use std::io::{Read, Error};
use futures::{Future, BoxFuture};
use handy_async::io::ReadFrom;
use handy_async::pattern::{Pattern, Endian};
use handy_async::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).map_err(|e| e.into_error()).boxed()
}

Installation

Add following lines to your Cargo.toml:

[dependencies]
handy_async = "0.2"

Dependencies

~175KB