-
Notifications
You must be signed in to change notification settings - Fork 340
feat: missing Read and Write methods #245
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
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
75dc819
feat(io): implement Read::take
dignifiedquire f751ebb
feat(io): implement Read::by_ref
dignifiedquire e681e29
feat(io): implement Read::bytes
dignifiedquire d9aec10
feat(io): implement Read::chain
dignifiedquire dc6c8fb
feat(io): add stub for BufRead for Take
dignifiedquire a1aa3f8
finish BufRead
dignifiedquire 064b44f
apply cr
dignifiedquire 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::pin::Pin; | ||
|
||
use crate::io::{self, Read}; | ||
use crate::stream::stream::Stream; | ||
use crate::task::{Context, Poll}; | ||
|
||
/// An iterator over `u8` values of a reader. | ||
/// | ||
/// This struct is generally created by calling [`bytes`] on a reader. | ||
/// Please see the documentation of [`bytes`] for more details. | ||
/// | ||
/// [`bytes`]: trait.Read.html#method.bytes | ||
#[derive(Debug)] | ||
pub struct Bytes<T> { | ||
pub(crate) inner: T, | ||
} | ||
|
||
impl<T: Read + Unpin> Stream for Bytes<T> { | ||
type Item = io::Result<u8>; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let mut byte = 0; | ||
|
||
let rd = Pin::new(&mut self.inner); | ||
|
||
match futures_core::ready!(rd.poll_read(cx, std::slice::from_mut(&mut byte))) { | ||
Ok(0) => Poll::Ready(None), | ||
Ok(..) => Poll::Ready(Some(Ok(byte))), | ||
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => Poll::Pending, | ||
Err(e) => Poll::Ready(Some(Err(e))), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::io; | ||
use crate::prelude::*; | ||
use crate::task; | ||
|
||
#[test] | ||
fn test_bytes_basics() -> std::io::Result<()> { | ||
task::block_on(async move { | ||
let raw: Vec<u8> = vec![0, 1, 2, 3, 4, 5, 6, 7, 8]; | ||
let source: io::Cursor<Vec<u8>> = io::Cursor::new(raw.clone()); | ||
|
||
let mut s = source.bytes(); | ||
|
||
// TODO(@dignifiedquire): Use collect, once it is stable. | ||
let mut result = Vec::new(); | ||
while let Some(byte) = s.next().await { | ||
result.push(byte?); | ||
} | ||
|
||
assert_eq!(result, raw); | ||
|
||
Ok(()) | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,197 @@ | ||
use crate::io::IoSliceMut; | ||
use std::fmt; | ||
use std::pin::Pin; | ||
|
||
use crate::io::{self, BufRead, Read}; | ||
use crate::task::{Context, Poll}; | ||
|
||
/// Adaptor to chain together two readers. | ||
/// | ||
/// This struct is generally created by calling [`chain`] on a reader. | ||
/// Please see the documentation of [`chain`] for more details. | ||
/// | ||
/// [`chain`]: trait.Read.html#method.chain | ||
pub struct Chain<T, U> { | ||
pub(crate) first: T, | ||
pub(crate) second: U, | ||
pub(crate) done_first: bool, | ||
} | ||
|
||
impl<T, U> Chain<T, U> { | ||
/// Consumes the `Chain`, returning the wrapped readers. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// use async_std::io; | ||
/// use async_std::prelude::*; | ||
/// use async_std::fs::File; | ||
/// | ||
/// fn main() -> io::Result<()> { async_std::task::block_on(async { | ||
/// let foo_file = File::open("foo.txt").await?; | ||
/// let bar_file = File::open("bar.txt").await?; | ||
/// | ||
/// let chain = foo_file.chain(bar_file); | ||
/// let (foo_file, bar_file) = chain.into_inner(); | ||
/// Ok(()) | ||
/// }) } | ||
/// ``` | ||
pub fn into_inner(self) -> (T, U) { | ||
(self.first, self.second) | ||
} | ||
|
||
/// Gets references to the underlying readers in this `Chain`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// use async_std::io; | ||
/// use async_std::prelude::*; | ||
/// use async_std::fs::File; | ||
/// | ||
/// fn main() -> io::Result<()> { async_std::task::block_on(async { | ||
/// let foo_file = File::open("foo.txt").await?; | ||
/// let bar_file = File::open("bar.txt").await?; | ||
/// | ||
/// let chain = foo_file.chain(bar_file); | ||
/// let (foo_file, bar_file) = chain.get_ref(); | ||
/// Ok(()) | ||
/// }) } | ||
/// ``` | ||
pub fn get_ref(&self) -> (&T, &U) { | ||
(&self.first, &self.second) | ||
} | ||
|
||
/// Gets mutable references to the underlying readers in this `Chain`. | ||
/// | ||
/// Care should be taken to avoid modifying the internal I/O state of the | ||
/// underlying readers as doing so may corrupt the internal state of this | ||
/// `Chain`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// use async_std::io; | ||
/// use async_std::prelude::*; | ||
/// use async_std::fs::File; | ||
/// | ||
/// fn main() -> io::Result<()> { async_std::task::block_on(async { | ||
/// let foo_file = File::open("foo.txt").await?; | ||
/// let bar_file = File::open("bar.txt").await?; | ||
/// | ||
/// let mut chain = foo_file.chain(bar_file); | ||
/// let (foo_file, bar_file) = chain.get_mut(); | ||
/// Ok(()) | ||
/// }) } | ||
/// ``` | ||
pub fn get_mut(&mut self) -> (&mut T, &mut U) { | ||
(&mut self.first, &mut self.second) | ||
} | ||
} | ||
|
||
impl<T: fmt::Debug, U: fmt::Debug> fmt::Debug for Chain<T, U> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("Chain") | ||
.field("t", &self.first) | ||
.field("u", &self.second) | ||
.finish() | ||
} | ||
} | ||
|
||
impl<T: Read + Unpin, U: Read + Unpin> Read for Chain<T, U> { | ||
fn poll_read( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &mut [u8], | ||
) -> Poll<io::Result<usize>> { | ||
if !self.done_first { | ||
let rd = Pin::new(&mut self.first); | ||
|
||
match futures_core::ready!(rd.poll_read(cx, buf)) { | ||
Ok(0) if !buf.is_empty() => self.done_first = true, | ||
Ok(n) => return Poll::Ready(Ok(n)), | ||
Err(err) => return Poll::Ready(Err(err)), | ||
} | ||
} | ||
|
||
let rd = Pin::new(&mut self.second); | ||
rd.poll_read(cx, buf) | ||
} | ||
|
||
fn poll_read_vectored( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
bufs: &mut [IoSliceMut<'_>], | ||
) -> Poll<io::Result<usize>> { | ||
if !self.done_first { | ||
let rd = Pin::new(&mut self.first); | ||
|
||
match futures_core::ready!(rd.poll_read_vectored(cx, bufs)) { | ||
Ok(0) if !bufs.is_empty() => self.done_first = true, | ||
Ok(n) => return Poll::Ready(Ok(n)), | ||
Err(err) => return Poll::Ready(Err(err)), | ||
} | ||
} | ||
|
||
let rd = Pin::new(&mut self.second); | ||
rd.poll_read_vectored(cx, bufs) | ||
} | ||
} | ||
|
||
impl<T: BufRead + Unpin, U: BufRead + Unpin> BufRead for Chain<T, U> { | ||
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> { | ||
let Self { | ||
first, | ||
second, | ||
done_first, | ||
} = unsafe { self.get_unchecked_mut() }; | ||
|
||
if !*done_first { | ||
let first = unsafe { Pin::new_unchecked(first) }; | ||
match futures_core::ready!(first.poll_fill_buf(cx)) { | ||
Ok(buf) if buf.is_empty() => { | ||
*done_first = true; | ||
} | ||
Ok(buf) => return Poll::Ready(Ok(buf)), | ||
Err(err) => return Poll::Ready(Err(err)), | ||
} | ||
} | ||
|
||
let second = unsafe { Pin::new_unchecked(second) }; | ||
second.poll_fill_buf(cx) | ||
} | ||
|
||
fn consume(mut self: Pin<&mut Self>, amt: usize) { | ||
if !self.done_first { | ||
let rd = Pin::new(&mut self.first); | ||
rd.consume(amt) | ||
} else { | ||
let rd = Pin::new(&mut self.second); | ||
rd.consume(amt) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::io; | ||
use crate::prelude::*; | ||
use crate::task; | ||
|
||
#[test] | ||
fn test_chain_basics() -> std::io::Result<()> { | ||
let source1: io::Cursor<Vec<u8>> = io::Cursor::new(vec![0, 1, 2]); | ||
let source2: io::Cursor<Vec<u8>> = io::Cursor::new(vec![3, 4, 5]); | ||
|
||
task::block_on(async move { | ||
let mut buffer = Vec::new(); | ||
|
||
let mut source = source1.chain(source2); | ||
|
||
assert_eq!(6, source.read_to_end(&mut buffer).await?); | ||
assert_eq!(buffer, vec![0, 1, 2, 3, 4, 5]); | ||
|
||
Ok(()) | ||
}) | ||
} | ||
} |
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.