From 9d634cb2a7cc045d3e3b8da0ad1e40ffc3cb525a Mon Sep 17 00:00:00 2001 From: yjhmelody <465402634@qq.com> Date: Wed, 13 Nov 2019 12:42:59 +0800 Subject: [PATCH 1/2] refactor io dir to be same with std --- src/io/buf_reader.rs | 6 ++---- src/io/buf_writer.rs | 6 ++---- src/io/mod.rs | 2 ++ 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/io/buf_reader.rs b/src/io/buf_reader.rs index 1d00b52..e6d8e66 100644 --- a/src/io/buf_reader.rs +++ b/src/io/buf_reader.rs @@ -4,11 +4,9 @@ use std::{cmp, fmt}; use pin_project_lite::pin_project; -use crate::io::{self, BufRead, Read, Seek, SeekFrom}; +use crate::io::{self, BufRead, Read, Seek, SeekFrom, DEFAULT_BUF_SIZE}; use crate::task::{Context, Poll}; -const DEFAULT_CAPACITY: usize = 8 * 1024; - pin_project! { /// Adds buffering to any reader. /// @@ -72,7 +70,7 @@ impl BufReader { /// # Ok(()) }) } /// ``` pub fn new(inner: R) -> BufReader { - BufReader::with_capacity(DEFAULT_CAPACITY, inner) + BufReader::with_capacity(DEFAULT_BUF_SIZE, inner) } /// Creates a new buffered reader with the specified capacity. diff --git a/src/io/buf_writer.rs b/src/io/buf_writer.rs index 8fa9eba..35b511f 100644 --- a/src/io/buf_writer.rs +++ b/src/io/buf_writer.rs @@ -4,11 +4,9 @@ use std::pin::Pin; use pin_project_lite::pin_project; use crate::io::write::WriteExt; -use crate::io::{self, Seek, SeekFrom, Write}; +use crate::io::{self, Seek, SeekFrom, Write, DEFAULT_BUF_SIZE}; use crate::task::{Context, Poll, ready}; -const DEFAULT_CAPACITY: usize = 8 * 1024; - pin_project! { /// Wraps a writer and buffers its output. /// @@ -107,7 +105,7 @@ impl BufWriter { /// # Ok(()) }) } /// ``` pub fn new(inner: W) -> BufWriter { - BufWriter::with_capacity(DEFAULT_CAPACITY, inner) + BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner) } /// Creates a new `BufWriter` with the specified buffer capacity. diff --git a/src/io/mod.rs b/src/io/mod.rs index 4e83230..065aaab 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -269,6 +269,8 @@ //! [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html //! [`.unwrap()`]: https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap +const DEFAULT_BUF_SIZE: usize = 8 * 1024; + cfg_std! { #[doc(inline)] pub use std::io::{Error, ErrorKind, IoSlice, IoSliceMut, Result, SeekFrom}; From 5adb112a00b7854f16025a6f343796723c444fe1 Mon Sep 17 00:00:00 2001 From: yjhmelody <465402634@qq.com> Date: Wed, 13 Nov 2019 13:52:16 +0800 Subject: [PATCH 2/2] export IntoInnerError for io --- src/io/buf_writer.rs | 26 +++++++++++++++++++++++++- src/io/mod.rs | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/io/buf_writer.rs b/src/io/buf_writer.rs index 35b511f..ce6a97b 100644 --- a/src/io/buf_writer.rs +++ b/src/io/buf_writer.rs @@ -85,8 +85,32 @@ pin_project! { } } +/// An error returned by `into_inner` which combines an error that +/// happened while writing out the buffer, and the buffered writer object +/// which may be used to recover from the condition. +/// +/// # Examples +/// +/// ```no_run +/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// use async_std::io::BufWriter; +/// use async_std::net::TcpStream; +/// +/// let buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34251").await?); +/// +/// // unwrap the TcpStream and flush the buffer +/// let stream = match buf_writer.into_inner().await { +/// Ok(s) => s, +/// Err(e) => { +/// // Here, e is an IntoInnerError +/// panic!("An error occurred"); +/// } +/// }; +/// # +/// # Ok(()) }) } +///``` #[derive(Debug)] -pub struct IntoInnerError(W, std::io::Error); +pub struct IntoInnerError(W, crate::io::Error); impl BufWriter { /// Creates a new `BufWriter` with a default buffer capacity. The default is currently 8 KB, diff --git a/src/io/mod.rs b/src/io/mod.rs index 065aaab..0c8144b 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -277,7 +277,7 @@ cfg_std! { pub use buf_read::{BufRead, Lines}; pub use buf_reader::BufReader; - pub use buf_writer::BufWriter; + pub use buf_writer::{BufWriter, IntoInnerError}; pub use copy::copy; pub use cursor::Cursor; pub use empty::{empty, Empty};