Skip to content

Commit

Permalink
Rollup merge of rust-lang#115460 - zachs18:borrowedcursor_write_no_pa…
Browse files Browse the repository at this point in the history
…nic, r=dtolnay

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` rust-lang#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
fmease authored Nov 8, 2023
2 parents ca1ca41 + 11a64a1 commit 7b11383
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 7b11383

Please sign in to comment.