chiark / gitweb /
packetframe: wip some basic tests, and fixes
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 16 Apr 2021 01:05:41 +0000 (02:05 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 23 Apr 2021 18:32:07 +0000 (19:32 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/packetframe.rs

index 9876f04d90c34eb6eb380f3289671c06996821a1..6e91fa25b19866c173be67023eb3dcf66f291b92 100644 (file)
@@ -117,6 +117,10 @@ impl Display for Broken {
 }
 
 impl<R:BufRead> FrameReader<R> {
+  pub fn new(r: R) -> FrameReader<R> {
+    FrameReader { inner: Fuse(Ok(r)), in_frame: None }
+  }
+
   #[throws(io::Error)]
   pub fn new_frame<'r>(&'r mut self) -> ReadFrame<'r,R> {
     if self.in_frame.is_some() {
@@ -173,6 +177,10 @@ impl<'r, R:BufRead> Read for ReadFrame<'r, R> {
 }
 
 impl<W:Write> FrameWriter<W> {
+  pub fn new(w: W) -> FrameWriter<W> {
+    FrameWriter { inner: Fuse(Ok(w)), in_frame: None }
+  }
+
   #[throws(io::Error)]
   pub fn new_frame<'w>(&'w mut self) -> WriteFrame<'w,W> {
     self.tidy(Err(SenderError))?;
@@ -215,7 +223,7 @@ impl<'w,W:Write> Drop for WriteFrameRaw<'w,W> {
       .unwrap_or_else(|_: io::Error| () /* Fuse will replicate this */);
   }
 }
-impl<'r, R:Write> Write for WriteFrameRaw<'r, R> {
+impl<'w,W:Write> Write for WriteFrameRaw<'w,W> {
   #[throws(io::Error)]
   fn write(&mut self, buf: &[u8]) -> usize {
     let now = min(buf.len(), CHUNK_MAX.into());
@@ -229,3 +237,20 @@ impl<'r, R:Write> Write for WriteFrameRaw<'r, R> {
     self.fw.inner.flush()?
   }
 }
+impl<'w,W:Write> Write for WriteFrame<'w,W> {
+  #[throws(io::Error)]
+  fn write(&mut self, buf: &[u8]) -> usize { self.buf.write(buf)? }
+  #[throws(io::Error)]
+  fn flush(&mut self) { self.buf.flush()? }
+}
+
+#[test]
+fn write_test(){
+  let mut buf = vec![];
+  let mut wr = FrameWriter::new(&mut buf);
+  {
+    let mut frame = wr.new_frame().unwrap();
+    frame.write(b"hi").unwrap();
+  }
+  dbg!(buf);
+}