@@ -211,12 +211,12 @@ pub struct DirBuilder {
211
211
recursive : bool ,
212
212
}
213
213
214
- /// How large a buffer to pre-allocate before reading the entire file at `path` .
215
- fn initial_buffer_size < P : AsRef < Path > > ( path : P ) -> usize {
214
+ /// How large a buffer to pre-allocate before reading the entire file.
215
+ fn initial_buffer_size ( file : & File ) -> usize {
216
216
// Allocate one extra byte so the buffer doesn't need to grow before the
217
217
// final `read` call at the end of the file. Don't worry about `usize`
218
218
// overflow because reading will fail regardless in that case.
219
- metadata ( path ) . map ( |m| m. len ( ) as usize + 1 ) . unwrap_or ( 0 )
219
+ file . metadata ( ) . map ( |m| m. len ( ) as usize + 1 ) . unwrap_or ( 0 )
220
220
}
221
221
222
222
/// 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 {
254
254
/// ```
255
255
#[ unstable( feature = "fs_read_write" , issue = "46588" ) ]
256
256
pub fn read < P : AsRef < Path > > ( path : P ) -> io:: Result < Vec < u8 > > {
257
- let mut bytes = Vec :: with_capacity ( initial_buffer_size ( & path) ) ;
258
- File :: open ( path) ?. read_to_end ( & mut bytes) ?;
257
+ let mut file = File :: open ( path) ?;
258
+ let mut bytes = Vec :: with_capacity ( initial_buffer_size ( & file) ) ;
259
+ file. read_to_end ( & mut bytes) ?;
259
260
Ok ( bytes)
260
261
}
261
262
@@ -295,8 +296,9 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
295
296
/// ```
296
297
#[ unstable( feature = "fs_read_write" , issue = "46588" ) ]
297
298
pub fn read_string < P : AsRef < Path > > ( path : P ) -> io:: Result < String > {
298
- let mut string = String :: with_capacity ( initial_buffer_size ( & path) ) ;
299
- File :: open ( path) ?. read_to_string ( & mut string) ?;
299
+ let mut file = File :: open ( path) ?;
300
+ let mut string = String :: with_capacity ( initial_buffer_size ( & file) ) ;
301
+ file. read_to_string ( & mut string) ?;
300
302
Ok ( string)
301
303
}
302
304
0 commit comments