Skip to content

Commit c98b68b

Browse files
committed
Change read_to_{end, string} to return the total number of bytes read
1 parent a152e6f commit c98b68b

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

futures-util/src/io/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ pub trait AsyncReadExt: AsyncRead {
219219

220220
/// Creates a future which will read all the bytes from this `AsyncRead`.
221221
///
222+
/// On success the total number of bytes read is returned.
223+
///
222224
/// # Examples
223225
///
224226
/// ```
@@ -230,8 +232,9 @@ pub trait AsyncReadExt: AsyncRead {
230232
/// let mut reader = Cursor::new([1, 2, 3, 4]);
231233
/// let mut output = Vec::with_capacity(4);
232234
///
233-
/// reader.read_to_end(&mut output).await?;
235+
/// let bytes = reader.read_to_end(&mut output).await?;
234236
///
237+
/// assert_eq!(bytes, 4);
235238
/// assert_eq!(output, vec![1, 2, 3, 4]);
236239
/// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap();
237240
/// ```
@@ -246,6 +249,8 @@ pub trait AsyncReadExt: AsyncRead {
246249

247250
/// Creates a future which will read all the bytes from this `AsyncRead`.
248251
///
252+
/// On success the total number of bytes read is returned.
253+
///
249254
/// # Examples
250255
///
251256
/// ```
@@ -257,8 +262,9 @@ pub trait AsyncReadExt: AsyncRead {
257262
/// let mut reader = Cursor::new(&b"1234"[..]);
258263
/// let mut buffer = String::with_capacity(4);
259264
///
260-
/// reader.read_to_string(&mut buffer).await?;
265+
/// let bytes = reader.read_to_string(&mut buffer).await?;
261266
///
267+
/// assert_eq!(bytes, 4);
262268
/// assert_eq!(buffer, String::from("1234"));
263269
/// # Ok::<(), Box<dyn std::error::Error>>(()) }).unwrap();
264270
/// ```

futures-util/src/io/read_to_end.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@ use std::vec::Vec;
1111
pub struct ReadToEnd<'a, R: ?Sized + Unpin> {
1212
reader: &'a mut R,
1313
buf: &'a mut Vec<u8>,
14+
start_len: usize,
1415
}
1516

1617
impl<R: ?Sized + Unpin> Unpin for ReadToEnd<'_, R> {}
1718

1819
impl<'a, R: AsyncRead + ?Sized + Unpin> ReadToEnd<'a, R> {
1920
pub(super) fn new(reader: &'a mut R, buf: &'a mut Vec<u8>) -> Self {
20-
ReadToEnd { reader, buf }
21+
let start_len = buf.len();
22+
Self {
23+
reader,
24+
buf,
25+
start_len,
26+
}
2127
}
2228
}
2329

@@ -42,7 +48,8 @@ pub(super) fn read_to_end_internal<R: AsyncRead + ?Sized>(
4248
mut rd: Pin<&mut R>,
4349
cx: &mut Context<'_>,
4450
buf: &mut Vec<u8>,
45-
) -> Poll<io::Result<()>> {
51+
start_len: usize,
52+
) -> Poll<io::Result<usize>> {
4653
let mut g = Guard { len: buf.len(), buf };
4754
let ret;
4855
loop {
@@ -57,7 +64,7 @@ pub(super) fn read_to_end_internal<R: AsyncRead + ?Sized>(
5764

5865
match ready!(rd.as_mut().poll_read(cx, &mut g.buf[g.len..])) {
5966
Ok(0) => {
60-
ret = Poll::Ready(Ok(()));
67+
ret = Poll::Ready(Ok(g.len - start_len));
6168
break;
6269
}
6370
Ok(n) => g.len += n,
@@ -74,10 +81,10 @@ pub(super) fn read_to_end_internal<R: AsyncRead + ?Sized>(
7481
impl<A> Future for ReadToEnd<'_, A>
7582
where A: AsyncRead + ?Sized + Unpin,
7683
{
77-
type Output = io::Result<()>;
84+
type Output = io::Result<usize>;
7885

7986
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
8087
let this = &mut *self;
81-
read_to_end_internal(Pin::new(&mut this.reader), cx, this.buf)
88+
read_to_end_internal(Pin::new(&mut this.reader), cx, this.buf, this.start_len)
8289
}
8390
}

futures-util/src/io/read_to_string.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ pub struct ReadToString<'a, R: ?Sized + Unpin> {
1313
reader: &'a mut R,
1414
buf: &'a mut String,
1515
bytes: Vec<u8>,
16+
start_len: usize,
1617
}
1718

1819
impl<R: ?Sized + Unpin> Unpin for ReadToString<'_, R> {}
1920

2021
impl<'a, R: AsyncRead + ?Sized + Unpin> ReadToString<'a, R> {
2122
pub(super) fn new(reader: &'a mut R, buf: &'a mut String) -> Self {
23+
let start_len = buf.len();
2224
Self {
2325
reader,
2426
bytes: unsafe { mem::replace(buf.as_mut_vec(), Vec::new()) },
2527
buf,
28+
start_len,
2629
}
2730
}
2831
}
@@ -32,8 +35,9 @@ fn read_to_string_internal<R: AsyncRead + ?Sized>(
3235
cx: &mut Context<'_>,
3336
buf: &mut String,
3437
bytes: &mut Vec<u8>,
35-
) -> Poll<io::Result<()>> {
36-
let ret = ready!(read_to_end_internal(reader, cx, bytes));
38+
start_len: usize,
39+
) -> Poll<io::Result<usize>> {
40+
let ret = ready!(read_to_end_internal(reader, cx, bytes, start_len));
3741
if str::from_utf8(&bytes).is_err() {
3842
Poll::Ready(ret.and_then(|_| {
3943
Err(io::Error::new(
@@ -53,10 +57,10 @@ impl<A> Future for ReadToString<'_, A>
5357
where
5458
A: AsyncRead + ?Sized + Unpin,
5559
{
56-
type Output = io::Result<()>;
60+
type Output = io::Result<usize>;
5761

5862
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
59-
let Self { reader, buf, bytes } = &mut *self;
60-
read_to_string_internal(Pin::new(reader), cx, buf, bytes)
63+
let Self { reader, buf, bytes, start_len } = &mut *self;
64+
read_to_string_internal(Pin::new(reader), cx, buf, bytes, *start_len)
6165
}
6266
}

futures/tests/io_read_to_string.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ use std::io::Cursor;
1111
fn read_to_string() {
1212
let mut c = Cursor::new(&b""[..]);
1313
let mut v = String::new();
14-
assert!(block_on(c.read_to_string(&mut v)).is_ok());
14+
assert_eq!(block_on(c.read_to_string(&mut v)).unwrap(), 0);
1515
assert_eq!(v, "");
1616

1717
let mut c = Cursor::new(&b"1"[..]);
1818
let mut v = String::new();
19-
assert!(block_on(c.read_to_string(&mut v)).is_ok());
19+
assert_eq!(block_on(c.read_to_string(&mut v)).unwrap(), 1);
2020
assert_eq!(v, "1");
2121

2222
let mut c = Cursor::new(&b"\xff"[..]);
@@ -41,6 +41,6 @@ fn interleave_pending() {
4141
.interleave_pending();
4242

4343
let mut v = String::new();
44-
assert!(run(buf.read_to_string(&mut v)).is_ok());
44+
assert_eq!(run(buf.read_to_string(&mut v)).unwrap(), 5);
4545
assert_eq!(v, "12333");
4646
}

0 commit comments

Comments
 (0)