<commit_before>use std::time;

use quic::engine::timer::{ScheduledEvent, Timer};


#[derive(Clone, Debug, Default, PartialEq)]
pub struct ThreadedTimer {
    // TODO
}

impl ThreadedTimer {
    pub fn new() -> ThreadedTimer {
        ThreadedTimer {}
    }
}

impl Timer for ThreadedTimer {
    fn now(&self) -> time::Instant {
        unimplemented!()
    }

    fn schedule(&mut self, when: time::Duration, event: ScheduledEvent) {
        unimplemented!()
    }
}<commit_msg>Threaded timer<commit_after>use std::time;

use quic::engine::timer::{ScheduledEvent, Timer};


#[derive(Clone, Debug, PartialEq)]
struct ScheduledItem {
    event: ScheduledEvent,
    instant: time::Instant,
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct ThreadedTimer {
    scheduled_items: Vec<ScheduledItem>,
}

impl ThreadedTimer {
    pub fn new() -> ThreadedTimer {
        ThreadedTimer::default()
    }

    pub fn time_until_next_event(&self) -> Option<time::Duration> {
        self.scheduled_items.iter()
        .map(|item| item.instant - time::Instant::now())
        .min()
    }

    pub fn due_events(&mut self) -> Vec<ScheduledEvent> {
        let (pending, due) =
            self.scheduled_items.drain(..)
            .partition(|item| item.instant > time::Instant::now());

        self.scheduled_items = pending;

        due.into_iter().map(|item| item.event).collect()
    }
}

impl Timer for ThreadedTimer {
    fn now(&self) -> time::Instant {
        time::Instant::now()
    }

    fn schedule(&mut self, when: time::Duration, event: ScheduledEvent) {
        self.scheduled_items.push(ScheduledItem {
            event: event,
            instant: time::Instant::now() + when,
        })
    }
}
{'fraction_non_alphanumeric': 0.14419147694103912, 'fraction_numerical': 0.0, 'mean_word_length': 2.9584295612009237, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2198251', 'n_tokens_mistral': 548, 'n_tokens_neox': 544, 'n_words': 113}