Skip to content

Commit 1f9dc9a

Browse files
committed
Auto merge of #80755 - sunfishcode:path-cleanup/copy, r=nagisa
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.
2 parents c87ef0a + 97baac4 commit 1f9dc9a

File tree

1 file changed

+6
-4
lines changed
  • library/std/src/sys_common

1 file changed

+6
-4
lines changed

library/std/src/sys_common/fs.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ use crate::io::{self, Error, ErrorKind};
55
use crate::path::Path;
66

77
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
8-
if !from.is_file() {
8+
let mut reader = fs::File::open(from)?;
9+
let metadata = reader.metadata()?;
10+
11+
if !metadata.is_file() {
912
return Err(Error::new(
1013
ErrorKind::InvalidInput,
1114
"the source path is not an existing regular file",
1215
));
1316
}
1417

15-
let mut reader = fs::File::open(from)?;
1618
let mut writer = fs::File::create(to)?;
17-
let perm = reader.metadata()?.permissions();
19+
let perm = metadata.permissions();
1820

1921
let ret = io::copy(&mut reader, &mut writer)?;
20-
fs::set_permissions(to, perm)?;
22+
writer.set_permissions(perm)?;
2123
Ok(ret)
2224
}
2325

0 commit comments

Comments
 (0)