summaryrefslogtreecommitdiffstats
path: root/library/std/src/fs.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
commit218caa410aa38c29984be31a5229b9fa717560ee (patch)
treec54bd55eeb6e4c508940a30e94c0032fbd45d677 /library/std/src/fs.rs
parentReleasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz
rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/std/src/fs.rs')
-rw-r--r--library/std/src/fs.rs12
1 files changed, 7 insertions, 5 deletions
diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs
index f357d505f..286ad68fd 100644
--- a/library/std/src/fs.rs
+++ b/library/std/src/fs.rs
@@ -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())
@@ -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())
@@ -1510,7 +1512,7 @@ impl FileType {
}
/// Tests whether this file type represents a regular file.
- /// The result is mutually exclusive to the results of
+ /// The result is mutually exclusive to the results of
/// [`is_dir`] and [`is_symlink`]; only zero or one of these
/// tests may pass.
///