diff options
| author | bors <bors@rust-lang.org> | 2024-02-17 00:23:15 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-02-17 00:23:15 +0000 |
| commit | 405b22f1a3a39eef5f4698b3662097c8a4f6f5d0 (patch) | |
| tree | 392d21332697dbf673926d68eb49ae13c3b94d0d /library/core/src | |
| parent | bccb9bbb418a30aeb332052e721beb6ebc6b1ce7 (diff) | |
| parent | 0a42a540c603846aa22f29f378a61a64c9d4383e (diff) | |
| download | rust-405b22f1a3a39eef5f4698b3662097c8a4f6f5d0.tar.gz rust-405b22f1a3a39eef5f4698b3662097c8a4f6f5d0.zip | |
Auto merge of #120741 - a1phyr:safe_buffer_advance, r=m-ou-se
Make `io::BorrowedCursor::advance` safe This also keeps the old `advance` method under `advance_unchecked` name. This makes pattern like `std::io::default_read_buf` safe to write.
Diffstat (limited to 'library/core/src')
| -rw-r--r-- | library/core/src/io/borrowed_buf.rs | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/library/core/src/io/borrowed_buf.rs b/library/core/src/io/borrowed_buf.rs index b7be7811c8b..ed06ce6927e 100644 --- a/library/core/src/io/borrowed_buf.rs +++ b/library/core/src/io/borrowed_buf.rs @@ -239,12 +239,32 @@ impl<'a> BorrowedCursor<'a> { /// accessed via the underlying buffer. I.e., the buffer's filled portion grows by `n` elements /// and its unfilled portion (and the capacity of this cursor) shrinks by `n` elements. /// + /// If less than `n` bytes initialized (by the cursor's point of view), `set_init` should be + /// called first. + /// + /// # Panics + /// + /// Panics if there are less than `n` bytes initialized. + #[inline] + pub fn advance(&mut self, n: usize) -> &mut Self { + assert!(self.buf.init >= self.buf.filled + n); + + self.buf.filled += n; + self + } + + /// Advance the cursor by asserting that `n` bytes have been filled. + /// + /// After advancing, the `n` bytes are no longer accessible via the cursor and can only be + /// accessed via the underlying buffer. I.e., the buffer's filled portion grows by `n` elements + /// and its unfilled portion (and the capacity of this cursor) shrinks by `n` elements. + /// /// # Safety /// /// The caller must ensure that the first `n` bytes of the cursor have been properly /// initialised. #[inline] - pub unsafe fn advance(&mut self, n: usize) -> &mut Self { + pub unsafe fn advance_unchecked(&mut self, n: usize) -> &mut Self { self.buf.filled += n; self.buf.init = cmp::max(self.buf.init, self.buf.filled); self |
