diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-06-18 00:50:06 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-06-18 00:50:06 +0800 |
| commit | d2e4cb85af2c6101cd435c10cfd3d4084cd83e76 (patch) | |
| tree | 4458b402f79d9f0914f76c7e39d9521c9a5a9cd0 /rola-utils/functions | |
| parent | c796e3f93183fc1fdbd4aa68976fceb339aff8d8 (diff) | |
refactor: use `io::Error::other` and simplify closures
Diffstat (limited to 'rola-utils/functions')
| -rw-r--r-- | rola-utils/functions/src/copy_with_temp_rename/async.rs | 31 | ||||
| -rw-r--r-- | rola-utils/functions/src/copy_with_temp_rename/sync.rs | 31 | ||||
| -rw-r--r-- | rola-utils/functions/src/test_sandbox.rs | 4 |
3 files changed, 26 insertions, 40 deletions
diff --git a/rola-utils/functions/src/copy_with_temp_rename/async.rs b/rola-utils/functions/src/copy_with_temp_rename/async.rs index 83275a1..33f7693 100644 --- a/rola-utils/functions/src/copy_with_temp_rename/async.rs +++ b/rola-utils/functions/src/copy_with_temp_rename/async.rs @@ -35,29 +35,23 @@ async fn internal_copy_with_temp_rename(src: &Path, dst: &Path) -> io::Result<() } } (Err(e), _) | (_, Err(e)) => { - return Err(io::Error::new( - io::ErrorKind::Other, - format!( - "failed to canonicalize paths (src: {}, dst: {}): {}", - src.display(), - dst.display(), - e - ), - )); + return Err(io::Error::other(format!( + "failed to canonicalize paths (src: {}, dst: {}): {}", + src.display(), + dst.display(), + e + ))); } } } else { // dst doesn't exist yet, so it cannot be the same file as src // If src canonicalization fails, propagate the error let _ = src_canonical.map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!( - "failed to canonicalize source path (src: {}): {}", - src.display(), - e - ), - ) + io::Error::other(format!( + "failed to canonicalize source path (src: {}): {}", + src.display(), + e + )) })?; } @@ -215,8 +209,7 @@ async fn atomic_create_temp(dir: &Path, base: &str) -> io::Result<PathBuf> { } } - Err(io::Error::new( - io::ErrorKind::Other, + Err(io::Error::other( "exceeded max attempts to create temp file", )) } diff --git a/rola-utils/functions/src/copy_with_temp_rename/sync.rs b/rola-utils/functions/src/copy_with_temp_rename/sync.rs index cdbe3db..18e0fcf 100644 --- a/rola-utils/functions/src/copy_with_temp_rename/sync.rs +++ b/rola-utils/functions/src/copy_with_temp_rename/sync.rs @@ -28,29 +28,23 @@ fn internal_copy_with_temp_rename(src: &Path, dst: &Path) -> io::Result<()> { } } (Err(e), _) | (_, Err(e)) => { - return Err(io::Error::new( - io::ErrorKind::Other, - format!( - "failed to canonicalize paths (src: {}, dst: {}): {}", - src.display(), - dst.display(), - e - ), - )); + return Err(io::Error::other(format!( + "failed to canonicalize paths (src: {}, dst: {}): {}", + src.display(), + dst.display(), + e + ))); } } } else { // dst doesn't exist yet, so it cannot be the same file as src // If src canonicalization fails, propagate the error let _ = src_canonical.map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!( - "failed to canonicalize source path (src: {}): {}", - src.display(), - e - ), - ) + io::Error::other(format!( + "failed to canonicalize source path (src: {}): {}", + src.display(), + e + )) })?; } @@ -202,8 +196,7 @@ fn atomic_create_temp(dir: &Path, base: &str) -> io::Result<PathBuf> { } } - Err(io::Error::new( - io::ErrorKind::Other, + Err(io::Error::other( "exceeded max attempts to create temp file", )) } diff --git a/rola-utils/functions/src/test_sandbox.rs b/rola-utils/functions/src/test_sandbox.rs index e0065f6..2705389 100644 --- a/rola-utils/functions/src/test_sandbox.rs +++ b/rola-utils/functions/src/test_sandbox.rs @@ -73,9 +73,9 @@ fn try_clear_contents(path: &Path) -> std::io::Result<()> { for entry in fs::read_dir(path)? { let entry = entry?; if entry.file_type()?.is_dir() { - fs::remove_dir_all(&entry.path())?; + fs::remove_dir_all(entry.path())?; } else { - fs::remove_file(&entry.path())?; + fs::remove_file(entry.path())?; } } } |
