Skip to content

refactor io dir to be same with std and export IntoInnerError #526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/io/buf_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -72,7 +70,7 @@ impl<R: io::Read> BufReader<R> {
/// # Ok(()) }) }
/// ```
pub fn new(inner: R) -> BufReader<R> {
BufReader::with_capacity(DEFAULT_CAPACITY, inner)
BufReader::with_capacity(DEFAULT_BUF_SIZE, inner)
}

/// Creates a new buffered reader with the specified capacity.
Expand Down
32 changes: 27 additions & 5 deletions src/io/buf_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -87,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>(W, std::io::Error);
pub struct IntoInnerError<W>(W, crate::io::Error);

impl<W: Write> BufWriter<W> {
/// Creates a new `BufWriter` with a default buffer capacity. The default is currently 8 KB,
Expand All @@ -107,7 +129,7 @@ impl<W: Write> BufWriter<W> {
/// # Ok(()) }) }
/// ```
pub fn new(inner: W) -> BufWriter<W> {
BufWriter::with_capacity(DEFAULT_CAPACITY, inner)
BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
}

/// Creates a new `BufWriter` with the specified buffer capacity.
Expand Down
4 changes: 3 additions & 1 deletion src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,15 @@
//! [`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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this const be marked pub(crate)? Is that even possible?

I guess I'm a bit confused how you're accessing this from other crates without it having a visibility specifier haha. Feel like I'm missing something about how consts work :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I think sub mod always can access father mod.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just like write tests in sub mod even for test some private functions.
I think pub(crate) is for being public to current crate and private to extern crate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See std::io, I just imitate its way of writing.
image


cfg_std! {
#[doc(inline)]
pub use std::io::{Error, ErrorKind, IoSlice, IoSliceMut, Result, SeekFrom};

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};
Expand Down