1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use crate::reactor::Queue;
use futures::future::Fuse;
use futures::Future;

/// Immediate switching implementation.
/// Whenever any job arrives, it replaces the currently running job as soon as possible.
pub struct Immediate<I> {
    /// Waiting job.
    pub job: Option<I>,
}
impl<I> Default for Immediate<I> {
    fn default() -> Self {
        Self { job: None }
    }
}
impl<I> Queue<I> for Immediate<I> {
    fn enqueue(&mut self, input: I) {
        // Replace any waiting job with te input.
        self.job = Some(input);
    }
    fn next<F, O>(&mut self, _handle: &Fuse<F>) -> Option<I>
    where
        F: Future<Output = O>,
    {
        // Release new input regardless of what is currently running.
        self.job.take()
    }
}