This commit is contained in:
dignifiedquire 2019-09-27 18:49:23 -06:00
parent a1aa3f823d
commit 064b44f695
4 changed files with 112 additions and 113 deletions

View file

@ -4,7 +4,7 @@ use crate::io::{self, Read};
use crate::stream::stream::Stream; use crate::stream::stream::Stream;
use crate::task::{Context, Poll}; use crate::task::{Context, Poll};
/// An iterator over `u8` values of a reader. /// A stream over `u8` values of a reader.
/// ///
/// This struct is generally created by calling [`bytes`] on a reader. /// This struct is generally created by calling [`bytes`] on a reader.
/// Please see the documentation of [`bytes`] for more details. /// Please see the documentation of [`bytes`] for more details.

View file

@ -23,18 +23,18 @@ impl<T, U> Chain<T, U> {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// use async_std::io; /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*; /// use async_std::prelude::*;
/// use async_std::fs::File; /// use async_std::fs::File;
/// ///
/// fn main() -> io::Result<()> { async_std::task::block_on(async { /// let foo_file = File::open("foo.txt").await?;
/// let foo_file = File::open("foo.txt").await?; /// let bar_file = File::open("bar.txt").await?;
/// let bar_file = File::open("bar.txt").await?;
/// ///
/// let chain = foo_file.chain(bar_file); /// let chain = foo_file.chain(bar_file);
/// let (foo_file, bar_file) = chain.into_inner(); /// let (foo_file, bar_file) = chain.into_inner();
/// Ok(()) /// #
/// }) } /// # Ok(()) }) }
/// ``` /// ```
pub fn into_inner(self) -> (T, U) { pub fn into_inner(self) -> (T, U) {
(self.first, self.second) (self.first, self.second)
@ -45,18 +45,18 @@ impl<T, U> Chain<T, U> {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// use async_std::io; /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*; /// use async_std::prelude::*;
/// use async_std::fs::File; /// use async_std::fs::File;
/// ///
/// fn main() -> io::Result<()> { async_std::task::block_on(async { /// let foo_file = File::open("foo.txt").await?;
/// let foo_file = File::open("foo.txt").await?; /// let bar_file = File::open("bar.txt").await?;
/// let bar_file = File::open("bar.txt").await?;
/// ///
/// let chain = foo_file.chain(bar_file); /// let chain = foo_file.chain(bar_file);
/// let (foo_file, bar_file) = chain.get_ref(); /// let (foo_file, bar_file) = chain.get_ref();
/// Ok(()) /// #
/// }) } /// # Ok(()) }) }
/// ``` /// ```
pub fn get_ref(&self) -> (&T, &U) { pub fn get_ref(&self) -> (&T, &U) {
(&self.first, &self.second) (&self.first, &self.second)
@ -71,18 +71,18 @@ impl<T, U> Chain<T, U> {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// use async_std::io; /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*; /// use async_std::prelude::*;
/// use async_std::fs::File; /// use async_std::fs::File;
/// ///
/// fn main() -> io::Result<()> { async_std::task::block_on(async { /// let foo_file = File::open("foo.txt").await?;
/// let foo_file = File::open("foo.txt").await?; /// let bar_file = File::open("bar.txt").await?;
/// let bar_file = File::open("bar.txt").await?;
/// ///
/// let mut chain = foo_file.chain(bar_file); /// let mut chain = foo_file.chain(bar_file);
/// let (foo_file, bar_file) = chain.get_mut(); /// let (foo_file, bar_file) = chain.get_mut();
/// Ok(()) /// #
/// }) } /// # Ok(()) }) }
/// ``` /// ```
pub fn get_mut(&mut self) -> (&mut T, &mut U) { pub fn get_mut(&mut self) -> (&mut T, &mut U) {
(&mut self.first, &mut self.second) (&mut self.first, &mut self.second)

View file

@ -282,21 +282,20 @@ extension_trait! {
[`read()`]: tymethod.read [`read()`]: tymethod.read
```no_run ```no_run
# fn main() -> std::io::Result<()> { async_std::task::block_on(async {
#
use async_std::io::prelude::*; use async_std::io::prelude::*;
use async_std::fs::File; use async_std::fs::File;
fn main() -> std::io::Result<()> { let f = File::open("foo.txt").await?;
async_std::task::block_on(async { let mut buffer = [0; 5];
let f = File::open("foo.txt").await?;
let mut buffer = [0; 5];
// read at most five bytes // read at most five bytes
let mut handle = f.take(5); let mut handle = f.take(5);
handle.read(&mut buffer).await?; handle.read(&mut buffer).await?;
Ok(()) #
}) # Ok(()) }) }
}
``` ```
"#] "#]
fn take(self, limit: u64) -> take::Take<Self> fn take(self, limit: u64) -> take::Take<Self>
@ -319,27 +318,27 @@ extension_trait! {
[file]: ../fs/struct.File.html [file]: ../fs/struct.File.html
```no_run ```no_run
use async_std::io; # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
#
use async_std::prelude::*; use async_std::prelude::*;
use async_std::fs::File; use async_std::fs::File;
fn main() -> io::Result<()> { async_std::task::block_on(async { let mut f = File::open("foo.txt").await?;
let mut f = File::open("foo.txt").await?; let mut buffer = Vec::new();
let mut buffer = Vec::new(); let mut other_buffer = Vec::new();
let mut other_buffer = Vec::new();
{ {
let reference = f.by_ref(); let reference = f.by_ref();
// read at most 5 bytes // read at most 5 bytes
reference.take(5).read_to_end(&mut buffer).await?; reference.take(5).read_to_end(&mut buffer).await?;
} // drop our &mut reference so we can use f again } // drop our &mut reference so we can use f again
// original file still usable, read the rest // original file still usable, read the rest
f.read_to_end(&mut other_buffer).await?; f.read_to_end(&mut other_buffer).await?;
Ok(()) #
}) } # Ok(()) }) }
``` ```
"#] "#]
fn by_ref(&mut self) -> &mut Self where Self: Sized { self } fn by_ref(&mut self) -> &mut Self where Self: Sized { self }
@ -360,19 +359,19 @@ extension_trait! {
[file]: ../fs/struct.File.html [file]: ../fs/struct.File.html
```no_run ```no_run
use async_std::io; # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
#
use async_std::prelude::*; use async_std::prelude::*;
use async_std::fs::File; use async_std::fs::File;
fn main() -> io::Result<()> { async_std::task::block_on(async { let f = File::open("foo.txt").await?;
let f = File::open("foo.txt").await?; let mut s = f.bytes();
let mut s = f.bytes();
while let Some(byte) = s.next().await { while let Some(byte) = s.next().await {
println!("{}", byte.unwrap()); println!("{}", byte.unwrap());
} }
Ok(()) #
}) } # Ok(()) }) }
``` ```
"#] "#]
fn bytes(self) -> bytes::Bytes<Self> where Self: Sized { fn bytes(self) -> bytes::Bytes<Self> where Self: Sized {
@ -393,22 +392,22 @@ extension_trait! {
[file]: ../fs/struct.File.html [file]: ../fs/struct.File.html
```no_run ```no_run
use async_std::io; # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
#
use async_std::prelude::*; use async_std::prelude::*;
use async_std::fs::File; use async_std::fs::File;
fn main() -> io::Result<()> { async_std::task::block_on(async { let f1 = File::open("foo.txt").await?;
let f1 = File::open("foo.txt").await?; let f2 = File::open("bar.txt").await?;
let f2 = File::open("bar.txt").await?;
let mut handle = f1.chain(f2); let mut handle = f1.chain(f2);
let mut buffer = String::new(); let mut buffer = String::new();
// read the value into a String. We could use any Read method here, // read the value into a String. We could use any Read method here,
// this is just one example. // this is just one example.
handle.read_to_string(&mut buffer).await?; handle.read_to_string(&mut buffer).await?;
Ok(()) #
}) } # Ok(()) }) }
``` ```
"#] "#]
fn chain<R: Read>(self, next: R) -> chain::Chain<Self, R> where Self: Sized { fn chain<R: Read>(self, next: R) -> chain::Chain<Self, R> where Self: Sized {

View file

@ -30,19 +30,19 @@ impl<T> Take<T> {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// use async_std::io; /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*; /// use async_std::prelude::*;
/// use async_std::fs::File; /// use async_std::fs::File;
/// ///
/// fn main() -> io::Result<()> { async_std::task::block_on(async { /// let f = File::open("foo.txt").await?;
/// let f = File::open("foo.txt").await?;
/// ///
/// // read at most five bytes /// // read at most five bytes
/// let handle = f.take(5); /// let handle = f.take(5);
/// ///
/// println!("limit: {}", handle.limit()); /// println!("limit: {}", handle.limit());
/// Ok(()) /// #
/// }) } /// # Ok(()) }) }
/// ``` /// ```
pub fn limit(&self) -> u64 { pub fn limit(&self) -> u64 {
self.limit self.limit
@ -56,20 +56,20 @@ impl<T> Take<T> {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// use async_std::io; /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*; /// use async_std::prelude::*;
/// use async_std::fs::File; /// use async_std::fs::File;
/// ///
/// fn main() -> io::Result<()> { async_std::task::block_on(async { /// let f = File::open("foo.txt").await?;
/// let f = File::open("foo.txt").await?;
/// ///
/// // read at most five bytes /// // read at most five bytes
/// let mut handle = f.take(5); /// let mut handle = f.take(5);
/// handle.set_limit(10); /// handle.set_limit(10);
/// ///
/// assert_eq!(handle.limit(), 10); /// assert_eq!(handle.limit(), 10);
/// Ok(()) /// #
/// }) } /// # Ok(()) }) }
/// ``` /// ```
pub fn set_limit(&mut self, limit: u64) { pub fn set_limit(&mut self, limit: u64) {
self.limit = limit; self.limit = limit;
@ -80,20 +80,20 @@ impl<T> Take<T> {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// use async_std::io; /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*; /// use async_std::prelude::*;
/// use async_std::fs::File; /// use async_std::fs::File;
/// ///
/// fn main() -> io::Result<()> { async_std::task::block_on(async { /// let file = File::open("foo.txt").await?;
/// let file = File::open("foo.txt").await?;
/// ///
/// let mut buffer = [0; 5]; /// let mut buffer = [0; 5];
/// let mut handle = file.take(5); /// let mut handle = file.take(5);
/// handle.read(&mut buffer).await?; /// handle.read(&mut buffer).await?;
/// ///
/// let file = handle.into_inner(); /// let file = handle.into_inner();
/// Ok(()) /// #
/// }) } /// # Ok(()) }) }
/// ``` /// ```
pub fn into_inner(self) -> T { pub fn into_inner(self) -> T {
self.inner self.inner
@ -104,20 +104,20 @@ impl<T> Take<T> {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// use async_std::io; /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*; /// use async_std::prelude::*;
/// use async_std::fs::File; /// use async_std::fs::File;
/// ///
/// fn main() -> io::Result<()> { async_std::task::block_on(async { /// let file = File::open("foo.txt").await?;
/// let file = File::open("foo.txt").await?;
/// ///
/// let mut buffer = [0; 5]; /// let mut buffer = [0; 5];
/// let mut handle = file.take(5); /// let mut handle = file.take(5);
/// handle.read(&mut buffer).await?; /// handle.read(&mut buffer).await?;
/// ///
/// let file = handle.get_ref(); /// let file = handle.get_ref();
/// Ok(()) /// #
/// }) } /// # Ok(()) }) }
/// ``` /// ```
pub fn get_ref(&self) -> &T { pub fn get_ref(&self) -> &T {
&self.inner &self.inner
@ -132,20 +132,20 @@ impl<T> Take<T> {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// use async_std::io; /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::prelude::*; /// use async_std::prelude::*;
/// use async_std::fs::File; /// use async_std::fs::File;
/// ///
/// fn main() -> io::Result<()> { async_std::task::block_on(async { /// let file = File::open("foo.txt").await?;
/// let file = File::open("foo.txt").await?;
/// ///
/// let mut buffer = [0; 5]; /// let mut buffer = [0; 5];
/// let mut handle = file.take(5); /// let mut handle = file.take(5);
/// handle.read(&mut buffer).await?; /// handle.read(&mut buffer).await?;
/// ///
/// let file = handle.get_mut(); /// let file = handle.get_mut();
/// Ok(()) /// #
/// }) } /// # Ok(()) }) }
/// ``` /// ```
pub fn get_mut(&mut self) -> &mut T { pub fn get_mut(&mut self) -> &mut T {
&mut self.inner &mut self.inner