1use crate::Error;
28use crate::Result;
29
30use std::collections::VecDeque;
31
32#[derive(Default)]
34pub struct DatagramQueue {
35 queue: Option<VecDeque<Vec<u8>>>,
36 queue_max_len: usize,
37 queue_bytes_size: usize,
38}
39
40impl DatagramQueue {
41 pub fn new(queue_max_len: usize) -> Self {
42 DatagramQueue {
43 queue: None,
44 queue_bytes_size: 0,
45 queue_max_len,
46 }
47 }
48
49 pub fn push(&mut self, data: Vec<u8>) -> Result<()> {
50 if self.is_full() {
51 return Err(Error::Done);
52 }
53
54 self.queue_bytes_size += data.len();
55 self.queue
56 .get_or_insert_with(Default::default)
57 .push_back(data);
58
59 Ok(())
60 }
61
62 pub fn peek_front_len(&self) -> Option<usize> {
63 self.queue.as_ref().and_then(|q| q.front().map(|d| d.len()))
64 }
65
66 pub fn peek_front_bytes(&self, buf: &mut [u8], len: usize) -> Result<usize> {
67 match self.queue.as_ref().and_then(|q| q.front()) {
68 Some(d) => {
69 let len = std::cmp::min(len, d.len());
70 if buf.len() < len {
71 return Err(Error::BufferTooShort);
72 }
73
74 buf[..len].copy_from_slice(&d[..len]);
75 Ok(len)
76 },
77
78 None => Err(Error::Done),
79 }
80 }
81
82 pub fn pop(&mut self) -> Option<Vec<u8>> {
83 if let Some(d) = self.queue.as_mut().and_then(|q| q.pop_front()) {
84 self.queue_bytes_size = self.queue_bytes_size.saturating_sub(d.len());
85 return Some(d);
86 }
87
88 None
89 }
90
91 pub fn has_pending(&self) -> bool {
92 !self.queue.as_ref().map(|q| q.is_empty()).unwrap_or(true)
93 }
94
95 pub fn purge<F: Fn(&[u8]) -> bool>(&mut self, f: F) {
96 if let Some(q) = self.queue.as_mut() {
97 q.retain(|d| !f(d));
98 self.queue_bytes_size = q.iter().fold(0, |total, d| total + d.len());
99 }
100 }
101
102 pub fn is_full(&self) -> bool {
103 self.len() == self.queue_max_len
104 }
105
106 pub fn is_empty(&self) -> bool {
107 self.len() == 0
108 }
109
110 pub fn len(&self) -> usize {
111 self.queue.as_ref().map(|q| q.len()).unwrap_or(0)
112 }
113
114 pub fn byte_size(&self) -> usize {
115 self.queue_bytes_size
116 }
117}