Skip to main content

hotshot_new_protocol/
outbox.rs

1use std::{
2    collections::{VecDeque, vec_deque},
3    mem,
4};
5
6#[derive(Debug, Clone)]
7pub struct Outbox<T>(VecDeque<T>);
8
9impl<T> Default for Outbox<T> {
10    fn default() -> Self {
11        Self(VecDeque::new())
12    }
13}
14
15impl<T> Outbox<T> {
16    pub fn new() -> Self {
17        Self::default()
18    }
19
20    pub fn is_empty(&self) -> bool {
21        self.0.is_empty()
22    }
23
24    pub fn len(&self) -> usize {
25        self.0.len()
26    }
27
28    pub fn clear(&mut self) {
29        self.0.clear()
30    }
31
32    pub fn push_back<U: Into<T>>(&mut self, item: U) {
33        self.0.push_back(item.into())
34    }
35
36    pub fn pop_front(&mut self) -> Option<T> {
37        self.0.pop_front()
38    }
39
40    pub fn iter(&self) -> impl Iterator<Item = &T> {
41        self.0.iter()
42    }
43
44    pub fn take(&mut self) -> Self {
45        Self(mem::take(&mut self.0))
46    }
47}
48
49impl<T> IntoIterator for Outbox<T> {
50    type IntoIter = vec_deque::IntoIter<T>;
51    type Item = T;
52
53    fn into_iter(self) -> Self::IntoIter {
54        self.0.into_iter()
55    }
56}
57
58impl<'a, T> IntoIterator for &'a Outbox<T> {
59    type IntoIter = vec_deque::Iter<'a, T>;
60    type Item = &'a T;
61
62    fn into_iter(self) -> Self::IntoIter {
63        self.0.iter()
64    }
65}
66
67impl<T> Extend<T> for Outbox<T> {
68    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
69        self.0.extend(iter)
70    }
71}
72
73impl<T: PartialEq> Outbox<T> {
74    pub fn contains(&self, item: &T) -> bool {
75        self.0.contains(item)
76    }
77}