Skip to content

Commit 7409e41

Browse files
authoredJan 28, 2025
clarify BufRead::{fill_buf, consume} docs
Fixes #85394
1 parent 2f348cb commit 7409e41

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed
 

‎library/std/src/io/mod.rs

+18-26
Original file line numberDiff line numberDiff line change
@@ -2250,24 +2250,18 @@ fn skip_until<R: BufRead + ?Sized>(r: &mut R, delim: u8) -> Result<usize> {
22502250
/// ```
22512251
#[stable(feature = "rust1", since = "1.0.0")]
22522252
pub trait BufRead: Read {
2253-
/// Returns the contents of the internal buffer, filling it with more data
2254-
/// from the inner reader if it is empty.
2253+
/// Returns the contents of the internal buffer, filling it with more data, via Read methods, if empty.
22552254
///
2256-
/// This function is a lower-level call. It needs to be paired with the
2257-
/// [`consume`] method to function properly. When calling this
2258-
/// method, none of the contents will be "read" in the sense that later
2259-
/// calling `read` may return the same contents. As such, [`consume`] must
2260-
/// be called with the number of bytes that are consumed from this buffer to
2261-
/// ensure that the bytes are never returned twice.
2255+
/// This is a lower-level method and is meant to be used together with [`consume`],
2256+
/// which can be used to mark bytes that should not be returned by subsequent calls to `read`.
22622257
///
22632258
/// [`consume`]: BufRead::consume
22642259
///
2265-
/// An empty buffer returned indicates that the stream has reached EOF.
2260+
/// Returns an empty buffer to indicate that the stream has reached EOF.
22662261
///
22672262
/// # Errors
22682263
///
2269-
/// This function will return an I/O error if the underlying reader was
2270-
/// read, but returned an error.
2264+
/// Passes on I/O errors from Read.
22712265
///
22722266
/// # Examples
22732267
///
@@ -2285,26 +2279,20 @@ pub trait BufRead: Read {
22852279
/// // work with buffer
22862280
/// println!("{buffer:?}");
22872281
///
2288-
/// // ensure the bytes we worked with aren't returned again later
2289-
/// let length = buffer.len();
2290-
/// stdin.consume(length);
2282+
/// // mark the bytes we worked with as read
2283+
/// stdin.consume(buffer.len());
22912284
/// # std::io::Result::Ok(())
22922285
/// ```
22932286
#[stable(feature = "rust1", since = "1.0.0")]
22942287
fn fill_buf(&mut self) -> Result<&[u8]>;
22952288

2296-
/// Tells this buffer that `amt` bytes have been consumed from the buffer,
2297-
/// so they should no longer be returned in calls to `read`.
2289+
/// Marks the given `amount` of additional bytes from the internal buffer as having been read.
2290+
/// Subsequent calls to `read` return bytes that have not yet been so marked.
22982291
///
2299-
/// This function is a lower-level call. It needs to be paired with the
2300-
/// [`fill_buf`] method to function properly. This function does
2301-
/// not perform any I/O, it simply informs this object that some amount of
2302-
/// its buffer, returned from [`fill_buf`], has been consumed and should
2303-
/// no longer be returned. As such, this function may do odd things if
2304-
/// [`fill_buf`] isn't called before calling it.
2292+
/// This is a lower-level method and is meant to be used together with [`fill_buf`],
2293+
/// which can be used to fill the internal buffer via Read methods.
23052294
///
2306-
/// The `amt` must be `<=` the number of bytes in the buffer returned by
2307-
/// [`fill_buf`].
2295+
/// It is a logic error if `amount` exceeds the number of unread bytes in the internal buffer.
23082296
///
23092297
/// # Examples
23102298
///
@@ -2313,9 +2301,9 @@ pub trait BufRead: Read {
23132301
///
23142302
/// [`fill_buf`]: BufRead::fill_buf
23152303
#[stable(feature = "rust1", since = "1.0.0")]
2316-
fn consume(&mut self, amt: usize);
2304+
fn consume(&mut self, amount: usize);
23172305

2318-
/// Checks if the underlying `Read` has any data left to be read.
2306+
/// Checks if there is any data left to be `read`.
23192307
///
23202308
/// This function may fill the buffer to check for data,
23212309
/// so this functions returns `Result<bool>`, not `bool`.
@@ -2324,6 +2312,10 @@ pub trait BufRead: Read {
23242312
/// returned slice is empty (which means that there is no data left,
23252313
/// since EOF is reached).
23262314
///
2315+
/// # Errors
2316+
///
2317+
/// Passes on I/O errors from Read.
2318+
///
23272319
/// Examples
23282320
///
23292321
/// ```

0 commit comments

Comments
 (0)