chiark / gitweb /
own http body type
[hippotat.git] / src / queue.rs
index 9deb48b584ef9a8313bff5908f685f11d910a672..5e22de9fb21eebfa40366e8945be27a6e87a1d5a 100644 (file)
@@ -59,6 +59,7 @@ impl<E> QueueBuf<E> where E: AsRef<[u8]> {
     self.content += l;
   }
   pub fn is_empty(&self) -> bool { self.content == 0 }
+  pub fn len(&self) -> usize { self.content }
 }
 
 impl FrameQueueBuf {
@@ -70,6 +71,7 @@ impl FrameQueueBuf {
     self.queue.push_(Cervine::Borrowed(&SLIP_END_SLICE));
   }
   pub fn is_empty(&self) -> bool { self.queue.is_empty() }
+  pub fn len(&self) -> usize { self.queue.len() }
 }
 
 impl<E> Extend<E> for FrameQueueBuf where E: Into<Box<[u8]>> {
@@ -104,3 +106,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(s);
+    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))
+  }
+}