Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::io: Modify some ReadBuf method signatures to return &mut Self #95770

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions library/std/src/io/readbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ impl<'a> ReadBuf<'a> {
///
/// The number of initialized bytes is not changed, and the contents of the buffer are not modified.
#[inline]
pub fn clear(&mut self) {
self.set_filled(0); // The assertion in `set_filled` is optimized out
pub fn clear(&mut self) -> &mut Self {
self.set_filled(0) // The assertion in `set_filled` is optimized out
}

/// Increases the size of the filled region of the buffer.
Expand All @@ -178,8 +178,8 @@ impl<'a> ReadBuf<'a> {
///
/// Panics if the filled region of the buffer would become larger than the initialized region.
#[inline]
pub fn add_filled(&mut self, n: usize) {
self.set_filled(self.filled + n);
pub fn add_filled(&mut self, n: usize) -> &mut Self {
self.set_filled(self.filled + n)
}

/// Sets the size of the filled region of the buffer.
Expand All @@ -193,10 +193,11 @@ impl<'a> ReadBuf<'a> {
///
/// Panics if the filled region of the buffer would become larger than the initialized region.
#[inline]
pub fn set_filled(&mut self, n: usize) {
pub fn set_filled(&mut self, n: usize) -> &mut Self {
assert!(n <= self.initialized);

self.filled = n;
self
}

/// Asserts that the first `n` unfilled bytes of the buffer are initialized.
Expand All @@ -208,8 +209,9 @@ impl<'a> ReadBuf<'a> {
///
/// The caller must ensure that the first `n` unfilled bytes of the buffer have already been initialized.
#[inline]
pub unsafe fn assume_init(&mut self, n: usize) {
pub unsafe fn assume_init(&mut self, n: usize) -> &mut Self {
self.initialized = cmp::max(self.initialized, self.filled + n);
self
}

/// Appends data to the buffer, advancing the written position and possibly also the initialized position.
Expand All @@ -227,7 +229,9 @@ impl<'a> ReadBuf<'a> {
}

// SAFETY: We just added the entire contents of buf to the filled section.
unsafe { self.assume_init(buf.len()) }
unsafe {
self.assume_init(buf.len());
}
self.add_filled(buf.len());
}

Expand Down