Skip to content

Commit a1eaf4e

Browse files
Rollup merge of #106643 - WaffleLapkin:read_recursive, r=joshtriplett
Allow only implementing `Read::read_buf` This PR allows users to only implement `Read::read_buf`, without the need for implementing `Read::read`. `rustc_must_implement_one_of` annotation ensures that **at least** one of the methods is implemented, so that the default impls don't create infinite recursion. Note that `Read::read_buf` is unstable, so this doesn't change anything on stable, there you still need to implement `Read::read`, since you can't implement `Read::read_buf`. Thus, we don't expose `rustc_must_implement_one_of` to stable. r? @thomcc
2 parents ea3f859 + 39429fd commit a1eaf4e

3 files changed

Lines changed: 7 additions & 3 deletions

File tree

library/alloc/src/io/read.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ use crate::vec::Vec;
8484
#[stable(feature = "rust1", since = "1.0.0")]
8585
#[doc(notable_trait)]
8686
#[cfg_attr(not(test), rustc_diagnostic_item = "IoRead")]
87+
#[rustc_must_implement_one_of(read, read_buf)]
8788
pub trait Read {
8889
/// Pull some bytes from this source into the specified buffer, returning
8990
/// how many bytes were read.
@@ -164,7 +165,10 @@ pub trait Read {
164165
/// }
165166
/// ```
166167
#[stable(feature = "rust1", since = "1.0.0")]
167-
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
168+
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
169+
let mut buf = BorrowedBuf::from(buf);
170+
self.read_buf(buf.unfilled()).map(|()| buf.len())
171+
}
168172

169173
/// Like `read`, except that it reads into a slice of buffers.
170174
///

library/core/src/io/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl Seek for Empty {
130130
/// [`Ok(0)`]: Ok
131131
///
132132
/// [`write`]: crate::io::Write::write
133-
/// [`read`]: ../../std/io/trait.Read.html#tymethod.read
133+
/// [`read`]: ../../std/io/trait.Read.html#method.read
134134
///
135135
/// # Examples
136136
///

tests/rustdoc-html/jump-to-def/non-local-method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::cmp::Ordering;
1616
use std::marker::PhantomData;
1717

1818
pub fn bar2<T: Read>(readable: T) {
19-
//@ has - '//a[@href="{{channel}}/alloc/io/read/trait.Read.html#tymethod.read"]' 'read'
19+
//@ has - '//a[@href="{{channel}}/alloc/io/read/trait.Read.html#method.read"]' 'read'
2020
let _ = readable.read(&mut []);
2121
}
2222

0 commit comments

Comments
 (0)