-
Notifications
You must be signed in to change notification settings - Fork 660
Add AsyncBufRead trait #1552
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
Add AsyncBufRead trait #1552
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,18 +5,12 @@ | |
//! `AsyncReadExt` and `AsyncWriteExt` traits which add methods | ||
//! to the `AsyncRead` and `AsyncWrite` types. | ||
|
||
use std::vec::Vec; | ||
|
||
pub use futures_io::{AsyncRead, AsyncWrite, AsyncSeek, IoVec, SeekFrom}; | ||
pub use futures_io::{ | ||
AsyncRead, AsyncWrite, AsyncSeek, AsyncBufRead, IoVec, SeekFrom, | ||
}; | ||
|
||
#[cfg(feature = "io-compat")] use crate::compat::Compat; | ||
|
||
// Temporarily removed until AsyncBufRead is implemented | ||
// pub use io::lines::{lines, Lines}; | ||
// pub use io::read_until::{read_until, ReadUntil}; | ||
// mod lines; | ||
// mod read_until; | ||
|
||
mod allow_std; | ||
pub use self::allow_std::AllowStdIo; | ||
|
||
|
@@ -26,15 +20,26 @@ pub use self::copy_into::CopyInto; | |
mod flush; | ||
pub use self::flush::Flush; | ||
|
||
// TODO | ||
// mod lines; | ||
taiki-e marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// pub use self::lines::Lines; | ||
|
||
mod read; | ||
pub use self::read::Read; | ||
|
||
mod read_exact; | ||
pub use self::read_exact::ReadExact; | ||
|
||
// TODO | ||
// mod read_line; | ||
// pub use self::read_line::ReadLine; | ||
|
||
mod read_to_end; | ||
pub use self::read_to_end::ReadToEnd; | ||
|
||
mod read_until; | ||
pub use self::read_until::ReadUntil; | ||
|
||
mod close; | ||
pub use self::close::Close; | ||
|
||
|
@@ -343,3 +348,61 @@ pub trait AsyncSeekExt: AsyncSeek { | |
} | ||
|
||
impl<S: AsyncSeek + ?Sized> AsyncSeekExt for S {} | ||
|
||
/// An extension trait which adds utility methods to `AsyncBufRead` types. | ||
pub trait AsyncBufReadExt: AsyncBufRead { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I imagine we'll also want |
||
/// Creates a future which will read all the bytes associated with this I/O | ||
/// object into `buf` until the delimiter `byte` or EOF is reached. | ||
/// This method is the async equivalent to [`BufRead::read_until`](std::io::BufRead::read_until). | ||
/// | ||
/// This function will read bytes from the underlying stream until the | ||
/// delimiter or EOF is found. Once found, all bytes up to, and including, | ||
/// the delimiter (if found) will be appended to `buf`. | ||
/// | ||
/// The returned future will resolve to the number of bytes read once the read | ||
/// operation is completed. | ||
/// | ||
/// In the case of an error the buffer and the object will be discarded, with | ||
/// the error yielded. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// #![feature(async_await, await_macro)] | ||
/// # futures::executor::block_on(async { | ||
/// use futures::io::AsyncBufReadExt; | ||
/// use std::io::Cursor; | ||
/// | ||
/// let mut cursor = Cursor::new(b"lorem-ipsum"); | ||
/// let mut buf = vec![]; | ||
/// | ||
/// // cursor is at 'l' | ||
/// let num_bytes = await!(cursor.read_until(b'-', &mut buf))?; | ||
/// assert_eq!(num_bytes, 6); | ||
/// assert_eq!(buf, b"lorem-"); | ||
/// buf.clear(); | ||
/// | ||
/// // cursor is at 'i' | ||
/// let num_bytes = await!(cursor.read_until(b'-', &mut buf))?; | ||
/// assert_eq!(num_bytes, 5); | ||
/// assert_eq!(buf, b"ipsum"); | ||
/// buf.clear(); | ||
/// | ||
/// // cursor is at EOF | ||
/// let num_bytes = await!(cursor.read_until(b'-', &mut buf))?; | ||
/// assert_eq!(num_bytes, 0); | ||
/// assert_eq!(buf, b""); | ||
/// # Ok::<(), Box<std::error::Error>>(()) }).unwrap(); | ||
/// ``` | ||
fn read_until<'a>( | ||
&'a mut self, | ||
byte: u8, | ||
buf: &'a mut Vec<u8>, | ||
) -> ReadUntil<'a, Self> | ||
where Self: Unpin, | ||
{ | ||
ReadUntil::new(self, byte, buf) | ||
} | ||
} | ||
|
||
impl<R: AsyncBufRead + ?Sized> AsyncBufReadExt for R {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.