Skip to content

Commit 42d6922

Browse files
jyn514Joshua Nelson
authored and
Joshua Nelson
committed
refactor copy_doc_dir to use less allocation
1 parent f8cd9bb commit 42d6922

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/utils/copy.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11

2-
// FIXME: There is so many PathBuf's in this module
3-
// Conver them to Path
4-
5-
use std::path::{Path, PathBuf};
2+
use std::path::Path;
63
use std::fs;
74
use error::Result;
85

@@ -13,12 +10,12 @@ use regex::Regex;
1310
/// Target directory must have doc directory.
1411
///
1512
/// This function is designed to avoid file duplications.
16-
pub fn copy_doc_dir<P: AsRef<Path>>(source: P, destination: P) -> Result<()> {
17-
let destination = destination.as_ref().to_path_buf();
13+
pub fn copy_doc_dir<P: AsRef<Path>, Q: AsRef<Path>>(source: P, destination: Q) -> Result<()> {
14+
let destination = destination.as_ref();
1815

1916
// Make sure destination directory exists
2017
if !destination.exists() {
21-
fs::create_dir_all(&destination)?;
18+
fs::create_dir_all(destination)?;
2219
}
2320

2421
// Avoid copying common files
@@ -29,8 +26,7 @@ pub fn copy_doc_dir<P: AsRef<Path>>(source: P, destination: P) -> Result<()> {
2926
for file in source.as_ref().read_dir()? {
3027

3128
let file = file?;
32-
let mut destination_full_path = PathBuf::from(&destination);
33-
destination_full_path.push(file.file_name());
29+
let destination_full_path = destination.join(file.file_name());
3430

3531
let metadata = file.metadata()?;
3632

@@ -61,13 +57,18 @@ mod test {
6157
let destination = tempdir::TempDir::new("cratesfyi-dst").unwrap();
6258
let doc = source.path().join("doc");
6359
fs::create_dir(&doc).unwrap();
60+
fs::create_dir(doc.join("inner")).unwrap();
6461

6562
fs::write(doc.join("index.html"), "<html>spooky</html>").unwrap();
6663
fs::write(doc.join("index.txt"), "spooky").unwrap();
64+
fs::write(doc.join("inner").join("index.html"), "<html>spooky</html>").unwrap();
65+
fs::write(doc.join("inner").join("index.txt"), "spooky").unwrap();
6766

6867
// lets try to copy a src directory to tempdir
69-
copy_doc_dir(source.path(), destination.path()).unwrap();
68+
copy_doc_dir(source.path().join("doc"), destination.path()).unwrap();
7069
assert!(destination.path().join("index.html").exists());
7170
assert!(!destination.path().join("index.txt").exists());
71+
assert!(destination.path().join("inner").join("index.html").exists());
72+
assert!(!destination.path().join("inner").join("index.txt").exists());
7273
}
7374
}

0 commit comments

Comments
 (0)