chiark / gitweb /
wip queue
[hippotat.git] / src / queue.rs
index 9deb48b584ef9a8313bff5908f685f11d910a672..6b6c7bba6f9a93b2441baa04c440cbe27f208f9c 100644 (file)
@@ -23,6 +23,9 @@ impl<D> PacketQueue<D> where D: AsRef<[u8]> {
     Some(data)
   }
 
+  pub fn content_count(&self) -> usize { self.queue.len() }
+  pub fn content_len(&self) -> usize { self.content }
+
   pub fn is_empty(&self) -> bool { self.queue.is_empty() }
   pub fn peek_front(&self) -> Option<&D> { self.queue.front() }
 }
@@ -53,31 +56,32 @@ impl<E> QueueBuf<E> where E: AsRef<[u8]> {
   pub fn push<B: Into<E>>(&mut self, b: B) {
     self.push_(b.into());
   }
-  pub fn push_(&mut self, b: E) {
+  fn push_(&mut self, b: E) {
     let l = b.as_ref().len();
     self.queue.push_back(b);
     self.content += l;
   }
   pub fn is_empty(&self) -> bool { self.content == 0 }
+  pub fn len(&self) -> usize { self.content }
 }
 
 impl FrameQueueBuf {
-  pub fn push<B: Into<Box<[u8]>>>(&mut self, b: B) {
-    self.push_(b.into());
+  pub fn push_esc<B: Into<Box<[u8]>>>(&mut self, b: B) {
+    self.push_esc_(b.into());
   }
-  pub fn push_(&mut self, b: Box<[u8]>) {
+  fn push_esc_(&mut self, b: Box<[u8]>) {
     self.queue.push_(Cervine::Owned(b));
     self.queue.push_(Cervine::Borrowed(&SLIP_END_SLICE));
   }
-  pub fn is_empty(&self) -> bool { self.queue.is_empty() }
-}
-
-impl<E> Extend<E> for FrameQueueBuf where E: Into<Box<[u8]>> {
-  fn extend<I>(&mut self, it: I)
-  where I: IntoIterator<Item=E>
-  {
-    for b in it { self.push(b) }
+  pub fn esc_push(&mut self, b: Box<[u8]>) {
+    self.queue.push_(Cervine::Borrowed(&SLIP_END_SLICE));
+    self.queue.push_(Cervine::Owned(b));
   }
+  pub fn push_raw(&mut self, b: Box<[u8]>) {
+    self.queue.push_(Cervine::Owned(b));
+  }
+  pub fn is_empty(&self) -> bool { self.queue.is_empty() }
+  pub fn len(&self) -> usize { self.queue.len() }
 }
 
 impl<E> hyper::body::Buf for QueueBuf<E> where E: AsRef<[u8]> {
@@ -104,3 +108,33 @@ impl hyper::body::Buf for FrameQueueBuf {
   fn chunk(&self) -> &[u8] { self.queue.chunk() }
   fn advance(&mut self, cnt: usize) { self.queue.advance(cnt) }
 }
+
+pin_project!{
+  pub struct BufBody<B:Buf> {
+    body: Option<B>,
+  }
+}
+impl<B:Buf> BufBody<B> {
+  pub fn new(body: B) -> Self { Self { body: Some(body ) } }
+}
+impl BufBody<FrameQueueBuf> {
+  pub fn display<S:Display>(s: S) -> Self {
+    let s = s.to_string().into_bytes();
+    let mut buf: FrameQueueBuf = default();
+    buf.push_raw(s.into());
+    Self::new(buf)
+  }
+}
+
+impl<B:Buf> HttpBody for BufBody<B> {
+  type Error = Void;
+  type Data = B;
+  fn poll_data(self: Pin<&mut Self>, _: &mut std::task::Context<'_>)
+               -> Poll<Option<Result<B, Void>>> {
+    Poll::Ready(Ok(self.project().body.take()).transpose())
+  }
+  fn poll_trailers(self: Pin<&mut Self>, _: &mut std::task::Context<'_>)
+ -> Poll<Result<Option<hyper::HeaderMap<hyper::header::HeaderValue>>, Void>> {
+    Poll::Ready(Ok(None))
+  }
+}