Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Path with AsRef<Path> in Watcher functions #25

Merged
merged 6 commits into from
Jul 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ authors = [
"Pierre Baillet <oct@zoy.org>",
"ShuYu Wang <andelf@gmail.com>",
"Jimmy Lu <gongchuo.lu@gmail.com>",
"Francisco Giordano <frangio.1@gmail.com>",
]

description = "Cross-platform filesystem notification library"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions src/fsevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,15 @@ impl Watcher for FsEventWatcher {
Ok(fsevent)
}

fn watch(&mut self, path: &Path) -> Result<(), Error> {
fn watch<P: AsRef<Path> + ?Sized>(&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<P: AsRef<Path> + ?Sized>(&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(())
Expand All @@ -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());
}

Expand Down
18 changes: 9 additions & 9 deletions src/inotify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl INotifyWatcher {
});
}

fn add_watch(&mut self, path: &Path) -> Result<(), Error> {
fn add_watch<P: AsRef<Path> + ?Sized>(&mut self, path: &P) -> Result<(), Error> {
let mut watching = flags::IN_ATTRIB
| flags::IN_CREATE
| flags::IN_DELETE
Expand All @@ -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) => {
Expand Down Expand Up @@ -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<P: AsRef<Path> + ?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)),
};
if is_dir {
match Walker::new(path) {
match Walker::new(path.as_ref()) {
Ok(dir) => {
for entry in dir {
match entry {
Expand All @@ -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<P: AsRef<Path> + ?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
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;
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,8 +50,8 @@ pub enum Error {

pub trait Watcher {
fn new(Sender<Event>) -> Result<Self, Error>;
fn watch(&mut self, &Path) -> Result<(), Error>;
fn unwatch(&mut self, &Path) -> Result<(), Error>;
fn watch<P: AsRef<Path> + ?Sized>(&mut self, &P) -> Result<(), Error>;
fn unwatch<P: AsRef<Path> + ?Sized>(&mut self, &P) -> Result<(), Error>;
}

#[cfg(target_os = "linux")] pub type RecommendedWatcher = INotifyWatcher;
Expand Down
4 changes: 2 additions & 2 deletions src/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ impl Watcher for NullWatcher {
Ok(NullWatcher)
}

fn watch(&mut self, path: &Path) -> Result<(), Error> {
fn watch<P: AsRef<Path> + ?Sized>(&mut self, path: &P) -> Result<(), Error> {
Ok(())
}

fn unwatch(&mut self, path: &Path) -> Result<(), Error> {
fn unwatch<P: AsRef<Path> + ?Sized>(&mut self, path: &P) -> Result<(), Error> {
Ok(())
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsRef<Path> + ?Sized>(&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<P: AsRef<Path> + ?Sized>(&mut self, path: &P) -> Result<(), Error> {
if (*self.watches).write().unwrap().remove(path.as_ref()) {
Ok(())
} else {
Err(Error::WatchNotFound)
Expand Down