queue.rs (1217B)
1 use std::collections::{btree_set, BTreeSet}; 2 3 use crate::relay::{OutboxSubId, RelayTask}; 4 5 /// QueuedTasks stores subscription work that could not be scheduled immediately. 6 #[derive(Default)] 7 pub struct QueuedTasks { 8 tasks: BTreeSet<OutboxSubId>, 9 } 10 11 impl QueuedTasks { 12 pub fn add(&mut self, id: OutboxSubId, task: RelayTask) { 13 match task { 14 RelayTask::Unsubscribe => { 15 // i guess swap remove is ok here? it's not super important to maintain strict insertion order 16 if !self.tasks.contains(&id) { 17 return; 18 } 19 self.tasks.remove(&id); 20 } 21 RelayTask::Subscribe => { 22 self.tasks.insert(id); 23 } 24 } 25 } 26 27 pub fn pop(&mut self) -> Option<OutboxSubId> { 28 self.tasks.pop_last() 29 } 30 31 pub fn is_empty(&self) -> bool { 32 self.tasks.is_empty() 33 } 34 35 #[allow(dead_code)] 36 pub fn len(&self) -> usize { 37 self.tasks.len() 38 } 39 } 40 41 impl IntoIterator for QueuedTasks { 42 type Item = OutboxSubId; 43 type IntoIter = btree_set::IntoIter<OutboxSubId>; 44 45 fn into_iter(self) -> Self::IntoIter { 46 self.tasks.into_iter() 47 } 48 }