From 17411be41b96f4a81df8a9cc6fa558d0d250c749 Mon Sep 17 00:00:00 2001 From: TD-Sky Date: Thu, 2 Nov 2023 09:56:01 +0800 Subject: [PATCH] accepting generic type instead of `&Path` --- src/freedesktop.rs | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/freedesktop.rs b/src/freedesktop.rs index c7facc7..04c46cf 100644 --- a/src/freedesktop.rs +++ b/src/freedesktop.rs @@ -324,17 +324,15 @@ where /// This function executes `op` providing it with a /// trash-folder path that's associated with the partition mounted at `topdir`. /// -fn execute_on_mounted_trash_folders( +fn execute_on_mounted_trash_folders Result<(), FsError>>( uid: u32, - topdir: &Path, + topdir: impl AsRef, first_only: bool, create_folder: bool, mut op: F, -) -> Result<(), FsError> -where - F: FnMut(PathBuf) -> Result<(), FsError>, -{ +) -> Result<(), FsError> { // See if there's a ".Trash" directory at the mounted location + let topdir = topdir.as_ref(); let trash_path = topdir.join(".Trash"); if trash_path.is_dir() { let validity = folder_validity(&trash_path)?; @@ -463,12 +461,16 @@ fn move_to_trash( Ok(()) } -fn execute_src_to_dst_operation( - src: impl AsRef, - dst: impl AsRef, +fn execute_src_to_dst_operation( + src: S1, + dst: D1, dir: &'static dyn Fn(&Path) -> Result<(), FsError>, file: &'static dyn Fn(&Path, &Path) -> Result<(), FsError>, -) -> Result<(), FsError> { +) -> Result<(), FsError> +where + S1: AsRef, + D1: AsRef, +{ let src = src.as_ref(); let dst = dst.as_ref(); @@ -492,7 +494,10 @@ fn execute_src_to_dst_operation( } /// An error may mean that a collision was found. -fn move_items_no_replace(src: &Path, dst: &Path) -> Result<(), FsError> { +fn move_items_no_replace(src: impl AsRef, dst: impl AsRef) -> Result<(), FsError> { + let src = src.as_ref(); + let dst = dst.as_ref(); + try_creating_placeholders(src, dst)?; // All placeholders are in place. LET'S OVERWRITE @@ -512,7 +517,9 @@ fn move_items_no_replace(src: &Path, dst: &Path) -> Result<(), FsError> { Ok(()) } -fn try_creating_placeholders(src: &Path, dst: &Path) -> Result<(), FsError> { +fn try_creating_placeholders(src: impl AsRef, dst: impl AsRef) -> Result<(), FsError> { + let src = src.as_ref(); + let dst = dst.as_ref(); let metadata = src.symlink_metadata().map_err(|e| (src.to_owned(), e))?; if metadata.is_dir() { // NOTE create_dir fails if the directory already exists @@ -542,11 +549,12 @@ enum TrashValidity { InvalidNotSticky, } -fn folder_validity(path: &Path) -> Result { +fn folder_validity(path: impl AsRef) -> Result { /// Mask for the sticky bit /// Taken from: http://man7.org/linux/man-pages/man7/inode.7.html const S_ISVTX: u32 = 0x1000; + let path = path.as_ref(); let metadata = path.symlink_metadata().map_err(|e| (path.to_owned(), e))?; if metadata.file_type().is_symlink() { return Ok(TrashValidity::InvalidSymlink);