rust_asio 0.3.2

Asynchronous I/O library
Documentation
extern crate asio;
extern crate time;
use std::io;
use time::Duration;
use asio::*;
use asio::ip::*;
use asio::socket_base::*;

static mut goal_flag: bool = false;

struct TcpAcceptor {
    soc: TcpListener,
    timer: SteadyTimer,
}

impl TcpAcceptor {
    fn start(io: &IoService) {
        let acc = Strand::new(io, TcpAcceptor {
            soc: TcpListener::new(io, Tcp::v6()).unwrap(),
            timer: SteadyTimer::new(io),
        });
        acc.soc.set_option(ReuseAddr::new(true)).unwrap();
        acc.soc.bind(&TcpEndpoint::new(IpAddrV6::any(), 12345)).unwrap();
        acc.soc.listen().unwrap();
        acc.timer.async_wait_for(Duration::milliseconds(1), acc.wrap(Self::on_wait));
        acc.soc.async_accept(acc.wrap(Self::on_accept));
    }

    fn on_wait(acc: Strand<Self>, res: io::Result<()>) {
        if let Ok(_) = res {
            acc.soc.cancel();
        } else {
            panic!();
        }
    }

    fn on_accept(_: Strand<Self>, res: io::Result<(TcpSocket, TcpEndpoint)>) {
        if let Err(err) = res {
            assert_eq!(err.kind(), io::ErrorKind::Other);  // cancel
            unsafe { goal_flag = true; }
        } else {
            panic!();
        }
    }
}

#[test]
fn main() {
    let io = IoService::new();
    TcpAcceptor::start(&io);
    io.run();
    assert!(unsafe { goal_flag });
}