From c9d8e72d30c4ab951bb98e74c8694586a38b7840 Mon Sep 17 00:00:00 2001 From: Julien Wajsberg Date: Fri, 3 Jun 2016 18:15:46 +0200 Subject: [PATCH] Add a Result alias to allow for an easier use in lib users --- src/fsevent.rs | 10 +++++----- src/inotify/mod.rs | 18 +++++++++--------- src/lib.rs | 13 ++++++++----- src/null.rs | 8 ++++---- src/poll.rs | 10 +++++----- src/windows.rs | 20 ++++++++++---------- tests/notify.rs | 16 ++++++++-------- 7 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/fsevent.rs b/src/fsevent.rs index 2b4f45e9..32f18f59 100644 --- a/src/fsevent.rs +++ b/src/fsevent.rs @@ -12,7 +12,7 @@ use std::convert::AsRef; use std::thread; use std::sync::mpsc::{channel, Sender, Receiver}; -use super::{Error, Event, op, Watcher}; +use super::{Error, Event, op, Result, Watcher}; use std::path::{Path, PathBuf}; use libc; @@ -104,7 +104,7 @@ impl FsEventWatcher { } } - pub fn run(&mut self) -> Result<(), Error> { + pub fn run(&mut self) -> Result<()> { if unsafe { cf::CFArrayGetCount(self.paths) } == 0 { return Err(Error::PathNotFound); } @@ -206,7 +206,7 @@ pub unsafe extern "C" fn callback( impl Watcher for FsEventWatcher { - fn new(tx: Sender) -> Result { + fn new(tx: Sender) -> Result { Ok(FsEventWatcher { paths: unsafe { cf::CFArrayCreateMutable(cf::kCFAllocatorDefault, 0, &cf::kCFTypeArrayCallBacks) @@ -220,13 +220,13 @@ impl Watcher for FsEventWatcher { }) } - fn watch>(&mut self, path: P) -> Result<(), Error> { + fn watch>(&mut self, path: P) -> Result<()> { self.stop(); self.append_path(&path.as_ref().to_str().unwrap()); self.run() } - fn unwatch>(&mut self, path: P) -> Result<(), Error> { + fn unwatch>(&mut self, path: P) -> Result<()> { 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 6641bc58..5ba3d97a 100644 --- a/src/inotify/mod.rs +++ b/src/inotify/mod.rs @@ -11,7 +11,7 @@ use std::path::{Path, PathBuf}; use std::sync::mpsc::{self, Sender}; use std::sync::{Arc, RwLock}; use std::thread::Builder as ThreadBuilder; -use super::{Error, Event, op, Op, Watcher}; +use super::{Error, Event, op, Op, Result, Watcher}; mod flags; @@ -27,8 +27,8 @@ struct INotifyHandler { } enum EventLoopMsg { - AddWatch(PathBuf, Sender>), - RemoveWatch(PathBuf, Sender>), + AddWatch(PathBuf, Sender>), + RemoveWatch(PathBuf, Sender>), Shutdown, } @@ -85,7 +85,7 @@ impl mio::Handler for INotifyHandler { } impl INotifyHandler { - fn add_watch_recursively(&mut self, path: PathBuf) -> Result<(), Error> { + fn add_watch_recursively(&mut self, path: PathBuf) -> Result<()> { match metadata(&path) { Err(e) => return Err(Error::Io(e)), Ok(m) => { @@ -105,7 +105,7 @@ impl INotifyHandler { Ok(()) } - fn add_watch(&mut self, path: PathBuf) -> Result<(), Error> { + fn add_watch(&mut self, path: PathBuf) -> Result<()> { let mut watching = flags::IN_ATTRIB | flags::IN_CREATE | flags::IN_DELETE | flags::IN_DELETE_SELF | flags::IN_MODIFY | flags::IN_MOVED_FROM | @@ -126,7 +126,7 @@ impl INotifyHandler { } } - fn remove_watch(&mut self, path: PathBuf) -> Result<(), Error> { + fn remove_watch(&mut self, path: PathBuf) -> Result<()> { match self.watches.remove(&path) { None => Err(Error::WatchNotFound), Some(p) => { @@ -182,7 +182,7 @@ fn handle_event(event: wrapper::Event, } impl Watcher for INotifyWatcher { - fn new(tx: Sender) -> Result { + fn new(tx: Sender) -> Result { INotify::init() .and_then(|inotify| EventLoop::new().map(|l| (inotify, l))) .and_then(|(inotify, mut event_loop)| { @@ -215,7 +215,7 @@ impl Watcher for INotifyWatcher { .map_err(Error::Io) } - fn watch>(&mut self, path: P) -> Result<(), Error> { + fn watch>(&mut self, path: P) -> Result<()> { let (tx, rx) = mpsc::channel(); let msg = EventLoopMsg::AddWatch(path.as_ref().to_owned(), tx); @@ -224,7 +224,7 @@ impl Watcher for INotifyWatcher { rx.recv().unwrap() } - fn unwatch>(&mut self, path: P) -> Result<(), Error> { + fn unwatch>(&mut self, path: P) -> Result<()> { let (tx, rx) = mpsc::channel(); let msg = EventLoopMsg::RemoveWatch(path.as_ref().to_owned(), tx); diff --git a/src/lib.rs b/src/lib.rs index 0d7d16cc..0345e215 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,7 @@ use std::sync::mpsc::Sender; use std::convert::AsRef; use std::fmt; use std::error::Error as StdError; +use std::result::Result as StdResult; #[cfg(target_os="macos")] pub use self::fsevent::FsEventWatcher; @@ -49,7 +50,7 @@ pub mod op { #[derive(Debug)] pub struct Event { pub path: Option, - pub op: Result, + pub op: Result, } unsafe impl Send for Event {} @@ -77,10 +78,12 @@ impl fmt::Display for Error { } } +pub type Result = StdResult; + pub trait Watcher: Sized { - fn new(Sender) -> Result; - fn watch>(&mut self, P) -> Result<(), Error>; - fn unwatch>(&mut self, P) -> Result<(), Error>; + fn new(Sender) -> Result; + fn watch>(&mut self, P) -> Result<()>; + fn unwatch>(&mut self, P) -> Result<()>; } #[cfg(target_os = "linux")] @@ -92,7 +95,7 @@ pub type RecommendedWatcher = ReadDirectoryChangesWatcher; #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))] pub type RecommendedWatcher = PollWatcher; -pub fn new(tx: Sender) -> Result { +pub fn new(tx: Sender) -> Result { Watcher::new(tx) } diff --git a/src/null.rs b/src/null.rs index 79c0a05d..a1417d80 100644 --- a/src/null.rs +++ b/src/null.rs @@ -2,20 +2,20 @@ use std::sync::mpsc::Sender; use std::path::Path; -use super::{Error, Event, Watcher}; +use super::{Event, Result, Watcher}; pub struct NullWatcher; impl Watcher for NullWatcher { - fn new(tx: Sender) -> Result { + fn new(tx: Sender) -> Result { Ok(NullWatcher) } - fn watch>(&mut self, path: P) -> Result<(), Error> { + fn watch>(&mut self, path: P) -> Result<()> { Ok(()) } - fn unwatch>(&mut self, path: P) -> Result<(), Error> { + fn unwatch>(&mut self, path: P) -> Result<()> { Ok(()) } } diff --git a/src/poll.rs b/src/poll.rs index 1a3372d2..955a154e 100644 --- a/src/poll.rs +++ b/src/poll.rs @@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock}; use std::sync::mpsc::Sender; use std::fs; use std::thread; -use super::{Error, Event, op, Watcher}; +use super::{Error, Event, op, Result, Watcher}; use std::path::{Path, PathBuf}; use std::time::Duration; use self::walkdir::WalkDir; @@ -19,7 +19,7 @@ pub struct PollWatcher { } impl PollWatcher { - pub fn with_delay(tx: Sender, delay: u32) -> Result { + pub fn with_delay(tx: Sender, delay: u32) -> Result { let mut p = PollWatcher { tx: tx, watches: Arc::new(RwLock::new(HashSet::new())), @@ -130,16 +130,16 @@ impl PollWatcher { } impl Watcher for PollWatcher { - fn new(tx: Sender) -> Result { + fn new(tx: Sender) -> Result { PollWatcher::with_delay(tx, 10) } - fn watch>(&mut self, path: P) -> Result<(), Error> { + fn watch>(&mut self, path: P) -> Result<()> { (*self.watches).write().unwrap().insert(path.as_ref().to_path_buf()); Ok(()) } - fn unwatch>(&mut self, path: P) -> Result<(), Error> { + fn unwatch>(&mut self, path: P) -> Result<()> { if (*self.watches).write().unwrap().remove(path.as_ref()) { Ok(()) } else { diff --git a/src/windows.rs b/src/windows.rs index b698acaf..d1130af0 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -15,7 +15,7 @@ use std::os::windows::ffi::{OsStrExt, OsStringExt}; use std::thread; use std::os::raw::c_void; -use super::{Event, Error, op, Op, Watcher}; +use super::{Event, Error, op, Op, Result, Watcher}; const BUF_SIZE: u32 = 16384; @@ -53,7 +53,7 @@ struct ReadDirectoryChangesServer { rx: Receiver, tx: Sender, meta_tx: Sender, - cmd_tx: Sender>, + cmd_tx: Sender>, watches: HashMap, wakeup_sem: HANDLE, } @@ -61,7 +61,7 @@ struct ReadDirectoryChangesServer { impl ReadDirectoryChangesServer { fn start(event_tx: Sender, meta_tx: Sender, - cmd_tx: Sender>, + cmd_tx: Sender>, wakeup_sem: HANDLE) -> Sender { @@ -124,7 +124,7 @@ impl ReadDirectoryChangesServer { } } - fn add_watch(&mut self, path: PathBuf) -> Result { + fn add_watch(&mut self, path: PathBuf) -> Result { // path must exist and be either a file or directory if !path.is_dir() && !path.is_file() { return Err(Error::Generic("Input watch path is neither a file nor a directory." @@ -331,14 +331,14 @@ unsafe extern "system" fn handle_event(error_code: u32, pub struct ReadDirectoryChangesWatcher { tx: Sender, - cmd_rx: Receiver>, + cmd_rx: Receiver>, wakeup_sem: HANDLE, } impl ReadDirectoryChangesWatcher { pub fn create(event_tx: Sender, meta_tx: Sender) - -> Result { + -> Result { let (cmd_tx, cmd_rx) = channel(); let wakeup_sem = unsafe { @@ -366,7 +366,7 @@ impl ReadDirectoryChangesWatcher { } } - fn send_action_require_ack(&mut self, action: Action, pb: &PathBuf) -> Result<(), Error> { + fn send_action_require_ack(&mut self, action: Action, pb: &PathBuf) -> Result<()> { match self.tx.send(action) { Err(_) => Err(Error::Generic("Error sending to internal channel".to_owned())), Ok(_) => { @@ -399,13 +399,13 @@ impl ReadDirectoryChangesWatcher { } impl Watcher for ReadDirectoryChangesWatcher { - fn new(event_tx: Sender) -> Result { + fn new(event_tx: Sender) -> Result { // create dummy channel for meta event let (meta_tx, _) = channel(); ReadDirectoryChangesWatcher::create(event_tx, meta_tx) } - fn watch>(&mut self, path: P) -> Result<(), Error> { + fn watch>(&mut self, path: P) -> Result<()> { // path must exist and be either a file or directory let pb = path.as_ref().to_path_buf(); if !pb.is_dir() && !pb.is_file() { @@ -415,7 +415,7 @@ impl Watcher for ReadDirectoryChangesWatcher { self.send_action_require_ack(Action::Watch(path.as_ref().to_path_buf()), &pb) } - fn unwatch>(&mut self, path: P) -> Result<(), Error> { + fn unwatch>(&mut self, path: P) -> Result<()> { let res = self.tx .send(Action::Unwatch(path.as_ref().to_path_buf())) .map_err(|_| Error::Generic("Error sending to internal channel".to_owned())); diff --git a/tests/notify.rs b/tests/notify.rs index df8b88ed..67eac9ed 100644 --- a/tests/notify.rs +++ b/tests/notify.rs @@ -93,7 +93,7 @@ fn validate_recv(rx: Receiver, evs: Vec<(&Path, Op)>) -> Vec { #[cfg(target_os = "windows")] // Windows needs to test this differently since it can't watch files that don't exist yet. fn validate_watch_single_file(ctor: F) where - F: Fn(Sender) -> Result, W: Watcher { + F: Fn(Sender) -> Result, W: Watcher { let (tx, rx) = channel(); let mut w = ctor(tx).unwrap(); @@ -127,7 +127,7 @@ fn validate_watch_single_file(ctor: F) where #[cfg(not(target_os = "windows"))] fn validate_watch_single_file(ctor: F) where - F: Fn(Sender) -> Result, W: Watcher { + F: Fn(Sender) -> Result, W: Watcher { let mut file = NamedTempFile::new().unwrap(); let (tx, rx) = channel(); let mut w = ctor(tx).unwrap(); @@ -141,7 +141,7 @@ fn validate_watch_single_file(ctor: F) where #[cfg(not(target_os = "linux"))] fn validate_watch_dir(ctor: F) where - F: Fn(Sender) -> Result, W: Watcher { + F: Fn(Sender) -> Result, W: Watcher { let dir = TempDir::new("dir").unwrap(); let dir1 = TempDir::new_in(dir.path(), "dir1").unwrap(); let dir2 = TempDir::new_in(dir.path(), "dir2").unwrap(); @@ -191,7 +191,7 @@ fn watch_single_file_poll() { #[test] fn new_inotify() { let (tx, _) = channel(); - let w: Result = Watcher::new(tx); + let w: Result = Watcher::new(tx); match w { Ok(_) => assert!(true), Err(_) => assert!(false) @@ -202,7 +202,7 @@ fn new_inotify() { #[test] fn new_fsevent() { let (tx, _) = channel(); - let w: Result = Watcher::new(tx); + let w: Result = Watcher::new(tx); match w { Ok(_) => assert!(true), Err(_) => assert!(false) @@ -212,7 +212,7 @@ fn new_fsevent() { #[test] fn new_null() { let (tx, _) = channel(); - let w: Result = Watcher::new(tx); + let w: Result = Watcher::new(tx); match w { Ok(_) => assert!(true), Err(_) => assert!(false) @@ -222,7 +222,7 @@ fn new_null() { #[test] fn new_poll() { let (tx, _) = channel(); - let w: Result = Watcher::new(tx); + let w: Result = Watcher::new(tx); match w { Ok(_) => assert!(true), Err(_) => assert!(false) @@ -232,7 +232,7 @@ fn new_poll() { #[test] fn new_recommended() { let (tx, _) = channel(); - let w: Result = Watcher::new(tx); + let w: Result = Watcher::new(tx); match w { Ok(_) => assert!(true), Err(_) => assert!(false)