about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-01-18 14:03:12 +0000
committerbors <bors@rust-lang.org>2018-01-18 14:03:12 +0000
commit3bd4af88bea2e6ecdd3455ed89b3ef1fc3500aa4 (patch)
tree7932af7a8bee193a20ff42ba160c1297e4aedfb4 /src/libstd
parent44afd76788df1a63fcd3fe19815eb28dbe9c2bf7 (diff)
parent260653772a35d44ea474fc6d464432ac6f2a676f (diff)
downloadrust-3bd4af88bea2e6ecdd3455ed89b3ef1fc3500aa4.tar.gz
rust-3bd4af88bea2e6ecdd3455ed89b3ef1fc3500aa4.zip
Auto merge of #47528 - GuillaumeGomez:rollup, r=GuillaumeGomez
Rollup of 6 pull requests

- Successful merges: #47250, #47313, #47398, #47468, #47471, #47520
- Failed merges:
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/fs.rs16
1 files changed, 9 insertions, 7 deletions
diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs
index 4e0ff450cab..d1f3ccbd2c6 100644
--- a/src/libstd/fs.rs
+++ b/src/libstd/fs.rs
@@ -211,12 +211,12 @@ pub struct DirBuilder {
     recursive: bool,
 }
 
-/// How large a buffer to pre-allocate before reading the entire file at `path`.
-fn initial_buffer_size<P: AsRef<Path>>(path: P) -> usize {
+/// How large a buffer to pre-allocate before reading the entire file.
+fn initial_buffer_size(file: &File) -> usize {
     // Allocate one extra byte so the buffer doesn't need to grow before the
     // final `read` call at the end of the file.  Don't worry about `usize`
     // overflow because reading will fail regardless in that case.
-    metadata(path).map(|m| m.len() as usize + 1).unwrap_or(0)
+    file.metadata().map(|m| m.len() as usize + 1).unwrap_or(0)
 }
 
 /// Read the entire contents of a file into a bytes vector.
@@ -254,8 +254,9 @@ fn initial_buffer_size<P: AsRef<Path>>(path: P) -> usize {
 /// ```
 #[unstable(feature = "fs_read_write", issue = "46588")]
 pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
-    let mut bytes = Vec::with_capacity(initial_buffer_size(&path));
-    File::open(path)?.read_to_end(&mut bytes)?;
+    let mut file = File::open(path)?;
+    let mut bytes = Vec::with_capacity(initial_buffer_size(&file));
+    file.read_to_end(&mut bytes)?;
     Ok(bytes)
 }
 
@@ -295,8 +296,9 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
 /// ```
 #[unstable(feature = "fs_read_write", issue = "46588")]
 pub fn read_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
-    let mut string = String::with_capacity(initial_buffer_size(&path));
-    File::open(path)?.read_to_string(&mut string)?;
+    let mut file = File::open(path)?;
+    let mut string = String::with_capacity(initial_buffer_size(&file));
+    file.read_to_string(&mut string)?;
     Ok(string)
 }