Skip to content

Commit 242ff95

Browse files
authored
Rollup merge of rust-lang#139069 - a1phyr:better_take, r=joboet
`io::Take`: avoid new `BorrowedBuf` creation in some case If `self.limit == buf.capacity()`, doing the whole `BorrowedBuf` dance is not necessary.
2 parents 8384b56 + 84f9d7b commit 242ff95

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Diff for: std/src/io/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2989,11 +2989,11 @@ impl<T: Read> Read for Take<T> {
29892989
return Ok(());
29902990
}
29912991

2992-
if self.limit <= buf.capacity() as u64 {
2993-
// if we just use an as cast to convert, limit may wrap around on a 32 bit target
2994-
let limit = cmp::min(self.limit, usize::MAX as u64) as usize;
2992+
if self.limit < buf.capacity() as u64 {
2993+
// The condition above guarantees that `self.limit` fits in `usize`.
2994+
let limit = self.limit as usize;
29952995

2996-
let extra_init = cmp::min(limit as usize, buf.init_ref().len());
2996+
let extra_init = cmp::min(limit, buf.init_ref().len());
29972997

29982998
// SAFETY: no uninit data is written to ibuf
29992999
let ibuf = unsafe { &mut buf.as_mut()[..limit] };

0 commit comments

Comments
 (0)