From da2335bd570eab4ad8364bc417e97f69c7350d7d Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Wed, 9 Oct 2019 14:45:36 +0200 Subject: [PATCH] Cleanup BufWriter --- src/io/buf_writer.rs | 103 ++++--------------------------------------- tests/buf_writer.rs | 71 +++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 94 deletions(-) create mode 100644 tests/buf_writer.rs diff --git a/src/io/buf_writer.rs b/src/io/buf_writer.rs index 2b7545a1..440e2705 100644 --- a/src/io/buf_writer.rs +++ b/src/io/buf_writer.rs @@ -1,10 +1,11 @@ -use crate::task::{Context, Poll}; -use futures_core::ready; -use futures_io::{AsyncSeek, AsyncWrite, SeekFrom}; use std::fmt; -use std::io; use std::pin::Pin; +use futures_core::ready; + +use crate::io::{self, Seek, SeekFrom, Write}; +use crate::task::{Context, Poll}; + const DEFAULT_CAPACITY: usize = 8 * 1024; /// Wraps a writer and buffers its output. @@ -82,7 +83,7 @@ pub struct BufWriter { written: usize, } -impl BufWriter { +impl BufWriter { pin_utils::unsafe_pinned!(inner: W); pin_utils::unsafe_unpinned!(buf: Vec); @@ -173,10 +174,6 @@ impl BufWriter { &mut self.inner } - // pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut W> { - // self.inner() - // } - /// Consumes BufWriter, returning the underlying writer /// /// This method will not write leftover data, it will be lost. @@ -187,10 +184,6 @@ impl BufWriter { self.inner } - // pub fn poll_into_inner(self: Pin<&mut Self>, _cx: Context<'_>) -> Poll> { - // unimplemented!("poll into inner method") - // } - /// Returns a reference to the internally buffered data. /// /// # Examples @@ -251,7 +244,7 @@ impl BufWriter { } } -impl AsyncWrite for BufWriter { +impl Write for BufWriter { fn poll_write( mut self: Pin<&mut Self>, cx: &mut Context<'_>, @@ -278,7 +271,7 @@ impl AsyncWrite for BufWriter { } } -impl fmt::Debug for BufWriter { +impl fmt::Debug for BufWriter { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("BufReader") .field("writer", &self.inner) @@ -287,11 +280,10 @@ impl fmt::Debug for BufWriter { } } -impl AsyncSeek for BufWriter { +impl Seek for BufWriter { /// Seek to the offset, in bytes, in the underlying writer. /// /// Seeking always writes out the internal buffer before seeking. - fn poll_seek( mut self: Pin<&mut Self>, cx: &mut Context<'_>, @@ -301,80 +293,3 @@ impl AsyncSeek for BufWriter { self.inner().poll_seek(cx, pos) } } - -mod tests { - #![allow(unused_imports)] - - use super::BufWriter; - use crate::io::{self, SeekFrom}; - use crate::prelude::*; - use crate::task; - - #[test] - fn test_buffered_writer() { - task::block_on(async { - let inner = Vec::new(); - let mut writer = BufWriter::with_capacity(2, inner); - - writer.write(&[0, 1]).await.unwrap(); - assert_eq!(writer.buffer(), []); - assert_eq!(*writer.get_ref(), [0, 1]); - - writer.write(&[2]).await.unwrap(); - assert_eq!(writer.buffer(), [2]); - assert_eq!(*writer.get_ref(), [0, 1]); - - writer.write(&[3]).await.unwrap(); - assert_eq!(writer.buffer(), [2, 3]); - assert_eq!(*writer.get_ref(), [0, 1]); - - writer.flush().await.unwrap(); - assert_eq!(writer.buffer(), []); - assert_eq!(*writer.get_ref(), [0, 1, 2, 3]); - - writer.write(&[4]).await.unwrap(); - writer.write(&[5]).await.unwrap(); - assert_eq!(writer.buffer(), [4, 5]); - assert_eq!(*writer.get_ref(), [0, 1, 2, 3]); - - writer.write(&[6]).await.unwrap(); - assert_eq!(writer.buffer(), [6]); - assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5]); - - writer.write(&[7, 8]).await.unwrap(); - assert_eq!(writer.buffer(), []); - assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8]); - - writer.write(&[9, 10, 11]).await.unwrap(); - assert_eq!(writer.buffer(), []); - assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); - - writer.flush().await.unwrap(); - assert_eq!(writer.buffer(), []); - assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); - }) - } - - #[test] - fn test_buffered_writer_inner_into_inner_does_not_flush() { - task::block_on(async { - let mut w = BufWriter::with_capacity(3, Vec::new()); - w.write(&[0, 1]).await.unwrap(); - assert_eq!(*w.get_ref(), []); - let w = w.into_inner(); - assert_eq!(w, []); - }) - } - - #[test] - fn test_buffered_writer_seek() { - task::block_on(async { - let mut w = BufWriter::with_capacity(3, io::Cursor::new(Vec::new())); - w.write_all(&[0, 1, 2, 3, 4, 5]).await.unwrap(); - w.write_all(&[6, 7]).await.unwrap(); - assert_eq!(w.seek(SeekFrom::Current(0)).await.ok(), Some(8)); - assert_eq!(&w.get_ref().get_ref()[..], &[0, 1, 2, 3, 4, 5, 6, 7][..]); - assert_eq!(w.seek(SeekFrom::Start(2)).await.ok(), Some(2)); - }) - } -} diff --git a/tests/buf_writer.rs b/tests/buf_writer.rs new file mode 100644 index 00000000..188cd8c8 --- /dev/null +++ b/tests/buf_writer.rs @@ -0,0 +1,71 @@ +use async_std::io::{self, BufWriter, SeekFrom}; +use async_std::prelude::*; +use async_std::task; + +#[test] +fn test_buffered_writer() { + task::block_on(async { + let inner = Vec::new(); + let mut writer = BufWriter::with_capacity(2, inner); + + writer.write(&[0, 1]).await.unwrap(); + assert_eq!(writer.buffer(), []); + assert_eq!(*writer.get_ref(), [0, 1]); + + writer.write(&[2]).await.unwrap(); + assert_eq!(writer.buffer(), [2]); + assert_eq!(*writer.get_ref(), [0, 1]); + + writer.write(&[3]).await.unwrap(); + assert_eq!(writer.buffer(), [2, 3]); + assert_eq!(*writer.get_ref(), [0, 1]); + + writer.flush().await.unwrap(); + assert_eq!(writer.buffer(), []); + assert_eq!(*writer.get_ref(), [0, 1, 2, 3]); + + writer.write(&[4]).await.unwrap(); + writer.write(&[5]).await.unwrap(); + assert_eq!(writer.buffer(), [4, 5]); + assert_eq!(*writer.get_ref(), [0, 1, 2, 3]); + + writer.write(&[6]).await.unwrap(); + assert_eq!(writer.buffer(), [6]); + assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5]); + + writer.write(&[7, 8]).await.unwrap(); + assert_eq!(writer.buffer(), []); + assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8]); + + writer.write(&[9, 10, 11]).await.unwrap(); + assert_eq!(writer.buffer(), []); + assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); + + writer.flush().await.unwrap(); + assert_eq!(writer.buffer(), []); + assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); + }) +} + +#[test] +fn test_buffered_writer_inner_into_inner_does_not_flush() { + task::block_on(async { + let mut w = BufWriter::with_capacity(3, Vec::new()); + w.write(&[0, 1]).await.unwrap(); + assert_eq!(*w.get_ref(), []); + let w = w.into_inner(); + assert_eq!(w, []); + }) +} + +#[test] +fn test_buffered_writer_seek() { + task::block_on(async { + let mut w = BufWriter::with_capacity(3, io::Cursor::new(Vec::new())); + w.write_all(&[0, 1, 2, 3, 4, 5]).await.unwrap(); + w.write_all(&[6, 7]).await.unwrap(); + assert_eq!(w.seek(SeekFrom::Current(0)).await.ok(), Some(8)); + assert_eq!(&w.get_ref().get_ref()[..], &[0, 1, 2, 3, 4, 5, 6, 7][..]); + assert_eq!(w.seek(SeekFrom::Start(2)).await.ok(), Some(2)); + }) +}