diff --git a/Cargo.toml b/Cargo.toml index 6711039b..50a8d04a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,5 +63,6 @@ notify = { path = "." } [workspace] members = [ ".", - "examples/hot_reload_tide" + "examples/hot_reload_tide", + "examples/watcher_kind" ] diff --git a/examples/watcher_kind/Cargo.toml b/examples/watcher_kind/Cargo.toml new file mode 100644 index 00000000..59eaa8e0 --- /dev/null +++ b/examples/watcher_kind/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "watcher_kind" +version = "0.1.0" +authors = ["Aron Heinecke "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +notify = { version = "5.0.0-pre.13", path = "../../" } diff --git a/examples/watcher_kind/src/main.rs b/examples/watcher_kind/src/main.rs new file mode 100644 index 00000000..df9743d0 --- /dev/null +++ b/examples/watcher_kind/src/main.rs @@ -0,0 +1,14 @@ +use std::time::Duration; + +use notify::*; +fn main() { + + let (tx, rx) = std::sync::mpsc::channel(); + let watcher: Box = if RecommendedWatcher::kind() == WatcherKind::PollWatcher { + Box::new(PollWatcher::with_delay(tx,Duration::from_secs(1)).unwrap()) + } else { + Box::new(RecommendedWatcher::new(tx).unwrap()) + }; + + +} \ No newline at end of file diff --git a/src/fsevent.rs b/src/fsevent.rs index 48ef9e52..a6bd6a9c 100644 --- a/src/fsevent.rs +++ b/src/fsevent.rs @@ -542,6 +542,10 @@ impl Watcher for FsEventWatcher { self.configure_raw_mode(config, tx); rx.recv()? } + + fn kind() -> crate::WatcherKind { + crate::WatcherKind::Fsevent + } } impl Drop for FsEventWatcher { diff --git a/src/inotify.rs b/src/inotify.rs index 65031b71..3a4c4c9f 100644 --- a/src/inotify.rs +++ b/src/inotify.rs @@ -619,6 +619,10 @@ impl Watcher for INotifyWatcher { self.waker.wake()?; rx.recv()? } + + fn kind() -> crate::WatcherKind { + crate::WatcherKind::Inotify + } } impl Drop for INotifyWatcher { diff --git a/src/kqueue.rs b/src/kqueue.rs index f8616521..648e81c4 100644 --- a/src/kqueue.rs +++ b/src/kqueue.rs @@ -386,6 +386,10 @@ impl Watcher for KqueueWatcher { fn unwatch(&mut self, path: &Path) -> Result<()> { self.unwatch_inner(path) } + + fn kind() -> crate::WatcherKind { + crate::WatcherKind::Kqueue + } } impl Drop for KqueueWatcher { diff --git a/src/lib.rs b/src/lib.rs index 61efdea3..fdbd5760 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -185,6 +185,23 @@ impl EventHandler for std::sync::mpsc::Sender> { } } +/// Watcher kind enumeration +#[derive(Debug,PartialEq,Eq)] +pub enum WatcherKind { + /// inotify backend (linux) + Inotify, + /// FS-Event backend (mac) + Fsevent, + /// KQueue backend (bsd,mac) + Kqueue, + /// Polling based backend (fallback) + PollWatcher, + /// Windows backend + ReadDirectoryChangesWatcher, + /// Fake watcher for testing + NullWatcher, +} + /// Type that can deliver file activity notifications /// /// Watcher is implemented per platform using the best implementation available on that platform. @@ -232,6 +249,9 @@ pub trait Watcher { fn configure(&mut self, _option: Config) -> Result { Ok(false) } + + /// Returns the watcher kind, allowing to perform backend-specific tasks + fn kind() -> WatcherKind where Self: Sized; } /// The recommended `Watcher` implementation for the current platform diff --git a/src/null.rs b/src/null.rs index f8763e3e..fda51771 100644 --- a/src/null.rs +++ b/src/null.rs @@ -22,4 +22,8 @@ impl Watcher for NullWatcher { fn new(event_handler: F) -> Result where Self: Sized { Ok(NullWatcher) } + + fn kind() -> crate::WatcherKind { + crate::WatcherKind::NullWatcher + } } diff --git a/src/poll.rs b/src/poll.rs index 4ca721a6..237976cb 100644 --- a/src/poll.rs +++ b/src/poll.rs @@ -288,6 +288,10 @@ impl Watcher for PollWatcher { fn unwatch(&mut self, path: &Path) -> Result<()> { self.unwatch_inner(path) } + + fn kind() -> crate::WatcherKind { + crate::WatcherKind::PollWatcher + } } impl Drop for PollWatcher { diff --git a/src/windows.rs b/src/windows.rs index 03ea0805..8f9c3ec5 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -15,7 +15,7 @@ use winapi::um::synchapi; use winapi::um::winbase::{self, INFINITE, WAIT_OBJECT_0}; use winapi::um::winnt::{self, FILE_NOTIFY_INFORMATION, HANDLE}; -use crate::event::*; +use crate::{WatcherKind, event::*}; use crate::{Config, Error, EventHandler, RecursiveMode, Result, Watcher}; use crossbeam_channel::{bounded, unbounded, Receiver, Sender}; use std::collections::HashMap; @@ -512,6 +512,10 @@ impl Watcher for ReadDirectoryChangesWatcher { self.tx.send(Action::Configure(config, tx))?; rx.recv()? } + + fn kind() -> crate::WatcherKind { + WatcherKind::ReadDirectoryChangesWatcher + } } impl Drop for ReadDirectoryChangesWatcher {