Skip to content

Commit

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

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

Fixes #106597
r? ```@bjorn3```
  • Loading branch information
Noratrieb authored Jan 11, 2023
2 parents 6e0c404 + f7bc68b commit 9a820f7
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 9a820f7

Please sign in to comment.