From 10160613408367176ae3c59b2f78c7ccc50d3387 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Sun, 26 Jul 2015 16:45:57 -0300 Subject: [PATCH 1/6] Replace &Path with AsRef in Watcher functions --- src/fsevent.rs | 8 ++++---- src/inotify/mod.rs | 14 +++++++------- src/lib.rs | 5 +++-- src/null.rs | 4 ++-- src/poll.rs | 8 ++++---- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/fsevent.rs b/src/fsevent.rs index e1a4b41a..9db70223 100644 --- a/src/fsevent.rs +++ b/src/fsevent.rs @@ -219,15 +219,15 @@ impl Watcher for FsEventWatcher { Ok(fsevent) } - fn watch(&mut self, path: &Path) -> Result<(), Error> { + fn watch>(&mut self, path: P) -> Result<(), Error> { self.stop(); - self.append_path(&path.to_str().unwrap()); + self.append_path(&path.as_ref().to_str().unwrap()); self.run() } - fn unwatch(&mut self, path: &Path) -> Result<(), Error> { + fn unwatch>(&mut self, path: P) -> Result<(), Error> { self.stop(); - self.remove_path(&path.to_str().unwrap()); + self.remove_path(&path.as_ref().to_str().unwrap()); // ignore return error: may be empty path list let _ = self.run(); Ok(()) diff --git a/src/inotify/mod.rs b/src/inotify/mod.rs index 3a8525e6..8917a8e9 100644 --- a/src/inotify/mod.rs +++ b/src/inotify/mod.rs @@ -130,13 +130,13 @@ impl Watcher for INotifyWatcher { return Ok(it); } - fn watch(&mut self, path: &Path) -> Result<(), Error> { - let is_dir = match metadata(&path) { + fn watch>(&mut self, path: P) -> Result<(), Error> { + let is_dir = match metadata(&path.as_ref()) { Ok(m) => m.is_dir(), Err(e) => return Err(Error::Io(e)), }; if is_dir { - match Walker::new(path) { + match Walker::new(path.as_ref()) { Ok(dir) => { for entry in dir { match entry { @@ -147,20 +147,20 @@ impl Watcher for INotifyWatcher { Err(e) => return Err(Error::Io(e)), } } - self.add_watch(path) + self.add_watch(path.as_ref()) }, Err(e) => Err(Error::Io(e)) } } else { - self.add_watch(&path) + self.add_watch(&path.as_ref()) } } - fn unwatch(&mut self, path: &Path) -> Result<(), Error> { + fn unwatch>(&mut self, path: P) -> Result<(), Error> { // FIXME: // once Rust 1.1 is released, just use a &Path // Relevant bug is https://github.com/rust-lang/rust/pull/25060 - match self.watches.remove(&path.to_path_buf()) { + match self.watches.remove(&path.as_ref().to_path_buf()) { None => Err(Error::WatchNotFound), Some(p) => { let w = &p.0; diff --git a/src/lib.rs b/src/lib.rs index 32fb1b2a..78da7671 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ pub use self::op::Op; use std::io; use std::path::{Path, PathBuf}; use std::sync::mpsc::Sender; +use std::convert::AsRef; #[cfg(target_os="macos")] pub use self::fsevent::FsEventWatcher; #[cfg(target_os="linux")] pub use self::inotify::INotifyWatcher; @@ -49,8 +50,8 @@ pub enum Error { pub trait Watcher { fn new(Sender) -> Result; - fn watch(&mut self, &Path) -> Result<(), Error>; - fn unwatch(&mut self, &Path) -> Result<(), Error>; + fn watch>(&mut self, P) -> Result<(), Error>; + fn unwatch>(&mut self, P) -> Result<(), Error>; } #[cfg(target_os = "linux")] pub type RecommendedWatcher = INotifyWatcher; diff --git a/src/null.rs b/src/null.rs index e4422315..456c516d 100644 --- a/src/null.rs +++ b/src/null.rs @@ -11,11 +11,11 @@ impl Watcher for NullWatcher { Ok(NullWatcher) } - fn watch(&mut self, path: &Path) -> Result<(), Error> { + fn watch>(&mut self, path: P) -> Result<(), Error> { Ok(()) } - fn unwatch(&mut self, path: &Path) -> Result<(), Error> { + fn unwatch>(&mut self, path: P) -> Result<(), Error> { Ok(()) } } diff --git a/src/poll.rs b/src/poll.rs index 52c2b3f8..5760f1b9 100644 --- a/src/poll.rs +++ b/src/poll.rs @@ -146,13 +146,13 @@ impl Watcher for PollWatcher { Ok(p) } - fn watch(&mut self, path: &Path) -> Result<(), Error> { - (*self.watches).write().unwrap().insert(path.to_path_buf()); + fn watch>(&mut self, path: P) -> Result<(), Error> { + (*self.watches).write().unwrap().insert(path.as_ref().to_path_buf()); Ok(()) } - fn unwatch(&mut self, path: &Path) -> Result<(), Error> { - if (*self.watches).write().unwrap().remove(path) { + fn unwatch>(&mut self, path: P) -> Result<(), Error> { + if (*self.watches).write().unwrap().remove(path.as_ref()) { Ok(()) } else { Err(Error::WatchNotFound) From f91b2feedb003171bbe8ac80c90fb9f420c25246 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Sun, 26 Jul 2015 17:08:49 -0300 Subject: [PATCH 2/6] Remove unnecessary `Path::new` from README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e49fcc8e..8e7017af 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ fn main() { Ok(mut watcher) => { // Add a path to be watched. All files and directories at that path and // below will be monitored for changes. - watcher.watch(&Path::new("/home/test/notify")); + watcher.watch("/home/test/notify"); // You'll probably want to do that in a loop. The type to match for is // notify::Event, look at src/lib.rs for details. From 1840b50d72523b00803643ceb6b65079d66da366 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Sun, 26 Jul 2015 17:31:25 -0300 Subject: [PATCH 3/6] Replace another instance of &Path by AsRef --- src/inotify/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/inotify/mod.rs b/src/inotify/mod.rs index 8917a8e9..1831bca6 100644 --- a/src/inotify/mod.rs +++ b/src/inotify/mod.rs @@ -49,7 +49,7 @@ impl INotifyWatcher { }); } - fn add_watch(&mut self, path: &Path) -> Result<(), Error> { + fn add_watch>(&mut self, path: P) -> Result<(), Error> { let mut watching = flags::IN_ATTRIB | flags::IN_CREATE | flags::IN_DELETE @@ -58,7 +58,7 @@ impl INotifyWatcher { | flags::IN_MOVED_FROM | flags::IN_MOVED_TO | flags::IN_MOVE_SELF; - let path = path.to_path_buf(); + let path = path.as_ref().to_path_buf(); match self.watches.get(&path) { None => {}, Some(p) => { From 02f6663865baab0ebf40e065fbd1f46c5bf5501e Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Sun, 26 Jul 2015 17:31:56 -0300 Subject: [PATCH 4/6] Remove another unnecessary Path::new --- src/fsevent.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fsevent.rs b/src/fsevent.rs index 9db70223..2d7d901d 100644 --- a/src/fsevent.rs +++ b/src/fsevent.rs @@ -252,12 +252,12 @@ fn test_fsevent_watcher_drop() { { let mut watcher: RecommendedWatcher = Watcher::new(tx).unwrap(); - watcher.watch(&Path::new("../../")).unwrap(); + watcher.watch("../../").unwrap(); thread::sleep_ms(2_000); println!("is running -> {}", watcher.is_running()); thread::sleep_ms(1_000); - watcher.unwatch(&Path::new("../..")).unwrap(); + watcher.unwatch("../..").unwrap(); println!("is running -> {}", watcher.is_running()); } From f4386d5fe7bd1b8c01257332ffde7816ef6265f0 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Mon, 27 Jul 2015 00:43:56 -0300 Subject: [PATCH 5/6] Add my name to authors list --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 46519e94..491303bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ authors = [ "Pierre Baillet ", "ShuYu Wang ", "Jimmy Lu ", + "Francisco Giordano ", ] description = "Cross-platform filesystem notification library" From bb3c97883edecc90c74c9dff7b91baed67aa2964 Mon Sep 17 00:00:00 2001 From: Francisco Giordano Date: Mon, 27 Jul 2015 01:14:13 -0300 Subject: [PATCH 6/6] Revert to borrowing the path instead of moving it --- src/fsevent.rs | 4 ++-- src/inotify/mod.rs | 6 +++--- src/lib.rs | 4 ++-- src/null.rs | 4 ++-- src/poll.rs | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/fsevent.rs b/src/fsevent.rs index 2d7d901d..cea1c965 100644 --- a/src/fsevent.rs +++ b/src/fsevent.rs @@ -219,13 +219,13 @@ impl Watcher for FsEventWatcher { Ok(fsevent) } - fn watch>(&mut self, path: P) -> Result<(), Error> { + fn watch + ?Sized>(&mut self, path: &P) -> Result<(), Error> { self.stop(); self.append_path(&path.as_ref().to_str().unwrap()); self.run() } - fn unwatch>(&mut self, path: P) -> Result<(), Error> { + fn unwatch + ?Sized>(&mut self, path: &P) -> Result<(), Error> { self.stop(); self.remove_path(&path.as_ref().to_str().unwrap()); // ignore return error: may be empty path list diff --git a/src/inotify/mod.rs b/src/inotify/mod.rs index 1831bca6..525ede10 100644 --- a/src/inotify/mod.rs +++ b/src/inotify/mod.rs @@ -49,7 +49,7 @@ impl INotifyWatcher { }); } - fn add_watch>(&mut self, path: P) -> Result<(), Error> { + fn add_watch + ?Sized>(&mut self, path: &P) -> Result<(), Error> { let mut watching = flags::IN_ATTRIB | flags::IN_CREATE | flags::IN_DELETE @@ -130,7 +130,7 @@ impl Watcher for INotifyWatcher { return Ok(it); } - fn watch>(&mut self, path: P) -> Result<(), Error> { + fn watch + ?Sized>(&mut self, path: &P) -> Result<(), Error> { let is_dir = match metadata(&path.as_ref()) { Ok(m) => m.is_dir(), Err(e) => return Err(Error::Io(e)), @@ -156,7 +156,7 @@ impl Watcher for INotifyWatcher { } } - fn unwatch>(&mut self, path: P) -> Result<(), Error> { + fn unwatch + ?Sized>(&mut self, path: &P) -> Result<(), Error> { // FIXME: // once Rust 1.1 is released, just use a &Path // Relevant bug is https://github.com/rust-lang/rust/pull/25060 diff --git a/src/lib.rs b/src/lib.rs index 78da7671..53530409 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,8 +50,8 @@ pub enum Error { pub trait Watcher { fn new(Sender) -> Result; - fn watch>(&mut self, P) -> Result<(), Error>; - fn unwatch>(&mut self, P) -> Result<(), Error>; + fn watch + ?Sized>(&mut self, &P) -> Result<(), Error>; + fn unwatch + ?Sized>(&mut self, &P) -> Result<(), Error>; } #[cfg(target_os = "linux")] pub type RecommendedWatcher = INotifyWatcher; diff --git a/src/null.rs b/src/null.rs index 456c516d..b0621f4b 100644 --- a/src/null.rs +++ b/src/null.rs @@ -11,11 +11,11 @@ impl Watcher for NullWatcher { Ok(NullWatcher) } - fn watch>(&mut self, path: P) -> Result<(), Error> { + fn watch + ?Sized>(&mut self, path: &P) -> Result<(), Error> { Ok(()) } - fn unwatch>(&mut self, path: P) -> Result<(), Error> { + fn unwatch + ?Sized>(&mut self, path: &P) -> Result<(), Error> { Ok(()) } } diff --git a/src/poll.rs b/src/poll.rs index 5760f1b9..3f3462fb 100644 --- a/src/poll.rs +++ b/src/poll.rs @@ -146,12 +146,12 @@ impl Watcher for PollWatcher { Ok(p) } - fn watch>(&mut self, path: P) -> Result<(), Error> { + fn watch + ?Sized>(&mut self, path: &P) -> Result<(), Error> { (*self.watches).write().unwrap().insert(path.as_ref().to_path_buf()); Ok(()) } - fn unwatch>(&mut self, path: P) -> Result<(), Error> { + fn unwatch + ?Sized>(&mut self, path: &P) -> Result<(), Error> { if (*self.watches).write().unwrap().remove(path.as_ref()) { Ok(()) } else {