Skip to content

Commit cc19592

Browse files
authored
Revert "Stabilize most stream method and remove unnecessary macros"
1 parent 61f9483 commit cc19592

34 files changed

+1192
-110
lines changed

Diff for: Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ default = [
2626
"async-task",
2727
"crossbeam-channel",
2828
"crossbeam-deque",
29+
"futures-timer",
2930
"kv-log-macro",
3031
"log",
3132
"mio",
@@ -34,14 +35,13 @@ default = [
3435
"pin-project-lite",
3536
]
3637
docs = ["attributes", "unstable", "default"]
37-
unstable = ["std", "broadcaster"]
38+
unstable = ["std", "broadcaster", "futures-timer"]
3839
attributes = ["async-attributes"]
3940
std = [
4041
"alloc",
4142
"crossbeam-utils",
4243
"futures-core/std",
4344
"futures-io",
44-
"futures-timer",
4545
"memchr",
4646
"once_cell",
4747
"pin-utils",

Diff for: examples/a-chat/client.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use futures::select;
22
use futures::FutureExt;
3-
use std::io::{self, BufRead, BufReader as StdBufReader};
43

54
use async_std::{
6-
io::BufReader,
5+
io::{stdin, BufReader},
76
net::{TcpStream, ToSocketAddrs},
87
prelude::*,
9-
stream, task,
8+
task,
109
};
1110

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

24-
let stdin = StdBufReader::new(io::stdin());
25-
let mut lines_from_stdin = stream::from_iter(stdin.lines());
26-
23+
let stdin = BufReader::new(stdin());
24+
let mut lines_from_stdin = futures::StreamExt::fuse(stdin.lines());
2725
loop {
2826
select! {
2927
line = lines_from_server.next().fuse() => match line {

Diff for: examples/print-file.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Prints a file given as an argument to stdout.
22
33
use std::env::args;
4-
use std::io::Write;
54

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

1615
task::block_on(async {
1716
let mut file = File::open(&path).await?;
18-
let mut stdout = std::io::stdout();
17+
let mut stdout = io::stdout();
1918
let mut buf = vec![0u8; LEN];
2019

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

2524
// If this is the end of file, clean up and return.
2625
if n == 0 {
27-
stdout.flush()?;
26+
stdout.flush().await?;
2827
return Ok(());
2928
}
3029

3130
// Write the buffer into stdout.
32-
stdout.write_all(&buf[..n])?;
31+
stdout.write_all(&buf[..n]).await?;
3332
}
3433
})
3534
}

Diff for: examples/stdin-echo.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
//! Echoes lines read on stdin to stdout.
22
33
use async_std::io;
4+
use async_std::prelude::*;
45
use async_std::task;
5-
use std::io::Write;
66

77
fn main() -> io::Result<()> {
88
task::block_on(async {
9-
let stdin = std::io::stdin();
10-
let mut stdout = std::io::stdout();
9+
let stdin = io::stdin();
10+
let mut stdout = io::stdout();
1111
let mut line = String::new();
1212

1313
loop {
1414
// Read a line from stdin.
15-
let n = stdin.read_line(&mut line)?;
15+
let n = stdin.read_line(&mut line).await?;
1616

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

2222
// Write the line to stdout.
23-
stdout.write_all(line.as_bytes())?;
24-
stdout.flush()?;
23+
stdout.write_all(line.as_bytes()).await?;
24+
stdout.flush().await?;
2525
line.clear();
2626
}
2727
})

Diff for: examples/stdin-timeout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use async_std::task;
88
fn main() -> io::Result<()> {
99
// This async scope times out after 5 seconds.
1010
task::block_on(io::timeout(Duration::from_secs(5), async {
11-
let stdin = std::io::stdin();
11+
let stdin = io::stdin();
1212

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

1818
Ok(())

Diff for: src/future/into_future.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ use std::future::Future;
55
/// # Examples
66
///
77
/// ```
8-
/// use std::pin::Pin;
9-
///
108
/// use async_std::future::{Future, IntoFuture};
119
/// use async_std::io;
10+
/// use async_std::pin::Pin;
1211
///
1312
/// struct Client;
1413
///

Diff for: src/future/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,12 @@ cfg_std! {
6363

6464
cfg_default! {
6565
pub use timeout::{timeout, TimeoutError};
66-
6766
mod timeout;
6867
}
6968

7069
cfg_unstable! {
7170
pub use into_future::IntoFuture;
7271
pub(crate) use maybe_done::MaybeDone;
73-
7472
mod into_future;
7573
mod maybe_done;
7674
}

Diff for: src/io/copy.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ use crate::utils::Context as _;
3232
///
3333
/// # Examples
3434
///
35-
/// ```no_run
35+
/// ```
3636
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
3737
/// #
3838
/// use async_std::io;
39-
/// use async_std::fs::File;
4039
///
4140
/// let mut reader: &[u8] = b"hello";
42-
/// let mut writer = File::open("foo.txt").await?;
41+
/// let mut writer = io::stdout();
4342
///
4443
/// io::copy(&mut reader, &mut writer).await?;
4544
/// #
@@ -120,14 +119,13 @@ where
120119
///
121120
/// # Examples
122121
///
123-
/// ```no_run
122+
/// ```
124123
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
125124
/// #
126125
/// use async_std::io;
127-
/// use async_std::fs::File;
128126
///
129127
/// let mut reader: &[u8] = b"hello";
130-
/// let mut writer = File::open("foo.txt").await?;
128+
/// let mut writer = io::stdout();
131129
///
132130
/// io::copy(&mut reader, &mut writer).await?;
133131
/// #

Diff for: src/io/mod.rs

+74-10
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,56 @@
122122
//! # Ok(()) }) }
123123
//! ```
124124
//!
125+
//! ## Standard input and output
126+
//!
127+
//! A very common source of input is standard input:
128+
//!
129+
//! ```no_run
130+
//! use async_std::io;
131+
//!
132+
//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
133+
//! #
134+
//! let mut input = String::new();
135+
//!
136+
//! io::stdin().read_line(&mut input).await?;
137+
//!
138+
//! println!("You typed: {}", input.trim());
139+
//! #
140+
//! # Ok(()) }) }
141+
//! ```
142+
//!
143+
//! Note that you cannot use the [`?` operator] in functions that do not return
144+
//! a [`Result<T, E>`][`Result`]. Instead, you can call [`.unwrap()`]
145+
//! or `match` on the return value to catch any possible errors:
146+
//!
147+
//! ```no_run
148+
//! use async_std::io;
149+
//!
150+
//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
151+
//! #
152+
//! let mut input = String::new();
153+
//!
154+
//! io::stdin().read_line(&mut input).await.unwrap();
155+
//! #
156+
//! # Ok(()) }) }
157+
//! ```
158+
//!
159+
//! And a very common source of output is standard output:
160+
//!
161+
//! ```no_run
162+
//! use async_std::io;
163+
//! use async_std::io::prelude::*;
164+
//!
165+
//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
166+
//! #
167+
//! io::stdout().write(&[42]).await?;
168+
//! #
169+
//! # Ok(()) }) }
170+
//! ```
171+
//!
172+
//! Of course, using [`io::stdout`] directly is less common than something like
173+
//! [`println!`].
174+
//!
125175
//! ## Iterator types
126176
//!
127177
//! A large number of the structures provided by `std::io` are for various
@@ -154,14 +204,10 @@
154204
//!
155205
//! ```no_run
156206
//! use async_std::io;
157-
//! use async_std::fs::File;
158207
//!
159208
//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
160209
//! #
161-
//! let mut reader: &[u8] = b"hello";
162-
//! let mut writer = File::open("foo.txt").await?;
163-
//!
164-
//! io::copy(&mut reader, &mut writer).await?;
210+
//! io::copy(&mut io::stdin(), &mut io::stdout()).await?;
165211
//! #
166212
//! # Ok(()) }) }
167213
//! ```
@@ -178,14 +224,13 @@
178224
//! ```
179225
//! #![allow(dead_code)]
180226
//! use async_std::io;
181-
//! use std::time::Duration;
182227
//!
183228
//! async fn read_input() -> io::Result<()> {
184-
//! let f = io::timeout(Duration::from_secs(5), async {
185-
//! Ok(())
186-
//! });
229+
//! let mut input = String::new();
230+
//!
231+
//! io::stdin().read_line(&mut input).await?;
187232
//!
188-
//! assert_eq!(f.await?, ());
233+
//! println!("You typed: {}", input.trim());
189234
//!
190235
//! Ok(())
191236
//! }
@@ -215,6 +260,8 @@
215260
//! [`BufReader`]: struct.BufReader.html
216261
//! [`BufWriter`]: struct.BufWriter.html
217262
//! [`Write::write`]: trait.Write.html#tymethod.write
263+
//! [`io::stdout`]: fn.stdout.html
264+
//! [`println!`]: ../macro.println.html
218265
//! [`Lines`]: struct.Lines.html
219266
//! [`io::Result`]: type.Result.html
220267
//! [`?` operator]: https://doc.rust-lang.org/stable/book/appendix-02-operators.html
@@ -258,7 +305,24 @@ cfg_std! {
258305
}
259306

260307
cfg_default! {
308+
// For use in the print macros.
309+
#[doc(hidden)]
310+
pub use stdio::{_eprint, _print};
311+
312+
pub use stderr::{stderr, Stderr};
313+
pub use stdin::{stdin, Stdin};
314+
pub use stdout::{stdout, Stdout};
261315
pub use timeout::timeout;
262316

263317
mod timeout;
318+
mod stderr;
319+
mod stdin;
320+
mod stdio;
321+
mod stdout;
322+
}
323+
324+
cfg_unstable_default! {
325+
pub use stderr::StderrLock;
326+
pub use stdin::StdinLock;
327+
pub use stdout::StdoutLock;
264328
}

0 commit comments

Comments
 (0)