Skip to content

Commit

Permalink
Rollup merge of rust-lang#106664 - chenyukang:yukang/fix-106597-remov…
Browse files Browse the repository at this point in the history
…e-lseek, r=cuviper

Remove unnecessary lseek syscall when using std::fs::read

Fixes rust-lang#106597
r? ``@bjorn3``
  • Loading branch information
Noratrieb authored Jan 11, 2023
2 parents 25e5025 + f7bc68b commit aa91217
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,9 @@ pub struct DirBuilder {
pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
fn inner(path: &Path) -> io::Result<Vec<u8>> {
let mut file = File::open(path)?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)?;
let size = file.metadata().map(|m| m.len()).unwrap_or(0);
let mut bytes = Vec::with_capacity(size as usize);
io::default_read_to_end(&mut file, &mut bytes)?;
Ok(bytes)
}
inner(path.as_ref())
Expand Down Expand Up @@ -288,8 +289,9 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
fn inner(path: &Path) -> io::Result<String> {
let mut file = File::open(path)?;
let mut string = String::new();
file.read_to_string(&mut string)?;
let size = file.metadata().map(|m| m.len()).unwrap_or(0);
let mut string = String::with_capacity(size as usize);
io::default_read_to_string(&mut file, &mut string)?;
Ok(string)
}
inner(path.as_ref())
Expand Down

0 comments on commit aa91217

Please sign in to comment.