4 releases

Uses old Rust 2015

0.1.3 Jun 23, 2016
0.1.2 May 30, 2016
0.1.1 May 11, 2016
0.1.0 Apr 24, 2016

#1361 in Asynchronous

27 downloads per month
Used in capnp-gj

MIT license

60KB
1K SLoC

good job, yo

Asynchronous input and ouput, built on top of the gj event loop.

Build Status Build status

documentation

Uses epoll on Linux, kqueue on OSX, and I/O completion ports on Windows.


lib.rs:

Asynchronous input and output.

Example

extern crate gj;
extern crate gjio;
use gj::{EventLoop, Promise};
use gjio::{AsyncRead, AsyncWrite, BufferPrefix, SocketStream};

fn echo(mut stream: SocketStream, buf: Vec<u8>) -> Promise<(), ::std::io::Error> {
    stream.try_read(buf, 1).then(move |(buf, n)| {
        if n == 0 { // EOF
            Promise::ok(())
        } else {
            stream.write(BufferPrefix::new(buf, n)).then(move |prefix| {
                echo(stream, prefix.buf)
            })
        }
    })
}

fn main() {
    EventLoop::top_level(|wait_scope| -> Result<(), ::std::io::Error> {
        let mut event_port = try!(gjio::EventPort::new());
        let network = event_port.get_network();
        let mut listen_address = network.get_tcp_address(
            ::std::str::FromStr::from_str("127.0.0.1:0").unwrap());
        let listener = try!(listen_address.listen());
        let connect_address = network.get_tcp_address(try!(listener.local_addr()));

        let promise1 = listener.accept().then(move |stream| {
            echo(stream, vec![0;5]) // Tiny buffer just to be difficult
        });

        let promise2 = connect_address.connect().then(move |mut stream| {
            stream.write(b"hello world").then(move |_| {
                stream.read(vec![0; 11], 11).map(|(buf, _)| {
                    assert_eq!(buf, b"hello world");
                    Ok(())
                })
           })
        });

        let all = Promise::all(vec![promise1, promise2].into_iter());
        try!(all.wait(wait_scope, &mut event_port));
        Ok(())
    }).expect("top level");
}

Dependencies

~2.5MB
~51K SLoC