Skip to content

Commit a27ad62

Browse files
Rollup merge of rust-lang#95770 - nrc:read-buf-builder, r=joshtriplett
std::io: Modify some ReadBuf method signatures to return `&mut Self` This allows using `ReadBuf` in a builder-like style and to setup a `ReadBuf` and pass it to `read_buf` in a single expression, e.g., ``` // With this PR: reader.read_buf(ReadBuf::uninit(buf).assume_init(init_len))?; // Previously: let mut buf = ReadBuf::uninit(buf); buf.assume_init(init_len); reader.read_buf(&mut buf)?; ``` r? `@sfackler` cc rust-lang#78485, rust-lang#94741
2 parents d7b8d77 + a3c30e4 commit a27ad62

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

library/std/src/io/readbuf.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ impl<'a> ReadBuf<'a> {
166166
///
167167
/// The number of initialized bytes is not changed, and the contents of the buffer are not modified.
168168
#[inline]
169-
pub fn clear(&mut self) {
170-
self.set_filled(0); // The assertion in `set_filled` is optimized out
169+
pub fn clear(&mut self) -> &mut Self {
170+
self.set_filled(0) // The assertion in `set_filled` is optimized out
171171
}
172172

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

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

199199
self.filled = n;
200+
self
200201
}
201202

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

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

229231
// SAFETY: We just added the entire contents of buf to the filled section.
230-
unsafe { self.assume_init(buf.len()) }
232+
unsafe {
233+
self.assume_init(buf.len());
234+
}
231235
self.add_filled(buf.len());
232236
}
233237

0 commit comments

Comments
 (0)