[solved]Behave unexpectedly about the mio::Timer


#1

Below codes; I expect will generate K to the power of running time ( geometric ratio ) amount of u64 integer, but In fact it only generate u64 integer in linear ratio . I don’t know why, and the mio::timer::Timer was introduce in Mio 0.6, lack of document. Whether someone is familiar with MIO ?

extern crate mio;

use std::time::Duration;
use mio::*;
use mio::timer::Timer;

const K: usize = 8;
const SCHEDULE: Token = Token(1);

struct Counter {
    counter: u64,
}

impl Counter {
    fn new() -> Counter {
        Counter { counter: 0 }
    }

    fn gen(&mut self) -> u64 {
        self.counter += 1;
        self.counter
    }
}

fn main() {
    let mut poll = Poll::new().unwrap();
    let mut schedule = Timer::default();
    let mut counter = Counter::new();
    
    poll.register(&schedule, SCHEDULE, Ready::readable(),
                  PollOpt::edge()).unwrap();
    schedule.set_timeout(Duration::from_millis(1000) , counter.gen());

    let mut events = Events::with_capacity(1024);
    loop {
        poll.poll(&mut events, None).unwrap();

        // for event in events.iter() {
        //     match event.token() {
        //         SCHEDULE => {
                    println!("{}", schedule.poll().unwrap());
                    for i in 0..K as u64 {
                        schedule.set_timeout(Duration::from_millis(1000 + i*10), counter.gen());
                    }
        //         }
        //         _ => unreachable!()
        //     }
        // }
    }           
}


#2

@carllerche Could you please give me some advice?


#3

solved;

SCHEDULE => {
                    loop {
                        match  schedule.poll() {
                            Some(x) => {
                                println!("{}", x);
                                for i in 0..K as u64 {
                                    schedule.set_timeout(Duration::from_millis(1000 + i*10), counter.gen()).unwrap();
                                }
                            }
                            None => break,
                        }
                    }
                }