Skip to content

Commit 564c569

Browse files
committed
Monomorphize less code in fs::{read|write}
Since the generic-ness is only for the as_refs, might as well have std just compile the important part once instead of on every use.
1 parent 16ca0b9 commit 564c569

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/libstd/fs.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,13 @@ fn initial_buffer_size(file: &File) -> usize {
254254
/// ```
255255
#[stable(feature = "fs_read_write_bytes", since = "1.26.0")]
256256
pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
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)?;
260-
Ok(bytes)
257+
fn inner(path: &Path) -> io::Result<Vec<u8>> {
258+
let mut file = File::open(path)?;
259+
let mut bytes = Vec::with_capacity(initial_buffer_size(&file));
260+
file.read_to_end(&mut bytes)?;
261+
Ok(bytes)
262+
}
263+
inner(path.as_ref())
261264
}
262265

263266
/// Read the entire contents of a file into a string.
@@ -296,10 +299,13 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
296299
/// ```
297300
#[stable(feature = "fs_read_write", since = "1.26.0")]
298301
pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<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)?;
302-
Ok(string)
302+
fn inner(path: &Path) -> io::Result<String> {
303+
let mut file = File::open(path)?;
304+
let mut string = String::with_capacity(initial_buffer_size(&file));
305+
file.read_to_string(&mut string)?;
306+
Ok(string)
307+
}
308+
inner(path.as_ref())
303309
}
304310

305311
/// Write a slice as the entire contents of a file.
@@ -326,7 +332,10 @@ pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
326332
/// ```
327333
#[stable(feature = "fs_read_write_bytes", since = "1.26.0")]
328334
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
329-
File::create(path)?.write_all(contents.as_ref())
335+
fn inner(path: &Path, contents: &[u8]) -> io::Result<()> {
336+
File::create(path)?.write_all(contents)
337+
}
338+
inner(path.as_ref(), contents.as_ref())
330339
}
331340

332341
impl File {

0 commit comments

Comments
 (0)