lustre-executor 0.2.0

A blazingly fast, minimal async executor with pluggable ID generation and I/O support.
Documentation
use crate::TimerData;
use futures::future::Future;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll as TaskPoll};
use std::time::Duration;

/// Simple async timer future.
pub struct TimerFuture {
    deadline: std::time::Instant,
    timer_data: Arc<Mutex<TimerData>>,
    registered: bool,
}

impl TimerFuture {
    /// Creates a new timer future.
    pub fn new(duration: Duration, timer_data: Arc<Mutex<TimerData>>) -> Self {
        Self {
            deadline: std::time::Instant::now() + duration,
            timer_data,
            registered: false,
        }
    }
}

impl Future for TimerFuture {
    type Output = ();

    fn poll(mut self: std::pin::Pin<&mut Self>, cx: &mut Context) -> TaskPoll<Self::Output> {
        if std::time::Instant::now() >= self.deadline {
            TaskPoll::Ready(())
        } else {
            if !self.registered {
                self.timer_data
                    .lock()
                    .unwrap()
                    .register_timer(self.deadline, cx.waker().clone());
                self.registered = true;
            }
            TaskPoll::Pending
        }
    }
}