Skip to content

Commit 9b3e8b8

Browse files
author
Stjepan Glavina
committed
Use our own Sink/Empty and fix compilation errors
1 parent cefdf51 commit 9b3e8b8

File tree

10 files changed

+170
-40
lines changed

10 files changed

+170
-40
lines changed

src/fs/file.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::sync::Mutex;
88

99
use cfg_if::cfg_if;
1010
use futures::future::{self, FutureExt, TryFutureExt};
11-
use futures::io::{AsyncSeek, Initializer};
11+
use futures::io::{AsyncRead, AsyncSeek, AsyncWrite, Initializer};
1212

1313
use crate::future::Future;
1414
use crate::io;
@@ -482,7 +482,7 @@ impl File {
482482
}
483483
}
484484

485-
impl futures::io::AsyncRead for File {
485+
impl AsyncRead for File {
486486
fn poll_read(
487487
self: Pin<&mut Self>,
488488
cx: &mut Context<'_>,
@@ -497,7 +497,7 @@ impl futures::io::AsyncRead for File {
497497
}
498498
}
499499

500-
impl futures::io::AsyncRead for &File {
500+
impl AsyncRead for &File {
501501
fn poll_read(
502502
mut self: Pin<&mut Self>,
503503
cx: &mut Context<'_>,
@@ -563,7 +563,7 @@ impl futures::io::AsyncRead for &File {
563563
}
564564
}
565565

566-
impl futures::io::AsyncWrite for File {
566+
impl AsyncWrite for File {
567567
fn poll_write(
568568
self: Pin<&mut Self>,
569569
cx: &mut Context<'_>,
@@ -581,7 +581,7 @@ impl futures::io::AsyncWrite for File {
581581
}
582582
}
583583

584-
impl futures::io::AsyncWrite for &File {
584+
impl AsyncWrite for &File {
585585
fn poll_write(
586586
mut self: Pin<&mut Self>,
587587
cx: &mut Context<'_>,
@@ -692,7 +692,7 @@ impl futures::io::AsyncWrite for &File {
692692
}
693693
}
694694

695-
impl futures::io::AsyncSeek for File {
695+
impl AsyncSeek for File {
696696
fn poll_seek(
697697
self: Pin<&mut Self>,
698698
cx: &mut Context<'_>,

src/io/empty.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use std::fmt;
2+
use std::pin::Pin;
3+
4+
use futures::io::{AsyncBufRead, AsyncRead, Initializer};
5+
6+
use crate::io;
7+
use crate::task::{Context, Poll};
8+
9+
/// Creates a reader that contains no data.
10+
///
11+
/// # Examples
12+
///
13+
/// ```rust
14+
/// # #![feature(async_await)]
15+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
16+
/// #
17+
/// use async_std::io;
18+
/// use async_std::prelude::*;
19+
///
20+
/// let mut buf = Vec::new();
21+
/// let mut reader = io::empty();
22+
/// reader.read_to_end(&mut buf).await?;
23+
///
24+
/// assert!(buf.is_empty());
25+
/// #
26+
/// # Ok(()) }) }
27+
/// ```
28+
pub fn empty() -> Empty {
29+
Empty { _priv: () }
30+
}
31+
32+
/// A reader that contains no data.
33+
///
34+
/// This reader is constructed by the [`sink`] function.
35+
///
36+
/// [`sink`]: fn.sink.html
37+
pub struct Empty {
38+
_priv: (),
39+
}
40+
41+
impl fmt::Debug for Empty {
42+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43+
f.pad("Empty { .. }")
44+
}
45+
}
46+
47+
impl AsyncRead for Empty {
48+
#[inline]
49+
fn poll_read(
50+
self: Pin<&mut Self>,
51+
_: &mut Context<'_>,
52+
_: &mut [u8],
53+
) -> Poll<io::Result<usize>> {
54+
Poll::Ready(Ok(0))
55+
}
56+
57+
#[inline]
58+
unsafe fn initializer(&self) -> Initializer {
59+
Initializer::nop()
60+
}
61+
}
62+
63+
impl AsyncBufRead for Empty {
64+
#[inline]
65+
fn poll_fill_buf(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
66+
Poll::Ready(Ok(&[]))
67+
}
68+
69+
#[inline]
70+
fn consume(self: Pin<&mut Self>, _: usize) {}
71+
}

src/io/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
//! ```
2323
2424
#[doc(inline)]
25-
pub use std::io::{empty, sink, Cursor, Empty, Error, ErrorKind, Result, SeekFrom, Sink};
25+
pub use std::io::{Error, ErrorKind, Result, SeekFrom};
2626

2727
pub use buf_read::{BufRead, Lines};
2828
pub use buf_reader::BufReader;
2929
pub use copy::copy;
30+
pub use empty::{empty, Empty};
3031
pub use read::Read;
3132
pub use seek::Seek;
33+
pub use sink::{sink, Sink};
3234
pub use stderr::{stderr, Stderr};
3335
pub use stdin::{stdin, Stdin};
3436
pub use stdout::{stdout, Stdout};
@@ -37,8 +39,10 @@ pub use write::Write;
3739
mod buf_read;
3840
mod buf_reader;
3941
mod copy;
42+
mod empty;
4043
mod read;
4144
mod seek;
45+
mod sink;
4246
mod stderr;
4347
mod stdin;
4448
mod stdout;

src/io/sink.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::fmt;
2+
use std::pin::Pin;
3+
4+
use futures::io::AsyncWrite;
5+
6+
use crate::io;
7+
use crate::task::{Context, Poll};
8+
9+
/// Creates a writer that consumes and drops all data.
10+
///
11+
/// # Examples
12+
///
13+
/// ```rust
14+
/// # #![feature(async_await)]
15+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
16+
/// #
17+
/// use async_std::io;
18+
/// use async_std::prelude::*;
19+
///
20+
/// let mut writer = io::sink();
21+
/// writer.write(b"hello world").await?;
22+
/// #
23+
/// # Ok(()) }) }
24+
/// ```
25+
pub fn sink() -> Sink {
26+
Sink { _priv: () }
27+
}
28+
29+
/// A writer that consumes and drops all data.
30+
///
31+
/// This writer is constructed by the [`sink`] function.
32+
///
33+
/// [`sink`]: fn.sink.html
34+
pub struct Sink {
35+
_priv: (),
36+
}
37+
38+
impl fmt::Debug for Sink {
39+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40+
f.pad("Sink { .. }")
41+
}
42+
}
43+
44+
impl AsyncWrite for Sink {
45+
#[inline]
46+
fn poll_write(
47+
self: Pin<&mut Self>,
48+
_: &mut Context<'_>,
49+
buf: &[u8],
50+
) -> Poll<io::Result<usize>> {
51+
Poll::Ready(Ok(buf.len()))
52+
}
53+
54+
#[inline]
55+
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
56+
Poll::Ready(Ok(()))
57+
}
58+
59+
#[inline]
60+
fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
61+
Poll::Ready(Ok(()))
62+
}
63+
}

src/io/stderr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::pin::Pin;
33
use std::sync::Mutex;
44

55
use cfg_if::cfg_if;
6+
use futures::io::AsyncWrite;
67

78
use crate::future::Future;
89
use crate::task::{blocking, Context, Poll};
@@ -80,7 +81,7 @@ enum Operation {
8081
Flush(io::Result<()>),
8182
}
8283

83-
impl futures::io::AsyncWrite for Stderr {
84+
impl AsyncWrite for Stderr {
8485
fn poll_write(
8586
mut self: Pin<&mut Self>,
8687
cx: &mut Context<'_>,

src/io/stdin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::sync::Mutex;
44

55
use cfg_if::cfg_if;
66
use futures::future;
7-
use futures::io::Initializer;
7+
use futures::io::{AsyncRead, Initializer};
88

99
use crate::future::Future;
1010
use crate::task::{blocking, Context, Poll};
@@ -140,7 +140,7 @@ impl Stdin {
140140
}
141141
}
142142

143-
impl futures::io::AsyncRead for Stdin {
143+
impl AsyncRead for Stdin {
144144
fn poll_read(
145145
mut self: Pin<&mut Self>,
146146
cx: &mut Context<'_>,

src/io/stdout.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::pin::Pin;
33
use std::sync::Mutex;
44

55
use cfg_if::cfg_if;
6+
use futures::io::AsyncWrite;
67

78
use crate::future::Future;
89
use crate::task::{blocking, Context, Poll};
@@ -80,7 +81,7 @@ enum Operation {
8081
Flush(io::Result<()>),
8182
}
8283

83-
impl futures::io::AsyncWrite for Stdout {
84+
impl AsyncWrite for Stdout {
8485
fn poll_write(
8586
mut self: Pin<&mut Self>,
8687
cx: &mut Context<'_>,

src/net/tcp/stream.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::pin::Pin;
55

66
use cfg_if::cfg_if;
77
use futures::future;
8+
use futures::io::{AsyncRead, AsyncWrite};
89

910
use crate::io;
1011
use crate::net::driver::IoHandle;
@@ -355,7 +356,7 @@ impl TcpStream {
355356
}
356357
}
357358

358-
impl futures::io::AsyncRead for TcpStream {
359+
impl AsyncRead for TcpStream {
359360
fn poll_read(
360361
self: Pin<&mut Self>,
361362
cx: &mut Context<'_>,
@@ -373,7 +374,7 @@ impl futures::io::AsyncRead for TcpStream {
373374
}
374375
}
375376

376-
impl futures::io::AsyncRead for &TcpStream {
377+
impl AsyncRead for &TcpStream {
377378
fn poll_read(
378379
self: Pin<&mut Self>,
379380
cx: &mut Context<'_>,
@@ -391,7 +392,7 @@ impl futures::io::AsyncRead for &TcpStream {
391392
}
392393
}
393394

394-
impl futures::io::AsyncWrite for TcpStream {
395+
impl AsyncWrite for TcpStream {
395396
fn poll_write(
396397
self: Pin<&mut Self>,
397398
cx: &mut Context<'_>,
@@ -417,7 +418,7 @@ impl futures::io::AsyncWrite for TcpStream {
417418
}
418419
}
419420

420-
impl futures::io::AsyncWrite for &TcpStream {
421+
impl AsyncWrite for &TcpStream {
421422
fn poll_write(
422423
self: Pin<&mut Self>,
423424
cx: &mut Context<'_>,

src/os/unix/net/stream.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::path::Path;
77
use std::pin::Pin;
88

99
use futures::future;
10+
use futures::io::{AsyncRead, AsyncWrite};
1011
use mio_uds;
1112

1213
use super::SocketAddr;
@@ -198,7 +199,7 @@ impl UnixStream {
198199
}
199200
}
200201

201-
impl futures::io::AsyncRead for UnixStream {
202+
impl AsyncRead for UnixStream {
202203
fn poll_read(
203204
self: Pin<&mut Self>,
204205
cx: &mut Context<'_>,
@@ -208,7 +209,7 @@ impl futures::io::AsyncRead for UnixStream {
208209
}
209210
}
210211

211-
impl futures::io::AsyncRead for &UnixStream {
212+
impl AsyncRead for &UnixStream {
212213
fn poll_read(
213214
self: Pin<&mut Self>,
214215
cx: &mut Context<'_>,
@@ -218,7 +219,7 @@ impl futures::io::AsyncRead for &UnixStream {
218219
}
219220
}
220221

221-
impl futures::io::AsyncWrite for UnixStream {
222+
impl AsyncWrite for UnixStream {
222223
fn poll_write(
223224
self: Pin<&mut Self>,
224225
cx: &mut Context<'_>,
@@ -236,7 +237,7 @@ impl futures::io::AsyncWrite for UnixStream {
236237
}
237238
}
238239

239-
impl futures::io::AsyncWrite for &UnixStream {
240+
impl AsyncWrite for &UnixStream {
240241
fn poll_write(
241242
self: Pin<&mut Self>,
242243
cx: &mut Context<'_>,

0 commit comments

Comments
 (0)