chiark / gitweb /
wip packetqueue
[hippotat.git] / src / queue.rs
index ba1d4ffedf76bbf55056e6e2b3824ed972000258..9deb48b584ef9a8313bff5908f685f11d910a672 100644 (file)
@@ -4,19 +4,42 @@
 
 use crate::prelude::*;
 
+// xxx are we using this at all ?
 #[derive(Default,Clone)]
-pub struct Queue<E> {
+pub struct PacketQueue<D> {
+  queue: VecDeque<D>,
+  content: usize,
+}
+
+impl<D> PacketQueue<D> where D: AsRef<[u8]> {
+  pub fn push_back(&mut self, data: D) {
+    self.content += data.as_ref().len();
+    self.queue.push_back(data);
+  }
+
+  pub fn pop_front(&mut self) -> Option<D> {
+    let data = self.queue.pop_front()?;
+    self.content -= data.as_ref().len();
+    Some(data)
+  }
+
+  pub fn is_empty(&self) -> bool { self.queue.is_empty() }
+  pub fn peek_front(&self) -> Option<&D> { self.queue.front() }
+}
+
+#[derive(Default,Clone)]
+pub struct QueueBuf<E> {
   content: usize,
   eaten1: usize, // 0 <= eaten1 < queue.front()...len()
   queue: VecDeque<E>,
 }
 
 #[derive(Default,Debug,Clone)]
-pub struct FrameQueue {
-  queue: Queue<Cervine<'static, Box<[u8]>, [u8]>>,
+pub struct FrameQueueBuf {
+  queue: QueueBuf<Cervine<'static, Box<[u8]>, [u8]>>,
 }
 
-impl<E> Debug for Queue<E> where E: AsRef<[u8]> {
+impl<E> Debug for QueueBuf<E> where E: AsRef<[u8]> {
   #[throws(fmt::Error)]
   fn fmt(&self, f: &mut fmt::Formatter) {
     write!(f, "Queue{{content={},eaten1={},queue=[",
@@ -26,7 +49,7 @@ impl<E> Debug for Queue<E> where E: AsRef<[u8]> {
   }
 }
 
-impl<E> Queue<E> where E: AsRef<[u8]> {
+impl<E> QueueBuf<E> where E: AsRef<[u8]> {
   pub fn push<B: Into<E>>(&mut self, b: B) {
     self.push_(b.into());
   }
@@ -38,7 +61,7 @@ impl<E> Queue<E> where E: AsRef<[u8]> {
   pub fn is_empty(&self) -> bool { self.content == 0 }
 }
 
-impl FrameQueue {
+impl FrameQueueBuf {
   pub fn push<B: Into<Box<[u8]>>>(&mut self, b: B) {
     self.push_(b.into());
   }
@@ -49,7 +72,7 @@ impl FrameQueue {
   pub fn is_empty(&self) -> bool { self.queue.is_empty() }
 }
 
-impl<E> Extend<E> for FrameQueue where E: Into<Box<[u8]>> {
+impl<E> Extend<E> for FrameQueueBuf where E: Into<Box<[u8]>> {
   fn extend<I>(&mut self, it: I)
   where I: IntoIterator<Item=E>
   {
@@ -57,7 +80,7 @@ impl<E> Extend<E> for FrameQueue where E: Into<Box<[u8]>> {
   }
 }
 
-impl<E> hyper::body::Buf for Queue<E> where E: AsRef<[u8]> {
+impl<E> hyper::body::Buf for QueueBuf<E> where E: AsRef<[u8]> {
   fn remaining(&self) -> usize { self.content }
   fn chunk(&self) -> &[u8] {
     let front = if let Some(f) = self.queue.front() { f } else { return &[] };
@@ -76,7 +99,7 @@ impl<E> hyper::body::Buf for Queue<E> where E: AsRef<[u8]> {
   }
 }
 
-impl hyper::body::Buf for FrameQueue {
+impl hyper::body::Buf for FrameQueueBuf {
   fn remaining(&self) -> usize { self.queue.remaining() }
   fn chunk(&self) -> &[u8] { self.queue.chunk() }
   fn advance(&mut self, cnt: usize) { self.queue.advance(cnt) }