diff options
| author | Dan Gohman <dev@sunfishcode.online> | 2021-01-05 18:00:38 -0800 |
|---|---|---|
| committer | Dan Gohman <dev@sunfishcode.online> | 2021-01-06 08:36:31 -0800 |
| commit | 97baac4184e3dfa4a98c076bdfe9b88266e47633 (patch) | |
| tree | 12b56e6fe8bb5dfc17ff11399b21134a99ef461c | |
| parent | da305a2b00530aa34dea4e48389204c26fa35dbb (diff) | |
| download | rust-97baac4184e3dfa4a98c076bdfe9b88266e47633.tar.gz rust-97baac4184e3dfa4a98c076bdfe9b88266e47633.zip | |
Optimize away some path lookups in the generic `fs::copy` implementation.
This also eliminates a use of a `Path` convenience function, in support of #80741, refactoring `std::path` to focus on pure data structures and algorithms.
| -rw-r--r-- | library/std/src/sys_common/fs.rs | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/library/std/src/sys_common/fs.rs b/library/std/src/sys_common/fs.rs index e30e8018a31..6bdb26cd078 100644 --- a/library/std/src/sys_common/fs.rs +++ b/library/std/src/sys_common/fs.rs @@ -5,19 +5,21 @@ use crate::io::{self, Error, ErrorKind}; use crate::path::Path; pub fn copy(from: &Path, to: &Path) -> io::Result<u64> { - if !from.is_file() { + let mut reader = fs::File::open(from)?; + let metadata = reader.metadata()?; + + if !metadata.is_file() { return Err(Error::new( ErrorKind::InvalidInput, "the source path is not an existing regular file", )); } - let mut reader = fs::File::open(from)?; let mut writer = fs::File::create(to)?; - let perm = reader.metadata()?.permissions(); + let perm = metadata.permissions(); let ret = io::copy(&mut reader, &mut writer)?; - fs::set_permissions(to, perm)?; + writer.set_permissions(perm)?; Ok(ret) } |
