summary refs log tree commit diff
path: root/library
diff options
context:
space:
mode:
authorBenoît du Garreau <benoit@dugarreau.fr>2025-03-28 15:58:41 +0100
committerBenoît du Garreau <benoit@dugarreau.fr>2025-03-28 16:36:49 +0100
commit9055765ce1c89b0bc619df39aa8ba66ad641da9b (patch)
tree1564806fa8c3b8a889492dcc00fb3c42256c0034 /library
parent2a06022951893fe5b5384f8dbd75b4e6e3b5cee0 (diff)
downloadrust-9055765ce1c89b0bc619df39aa8ba66ad641da9b.tar.gz
rust-9055765ce1c89b0bc619df39aa8ba66ad641da9b.zip
`io::Take`: avoid new `BorrowedBuf` creation in some case
Diffstat (limited to 'library')
-rw-r--r--library/std/src/io/mod.rs8
1 files changed, 4 insertions, 4 deletions
diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs
index 6579b6887aa..314cbb45d49 100644
--- a/library/std/src/io/mod.rs
+++ b/library/std/src/io/mod.rs
@@ -2989,11 +2989,11 @@ impl<T: Read> Read for Take<T> {
             return Ok(());
         }
 
-        if self.limit <= buf.capacity() as u64 {
-            // if we just use an as cast to convert, limit may wrap around on a 32 bit target
-            let limit = cmp::min(self.limit, usize::MAX as u64) as usize;
+        if self.limit < buf.capacity() as u64 {
+            // The condition above guarantees that `self.limit` fits in `usize`.
+            let limit = self.limit as usize;
 
-            let extra_init = cmp::min(limit as usize, buf.init_ref().len());
+            let extra_init = cmp::min(limit, buf.init_ref().len());
 
             // SAFETY: no uninit data is written to ibuf
             let ibuf = unsafe { &mut buf.as_mut()[..limit] };