Skip to content

Commit cf2003b

Browse files
committed
Change copy_into/copy_buf_into to free functions for consistency with the standard library
1 parent 0c165a3 commit cf2003b

File tree

5 files changed

+114
-124
lines changed

5 files changed

+114
-124
lines changed

futures-util/src/io/copy.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use futures_core::future::Future;
2+
use futures_core::task::{Context, Poll};
3+
use futures_io::{AsyncRead, AsyncWrite};
4+
use std::io;
5+
use std::pin::Pin;
6+
use super::{BufReader, copy_buf, CopyBuf};
7+
use pin_utils::unsafe_pinned;
8+
9+
/// Creates a future which copies all the bytes from one object to another.
10+
///
11+
/// The returned future will copy all the bytes read from this `AsyncRead` into the
12+
/// `writer` specified. This future will only complete once the `reader` has hit
13+
/// EOF and all bytes have been written to and flushed from the `writer`
14+
/// provided.
15+
///
16+
/// On success the number of bytes is returned.
17+
///
18+
/// # Examples
19+
///
20+
/// ```
21+
/// # futures::executor::block_on(async {
22+
/// use futures::io::{self, AsyncWriteExt, Cursor};
23+
///
24+
/// let reader = Cursor::new([1, 2, 3, 4]);
25+
/// let mut writer = Cursor::new(vec![0u8; 5]);
26+
///
27+
/// let bytes = io::copy(reader, &mut writer).await?;
28+
/// writer.close().await?;
29+
///
30+
/// assert_eq!(bytes, 4);
31+
/// assert_eq!(writer.into_inner(), [1, 2, 3, 4, 0]);
32+
/// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap();
33+
/// ```
34+
pub fn copy<R, W>(reader: R, writer: &mut W) -> Copy<'_, R, W>
35+
where
36+
R: AsyncRead,
37+
W: AsyncWrite + Unpin + ?Sized,
38+
{
39+
Copy {
40+
inner: copy_buf(BufReader::new(reader), writer),
41+
}
42+
}
43+
44+
/// Future for the [`copy()`] function.
45+
#[derive(Debug)]
46+
#[must_use = "futures do nothing unless you `.await` or poll them"]
47+
pub struct Copy<'a, R, W: ?Sized> {
48+
inner: CopyBuf<'a, BufReader<R>, W>,
49+
}
50+
51+
impl<'a, R: AsyncRead, W: ?Sized> Unpin for Copy<'a, R, W> where CopyBuf<'a, BufReader<R>, W>: Unpin {}
52+
53+
impl<'a, R: AsyncRead, W: ?Sized> Copy<'a, R, W> {
54+
unsafe_pinned!(inner: CopyBuf<'a, BufReader<R>, W>);
55+
}
56+
57+
impl<R: AsyncRead, W: AsyncWrite + Unpin + ?Sized> Future for Copy<'_, R, W> {
58+
type Output = io::Result<u64>;
59+
60+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
61+
self.inner().poll(cx)
62+
}
63+
}

futures-util/src/io/copy_buf_into.rs renamed to futures-util/src/io/copy_buf.rs

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,55 @@ use futures_io::{AsyncBufRead, AsyncWrite};
44
use std::io;
55
use std::pin::Pin;
66

7-
/// Future for the [`copy_buf_into`](super::AsyncBufReadExt::copy_buf_into) method.
7+
/// Creates a future which copies all the bytes from one object to another.
8+
///
9+
/// The returned future will copy all the bytes read from this `AsyncBufRead` into the
10+
/// `writer` specified. This future will only complete once the `reader` has hit
11+
/// EOF and all bytes have been written to and flushed from the `writer`
12+
/// provided.
13+
///
14+
/// On success the number of bytes is returned.
15+
///
16+
/// # Examples
17+
///
18+
/// ```
19+
/// # futures::executor::block_on(async {
20+
/// use futures::io::{self, AsyncWriteExt, Cursor};
21+
///
22+
/// let reader = Cursor::new([1, 2, 3, 4]);
23+
/// let mut writer = Cursor::new(vec![0u8; 5]);
24+
///
25+
/// let bytes = io::copy_buf(reader, &mut writer).await?;
26+
/// writer.close().await?;
27+
///
28+
/// assert_eq!(bytes, 4);
29+
/// assert_eq!(writer.into_inner(), [1, 2, 3, 4, 0]);
30+
/// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap();
31+
/// ```
32+
pub fn copy_buf<R, W>(reader: R, writer: &mut W) -> CopyBuf<'_, R, W>
33+
where
34+
R: AsyncBufRead,
35+
W: AsyncWrite + Unpin + ?Sized,
36+
{
37+
CopyBuf {
38+
reader,
39+
writer,
40+
amt: 0,
41+
}
42+
}
43+
44+
/// Future for the [`copy_buf()`] function.
845
#[derive(Debug)]
946
#[must_use = "futures do nothing unless you `.await` or poll them"]
10-
pub struct CopyBufInto<'a, R, W: ?Sized> {
47+
pub struct CopyBuf<'a, R, W: ?Sized> {
1148
reader: R,
1249
writer: &'a mut W,
1350
amt: u64,
1451
}
1552

16-
impl<R: Unpin, W: ?Sized> Unpin for CopyBufInto<'_, R, W> {}
17-
18-
impl<R, W: ?Sized> CopyBufInto<'_, R, W> {
19-
pub(super) fn new(reader: R, writer: &mut W) -> CopyBufInto<'_, R, W> {
20-
CopyBufInto {
21-
reader,
22-
writer,
23-
amt: 0,
24-
}
25-
}
26-
}
53+
impl<R: Unpin, W: ?Sized> Unpin for CopyBuf<'_, R, W> {}
2754

28-
impl<R, W: Unpin + ?Sized> CopyBufInto<'_, R, W> {
55+
impl<R, W: Unpin + ?Sized> CopyBuf<'_, R, W> {
2956
fn project(self: Pin<&mut Self>) -> (Pin<&mut R>, Pin<&mut W>, &mut u64) {
3057
unsafe {
3158
let this = self.get_unchecked_mut();
@@ -34,7 +61,7 @@ impl<R, W: Unpin + ?Sized> CopyBufInto<'_, R, W> {
3461
}
3562
}
3663

37-
impl<R, W> Future for CopyBufInto<'_, R, W>
64+
impl<R, W> Future for CopyBuf<'_, R, W>
3865
where R: AsyncBufRead,
3966
W: AsyncWrite + Unpin + ?Sized,
4067
{

futures-util/src/io/copy_into.rs

Lines changed: 0 additions & 34 deletions
This file was deleted.

futures-util/src/io/mod.rs

Lines changed: 7 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ pub use self::chain::Chain;
5353
mod close;
5454
pub use self::close::Close;
5555

56-
mod copy_into;
57-
pub use self::copy_into::CopyInto;
56+
mod copy;
57+
pub use self::copy::{copy, Copy};
5858

59-
mod copy_buf_into;
60-
pub use self::copy_buf_into::CopyBufInto;
59+
mod copy_buf;
60+
pub use self::copy_buf::{copy_buf, CopyBuf};
6161

6262
mod cursor;
6363
pub use self::cursor::Cursor;
@@ -157,39 +157,6 @@ pub trait AsyncReadExt: AsyncRead {
157157
Chain::new(self, next)
158158
}
159159

160-
/// Creates a future which copies all the bytes from one object to another.
161-
///
162-
/// The returned future will copy all the bytes read from this `AsyncRead` into the
163-
/// `writer` specified. This future will only complete once the `reader` has hit
164-
/// EOF and all bytes have been written to and flushed from the `writer`
165-
/// provided.
166-
///
167-
/// On success the number of bytes is returned.
168-
///
169-
/// # Examples
170-
///
171-
/// ```
172-
/// # futures::executor::block_on(async {
173-
/// use futures::io::{AsyncReadExt, AsyncWriteExt, Cursor};
174-
///
175-
/// let reader = Cursor::new([1, 2, 3, 4]);
176-
/// let mut writer = Cursor::new(vec![0u8; 5]);
177-
///
178-
/// let bytes = reader.copy_into(&mut writer).await?;
179-
/// writer.close().await?;
180-
///
181-
/// assert_eq!(bytes, 4);
182-
/// assert_eq!(writer.into_inner(), [1, 2, 3, 4, 0]);
183-
/// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap();
184-
/// ```
185-
fn copy_into<W>(self, writer: &mut W) -> CopyInto<'_, Self, W>
186-
where
187-
Self: Sized,
188-
W: AsyncWrite + Unpin + ?Sized,
189-
{
190-
CopyInto::new(self, writer)
191-
}
192-
193160
/// Tries to read some bytes directly into the given `buf` in asynchronous
194161
/// manner, returning a future type.
195162
///
@@ -342,7 +309,7 @@ pub trait AsyncReadExt: AsyncRead {
342309
///
343310
/// ```
344311
/// # futures::executor::block_on(async {
345-
/// use futures::io::{AsyncReadExt, Cursor};
312+
/// use futures::io::{self, AsyncReadExt, Cursor};
346313
///
347314
/// // Note that for `Cursor` the read and write halves share a single
348315
/// // seek position. This may or may not be true for other types that
@@ -354,8 +321,8 @@ pub trait AsyncReadExt: AsyncRead {
354321
///
355322
/// {
356323
/// let (buffer_reader, mut buffer_writer) = (&mut buffer).split();
357-
/// reader.copy_into(&mut buffer_writer).await?;
358-
/// buffer_reader.copy_into(&mut writer).await?;
324+
/// io::copy(reader, &mut buffer_writer).await?;
325+
/// io::copy(buffer_reader, &mut writer).await?;
359326
/// }
360327
///
361328
/// assert_eq!(buffer.into_inner(), [1, 2, 3, 4, 5, 6, 7, 8]);
@@ -558,39 +525,6 @@ impl<S: AsyncSeek + ?Sized> AsyncSeekExt for S {}
558525

559526
/// An extension trait which adds utility methods to `AsyncBufRead` types.
560527
pub trait AsyncBufReadExt: AsyncBufRead {
561-
/// Creates a future which copies all the bytes from one object to another.
562-
///
563-
/// The returned future will copy all the bytes read from this `AsyncBufRead` into the
564-
/// `writer` specified. This future will only complete once the `reader` has hit
565-
/// EOF and all bytes have been written to and flushed from the `writer`
566-
/// provided.
567-
///
568-
/// On success the number of bytes is returned.
569-
///
570-
/// # Examples
571-
///
572-
/// ```
573-
/// # futures::executor::block_on(async {
574-
/// use futures::io::{AsyncBufReadExt, AsyncWriteExt, Cursor};
575-
///
576-
/// let reader = Cursor::new([1, 2, 3, 4]);
577-
/// let mut writer = Cursor::new(vec![0u8; 5]);
578-
///
579-
/// let bytes = reader.copy_buf_into(&mut writer).await?;
580-
/// writer.close().await?;
581-
///
582-
/// assert_eq!(bytes, 4);
583-
/// assert_eq!(writer.into_inner(), [1, 2, 3, 4, 0]);
584-
/// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap();
585-
/// ```
586-
fn copy_buf_into<W>(self, writer: &mut W) -> CopyBufInto<'_, Self, W>
587-
where
588-
Self: Sized,
589-
W: AsyncWrite + Unpin + ?Sized,
590-
{
591-
CopyBufInto::new(self, writer)
592-
}
593-
594528
/// Creates a future which will read all the bytes associated with this I/O
595529
/// object into `buf` until the delimiter `byte` or EOF is reached.
596530
/// This method is the async equivalent to [`BufRead::read_until`](std::io::BufRead::read_until).

futures/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub mod executor {
134134
//! than threads). Tasks spawned onto the pool with the
135135
//! [`spawn_ok()`](crate::executor::ThreadPool::spawn_ok)
136136
//! function will run on ambiently on the created threads.
137-
//!
137+
//!
138138
//! # Spawning additional tasks
139139
//!
140140
//! Tasks can be spawned onto a spawner by calling its
@@ -271,7 +271,7 @@ pub mod io {
271271

272272
pub use futures_util::io::{
273273
AsyncReadExt, AsyncWriteExt, AsyncSeekExt, AsyncBufReadExt, AllowStdIo,
274-
BufReader, BufWriter, Cursor, Chain, Close, CopyInto, CopyBufInto,
274+
BufReader, BufWriter, Cursor, Chain, Close, copy, Copy, copy_buf, CopyBuf,
275275
empty, Empty, Flush, IntoSink, Lines, Read, ReadExact, ReadHalf,
276276
ReadLine, ReadToEnd, ReadToString, ReadUntil, ReadVectored, repeat,
277277
Repeat, Seek, sink, Sink, Take, Window, Write, WriteAll, WriteHalf,

0 commit comments

Comments
 (0)