Skip to content

Commit

Permalink
Auto merge of #115460 - zachs18:borrowedcursor_write_no_panic, r=dtolnay
Browse files Browse the repository at this point in the history
Don't panic in `<BorrowedCursor as io::Write>::write`

Instead of panicking if the BorrowedCursor does not have enough capacity for the whole buffer, just return a short write, [like `<&mut [u8] as io::Write>::write` does](https://doc.rust-lang.org/src/std/io/impls.rs.html#349).

(cc `@ChayimFriedman2` #78485 (comment))

(I'm not sure if this needs an ACP? since it's not changing the "API", just what the function does)
  • Loading branch information
bors committed Nov 8, 2023
2 parents 755629f + 11a64a1 commit 28acba3
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions library/std/src/io/readbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,9 @@ impl<'a> BorrowedCursor<'a> {

impl<'a> Write for BorrowedCursor<'a> {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.append(buf);
Ok(buf.len())
let amt = cmp::min(buf.len(), self.capacity());
self.append(&buf[..amt]);
Ok(amt)
}

#[inline]
Expand Down

0 comments on commit 28acba3

Please sign in to comment.