rust_asio 0.1.0

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

struct FooTimer {
    timer: SteadyTimer,
}

impl FooTimer {
    fn start(io: &IoService) {
        let obj = Strand::new(io, FooTimer {
            timer: SteadyTimer::new(),
        });
        SteadyTimer::async_wait_for(|obj| &obj.timer, &Duration::nanoseconds(1), Self::on_nano_wait, &obj);
    }

    fn on_nano_wait(obj: Strand<FooTimer>, res: io::Result<()>) {
        if let Ok(_) = res {
            SteadyTimer::async_wait_for(|obj| &obj.timer, &Duration::milliseconds(2), Self::on_milli_wait, &obj);
        } else {
            panic!();
        }
    }

    fn on_milli_wait(obj: Strand<FooTimer>, res: io::Result<()>) {
        if let Ok(_) = res {
            SteadyTimer::async_wait_for(|obj| &obj.timer, &Duration::seconds(3), Self::on_sec_wait, &obj);
        } else {
            panic!();
        }
    }

    fn on_sec_wait(_: Strand<FooTimer>, res: io::Result<()>) {
        if let Ok(_) = res {
        } else {
            panic!();
        }
    }
}

#[test]
fn tests_async_timer() {
    let io = IoService::new();
    FooTimer::start(&io);
    io.run();
}