Skip to content

Commit 22dca7e

Browse files
committed
Auto merge of rust-lang#141211 - fluiderson:dev, r=thomcc
Replace `try_reserve_exact` with `try_with_capacity` in `std::fs::read` This change restores the previous behavior prior to rust-lang#117925. That PR was made to handle OOM errors that turn into a panic with `Vec::with_capacity`. `try_reserve_exact` was used for that since there was no `try_with_capacity` method at the time. It was added later in rust-lang#120504. I think it'd a better fit here.
2 parents 60dabef + 0dec3fe commit 22dca7e

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

library/std/src/fs.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,7 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
285285
fn inner(path: &Path) -> io::Result<Vec<u8>> {
286286
let mut file = File::open(path)?;
287287
let size = file.metadata().map(|m| m.len() as usize).ok();
288-
let mut bytes = Vec::new();
289-
bytes.try_reserve_exact(size.unwrap_or(0))?;
288+
let mut bytes = Vec::try_with_capacity(size.unwrap_or(0))?;
290289
io::default_read_to_end(&mut file, &mut bytes, size)?;
291290
Ok(bytes)
292291
}

0 commit comments

Comments
 (0)