about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-11-08 14:08:48 +0000
committerbors <bors@rust-lang.org>2023-11-08 14:08:48 +0000
commit28acba3c61246ba5974df332a064ab0c19b1cff8 (patch)
tree5ad3dfbe5b41cb57f82af941e29779aa68b0020c
parent755629fe5942cfe24a865f5e8464be346f051066 (diff)
parent11a64a18342865b166d2da703a06deb447f834c1 (diff)
downloadrust-28acba3c61246ba5974df332a064ab0c19b1cff8.tar.gz
rust-28acba3c61246ba5974df332a064ab0c19b1cff8.zip
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` https://github.com/rust-lang/rust/issues/78485#issuecomment-1493129588)

(I'm not sure if this needs an ACP? since it's not changing the "API", just what the function does)
-rw-r--r--library/std/src/io/readbuf.rs5
1 files changed, 3 insertions, 2 deletions
diff --git a/library/std/src/io/readbuf.rs b/library/std/src/io/readbuf.rs
index 034ddd8df9a..95044de2a09 100644
--- a/library/std/src/io/readbuf.rs
+++ b/library/std/src/io/readbuf.rs
@@ -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]