Skip to content

Commit 28acba3

Browse files
committed
Auto merge of #115460 - zachs18:borrowedcursor_write_no_panic, 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` #78485 (comment)) (I'm not sure if this needs an ACP? since it's not changing the "API", just what the function does)
2 parents 755629f + 11a64a1 commit 28acba3

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

library/std/src/io/readbuf.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,9 @@ impl<'a> BorrowedCursor<'a> {
306306

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

313314
#[inline]

0 commit comments

Comments
 (0)