Skip to content

Stabilize most stream method and remove unnecessary macros #719

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 10 commits into from
Mar 6, 2020
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ default = [
"async-task",
"crossbeam-channel",
"crossbeam-deque",
"futures-timer",
"kv-log-macro",
"log",
"mio",
Expand All @@ -35,13 +34,14 @@ default = [
"pin-project-lite",
]
docs = ["attributes", "unstable", "default"]
unstable = ["std", "broadcaster", "futures-timer"]
unstable = ["std", "broadcaster"]
attributes = ["async-attributes"]
std = [
"alloc",
"crossbeam-utils",
"futures-core/std",
"futures-io",
"futures-timer",
"memchr",
"once_cell",
"pin-utils",
Expand Down
10 changes: 6 additions & 4 deletions examples/a-chat/client.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use futures::select;
use futures::FutureExt;
use std::io::{self, BufRead, BufReader as StdBufReader};

use async_std::{
io::{stdin, BufReader},
io::BufReader,
net::{TcpStream, ToSocketAddrs},
prelude::*,
task,
stream, task,
};

type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
Expand All @@ -20,8 +21,9 @@ async fn try_main(addr: impl ToSocketAddrs) -> Result<()> {
let reader = BufReader::new(reader);
let mut lines_from_server = futures::StreamExt::fuse(reader.lines());

let stdin = BufReader::new(stdin());
let mut lines_from_stdin = futures::StreamExt::fuse(stdin.lines());
let stdin = StdBufReader::new(io::stdin());
let mut lines_from_stdin = stream::from_iter(stdin.lines());

loop {
select! {
line = lines_from_server.next().fuse() => match line {
Expand Down
7 changes: 4 additions & 3 deletions examples/print-file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Prints a file given as an argument to stdout.

use std::env::args;
use std::io::Write;

use async_std::fs::File;
use async_std::io;
Expand All @@ -14,7 +15,7 @@ fn main() -> io::Result<()> {

task::block_on(async {
let mut file = File::open(&path).await?;
let mut stdout = io::stdout();
let mut stdout = std::io::stdout();
let mut buf = vec![0u8; LEN];

loop {
Expand All @@ -23,12 +24,12 @@ fn main() -> io::Result<()> {

// If this is the end of file, clean up and return.
if n == 0 {
stdout.flush().await?;
stdout.flush()?;
return Ok(());
}

// Write the buffer into stdout.
stdout.write_all(&buf[..n]).await?;
stdout.write_all(&buf[..n])?;
}
})
}
12 changes: 6 additions & 6 deletions examples/stdin-echo.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
//! Echoes lines read on stdin to stdout.

use async_std::io;
use async_std::prelude::*;
use async_std::task;
use std::io::Write;

fn main() -> io::Result<()> {
task::block_on(async {
let stdin = io::stdin();
let mut stdout = io::stdout();
let stdin = std::io::stdin();
let mut stdout = std::io::stdout();
let mut line = String::new();

loop {
// Read a line from stdin.
let n = stdin.read_line(&mut line).await?;
let n = stdin.read_line(&mut line)?;

// If this is the end of stdin, return.
if n == 0 {
return Ok(());
}

// Write the line to stdout.
stdout.write_all(line.as_bytes()).await?;
stdout.flush().await?;
stdout.write_all(line.as_bytes())?;
stdout.flush()?;
line.clear();
}
})
Expand Down
4 changes: 2 additions & 2 deletions examples/stdin-timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use async_std::task;
fn main() -> io::Result<()> {
// This async scope times out after 5 seconds.
task::block_on(io::timeout(Duration::from_secs(5), async {
let stdin = io::stdin();
let stdin = std::io::stdin();

// Read a line from the standard input and display it.
let mut line = String::new();
stdin.read_line(&mut line).await?;
stdin.read_line(&mut line)?;
dbg!(line);

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion src/future/into_future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use std::future::Future;
/// # Examples
///
/// ```
/// use std::pin::Pin;
///
/// use async_std::future::{Future, IntoFuture};
/// use async_std::io;
/// use async_std::pin::Pin;
///
/// struct Client;
///
Expand Down
2 changes: 2 additions & 0 deletions src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ cfg_std! {

cfg_default! {
pub use timeout::{timeout, TimeoutError};

mod timeout;
}

cfg_unstable! {
pub use into_future::IntoFuture;
pub(crate) use maybe_done::MaybeDone;

mod into_future;
mod maybe_done;
}
10 changes: 6 additions & 4 deletions src/io/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ use crate::utils::Context as _;
///
/// # Examples
///
/// ```
/// ```no_run
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::io;
/// use async_std::fs::File;
///
/// let mut reader: &[u8] = b"hello";
/// let mut writer = io::stdout();
/// let mut writer = File::open("foo.txt").await?;
///
/// io::copy(&mut reader, &mut writer).await?;
/// #
Expand Down Expand Up @@ -119,13 +120,14 @@ where
///
/// # Examples
///
/// ```
/// ```no_run
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::io;
/// use async_std::fs::File;
///
/// let mut reader: &[u8] = b"hello";
/// let mut writer = io::stdout();
/// let mut writer = File::open("foo.txt").await?;
///
/// io::copy(&mut reader, &mut writer).await?;
/// #
Expand Down
84 changes: 10 additions & 74 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,56 +122,6 @@
//! # Ok(()) }) }
//! ```
//!
//! ## Standard input and output
//!
//! A very common source of input is standard input:
//!
//! ```no_run
//! use async_std::io;
//!
//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
//! #
//! let mut input = String::new();
//!
//! io::stdin().read_line(&mut input).await?;
//!
//! println!("You typed: {}", input.trim());
//! #
//! # Ok(()) }) }
//! ```
//!
//! Note that you cannot use the [`?` operator] in functions that do not return
//! a [`Result<T, E>`][`Result`]. Instead, you can call [`.unwrap()`]
//! or `match` on the return value to catch any possible errors:
//!
//! ```no_run
//! use async_std::io;
//!
//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
//! #
//! let mut input = String::new();
//!
//! io::stdin().read_line(&mut input).await.unwrap();
//! #
//! # Ok(()) }) }
//! ```
//!
//! And a very common source of output is standard output:
//!
//! ```no_run
//! use async_std::io;
//! use async_std::io::prelude::*;
//!
//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
//! #
//! io::stdout().write(&[42]).await?;
//! #
//! # Ok(()) }) }
//! ```
//!
//! Of course, using [`io::stdout`] directly is less common than something like
//! [`println!`].
//!
//! ## Iterator types
//!
//! A large number of the structures provided by `std::io` are for various
Expand Down Expand Up @@ -204,10 +154,14 @@
//!
//! ```no_run
//! use async_std::io;
//! use async_std::fs::File;
//!
//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
//! #
//! io::copy(&mut io::stdin(), &mut io::stdout()).await?;
//! let mut reader: &[u8] = b"hello";
//! let mut writer = File::open("foo.txt").await?;
//!
//! io::copy(&mut reader, &mut writer).await?;
//! #
//! # Ok(()) }) }
//! ```
Expand All @@ -224,13 +178,14 @@
//! ```
//! #![allow(dead_code)]
//! use async_std::io;
//! use std::time::Duration;
//!
//! async fn read_input() -> io::Result<()> {
//! let mut input = String::new();
//!
//! io::stdin().read_line(&mut input).await?;
//! let f = io::timeout(Duration::from_secs(5), async {
//! Ok(())
//! });
//!
//! println!("You typed: {}", input.trim());
//! assert_eq!(f.await?, ());
//!
//! Ok(())
//! }
Expand Down Expand Up @@ -260,8 +215,6 @@
//! [`BufReader`]: struct.BufReader.html
//! [`BufWriter`]: struct.BufWriter.html
//! [`Write::write`]: trait.Write.html#tymethod.write
//! [`io::stdout`]: fn.stdout.html
//! [`println!`]: ../macro.println.html
//! [`Lines`]: struct.Lines.html
//! [`io::Result`]: type.Result.html
//! [`?` operator]: https://doc.rust-lang.org/stable/book/appendix-02-operators.html
Expand Down Expand Up @@ -305,24 +258,7 @@ cfg_std! {
}

cfg_default! {
// For use in the print macros.
#[doc(hidden)]
pub use stdio::{_eprint, _print};

pub use stderr::{stderr, Stderr};
pub use stdin::{stdin, Stdin};
pub use stdout::{stdout, Stdout};
pub use timeout::timeout;

mod timeout;
mod stderr;
mod stdin;
mod stdio;
mod stdout;
}

cfg_unstable_default! {
pub use stderr::StderrLock;
pub use stdin::StdinLock;
pub use stdout::StdoutLock;
}
Loading