chiark / gitweb /
rename methods etc.
[hippotat.git] / src / queue.rs
1 // Copyright 2021 Ian Jackson and contributors to Hippotat
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 // There is NO WARRANTY.
4
5 use crate::prelude::*;
6
7 // xxx are we using this at all ?
8 #[derive(Default,Clone)]
9 pub struct PacketQueue<D> {
10   queue: VecDeque<D>,
11   content: usize,
12 }
13
14 impl<D> PacketQueue<D> where D: AsRef<[u8]> {
15   pub fn push_back(&mut self, data: D) {
16     self.content += data.as_ref().len();
17     self.queue.push_back(data);
18   }
19
20   pub fn pop_front(&mut self) -> Option<D> {
21     let data = self.queue.pop_front()?;
22     self.content -= data.as_ref().len();
23     Some(data)
24   }
25
26   pub fn is_empty(&self) -> bool { self.queue.is_empty() }
27   pub fn peek_front(&self) -> Option<&D> { self.queue.front() }
28 }
29
30 #[derive(Default,Clone)]
31 pub struct QueueBuf<E> {
32   content: usize,
33   eaten1: usize, // 0 <= eaten1 < queue.front()...len()
34   queue: VecDeque<E>,
35 }
36
37 #[derive(Default,Debug,Clone)]
38 pub struct FrameQueueBuf {
39   queue: QueueBuf<Cervine<'static, Box<[u8]>, [u8]>>,
40 }
41
42 impl<E> Debug for QueueBuf<E> where E: AsRef<[u8]> {
43   #[throws(fmt::Error)]
44   fn fmt(&self, f: &mut fmt::Formatter) {
45     write!(f, "Queue{{content={},eaten1={},queue=[",
46            self.content, self.eaten1)?;
47     for q in &self.queue { write!(f, "{},", q.as_ref().len())?; }
48     write!(f, "]}}")?;
49   }
50 }
51
52 impl<E> QueueBuf<E> where E: AsRef<[u8]> {
53   pub fn push<B: Into<E>>(&mut self, b: B) {
54     self.push_(b.into());
55   }
56   fn push_(&mut self, b: E) {
57     let l = b.as_ref().len();
58     self.queue.push_back(b);
59     self.content += l;
60   }
61   pub fn is_empty(&self) -> bool { self.content == 0 }
62   pub fn len(&self) -> usize { self.content }
63 }
64
65 impl FrameQueueBuf {
66   pub fn push_esc<B: Into<Box<[u8]>>>(&mut self, b: B) {
67     self.push_esc_(b.into());
68   }
69   fn push_esc_(&mut self, b: Box<[u8]>) {
70     self.queue.push_(Cervine::Owned(b));
71     self.queue.push_(Cervine::Borrowed(&SLIP_END_SLICE));
72   }
73   pub fn esc_push(&mut self, b: Box<[u8]>) {
74     self.queue.push_(Cervine::Borrowed(&SLIP_END_SLICE));
75     self.queue.push_(Cervine::Owned(b));
76   }
77   pub fn push_raw(&mut self, b: Box<[u8]>) {
78     self.queue.push_(Cervine::Owned(b));
79   }
80   pub fn is_empty(&self) -> bool { self.queue.is_empty() }
81   pub fn len(&self) -> usize { self.queue.len() }
82 }
83
84 impl<E> hyper::body::Buf for QueueBuf<E> where E: AsRef<[u8]> {
85   fn remaining(&self) -> usize { self.content }
86   fn chunk(&self) -> &[u8] {
87     let front = if let Some(f) = self.queue.front() { f } else { return &[] };
88     &front.as_ref()[ self.eaten1.. ]
89   }
90   fn advance(&mut self, cnt: usize) {
91     self.content -= cnt;
92     self.eaten1 += cnt;
93     loop {
94       if self.eaten1 == 0 { break }
95       let front = self.queue.front().unwrap();
96       if self.eaten1 < front.as_ref().len() { break; }
97       self.eaten1 -= front.as_ref().len();
98       self.queue.pop_front().unwrap();
99     }
100   }
101 }
102
103 impl hyper::body::Buf for FrameQueueBuf {
104   fn remaining(&self) -> usize { self.queue.remaining() }
105   fn chunk(&self) -> &[u8] { self.queue.chunk() }
106   fn advance(&mut self, cnt: usize) { self.queue.advance(cnt) }
107 }
108
109 pin_project!{
110   pub struct BufBody<B:Buf> {
111     body: Option<B>,
112   }
113 }
114 impl<B:Buf> BufBody<B> {
115   pub fn new(body: B) -> Self { Self { body: Some(body ) } }
116 }
117 impl BufBody<FrameQueueBuf> {
118   pub fn display<S:Display>(s: S) -> Self {
119     let s = s.to_string().into_bytes();
120     let mut buf: FrameQueueBuf = default();
121     buf.push_raw(s.into());
122     Self::new(buf)
123   }
124 }
125
126 impl<B:Buf> HttpBody for BufBody<B> {
127   type Error = Void;
128   type Data = B;
129   fn poll_data(self: Pin<&mut Self>, _: &mut std::task::Context<'_>)
130                -> Poll<Option<Result<B, Void>>> {
131     Poll::Ready(Ok(self.project().body.take()).transpose())
132   }
133   fn poll_trailers(self: Pin<&mut Self>, _: &mut std::task::Context<'_>)
134  -> Poll<Result<Option<hyper::HeaderMap<hyper::header::HeaderValue>>, Void>> {
135     Poll::Ready(Ok(None))
136   }
137 }