1use std::collections::BTreeMap;
12
13use futures::Future;
14use futures::future::{Fuse, FusedFuture};
15
16use crate::key::KeyedData;
17use crate::reactor::Queue;
18
19pub struct SkipQueue<I> {
23 pub job: Option<I>,
25}
26impl<I> Default for SkipQueue<I> {
27 fn default() -> Self {
28 Self { job: None }
29 }
30}
31impl<I> Queue<I> for SkipQueue<I> {
32 fn enqueue(&mut self, input: I) {
33 self.job = Some(input);
35 }
36 fn next<F, O>(&mut self, handle: &Fuse<F>) -> Option<I>
37 where
38 F: Future<Output = O>,
39 {
40 if handle.is_terminated() {
42 self.job.take()
43 } else {
44 None
45 }
46 }
47}
48
49#[derive(Clone, Debug)]
53pub struct KeySkipQueue<K: Ord, D> {
54 pub waiting: BTreeMap<K, D>,
56}
57impl<K: Ord, D> Default for KeySkipQueue<K, D> {
58 fn default() -> Self {
59 Self {
60 waiting: BTreeMap::new(),
61 }
62 }
63}
64impl<K: Ord + Clone, D> Queue<KeyedData<K, D>> for KeySkipQueue<K, D> {
65 fn enqueue(&mut self, input: KeyedData<K, D>) {
66 let KeyedData { key, data } = input;
67 self.waiting.insert(key, data);
68 }
69
70 fn next<F, O>(&mut self, handle: &Fuse<F>) -> Option<KeyedData<K, D>>
71 where
72 F: Future<Output = O>,
73 {
74 if handle.is_terminated() {
76 self.waiting
77 .pop_first()
78 .map(|(key, data)| KeyedData { key, data })
79 } else {
80 None
81 }
82 }
83}