Skip to content

Commit

Permalink
Add Watcher::kind()
Browse files Browse the repository at this point in the history
Note that non-boxing would require an enum that matches the CFG chaos of RecommendedWatcher, adding an enum for every possible configurable watcher combination. And otherwise we'll have to add the specific config calls to the trait.

Closes #361
  • Loading branch information
0xpr03 committed Sep 30, 2021
1 parent ff1a64c commit db9841a
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -63,5 +63,6 @@ notify = { path = "." }
[workspace]
members = [
".",
"examples/hot_reload_tide"
"examples/hot_reload_tide",
"examples/watcher_kind"
]
10 changes: 10 additions & 0 deletions examples/watcher_kind/Cargo.toml
@@ -0,0 +1,10 @@
[package]
name = "watcher_kind"
version = "0.1.0"
authors = ["Aron Heinecke <aron.heinecke@t-online.de>"]
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 = "../../" }
11 changes: 11 additions & 0 deletions examples/watcher_kind/src/main.rs
@@ -0,0 +1,11 @@
use std::time::Duration;

use notify::*;
fn main() {
let (tx, rx) = std::sync::mpsc::channel();
let watcher: Box<dyn Watcher> = if RecommendedWatcher::kind() == WatcherKind::PollWatcher {
Box::new(PollWatcher::with_delay(tx,Duration::from_secs(1)).unwrap())
} else {
Box::new(RecommendedWatcher::new(tx).unwrap())
};
}
4 changes: 4 additions & 0 deletions src/fsevent.rs
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/inotify.rs
Expand Up @@ -619,6 +619,10 @@ impl Watcher for INotifyWatcher {
self.waker.wake()?;
rx.recv()?
}

fn kind() -> crate::WatcherKind {
crate::WatcherKind::Inotify
}
}

impl Drop for INotifyWatcher {
Expand Down
4 changes: 4 additions & 0 deletions src/kqueue.rs
Expand Up @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions src/lib.rs
Expand Up @@ -185,6 +185,24 @@ impl EventHandler for std::sync::mpsc::Sender<Result<Event>> {
}
}

/// Watcher kind enumeration
#[derive(Debug,PartialEq,Eq)]
#[non_exhaustive]
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.
Expand Down Expand Up @@ -232,6 +250,9 @@ pub trait Watcher {
fn configure(&mut self, _option: Config) -> Result<bool> {
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
Expand Down
4 changes: 4 additions & 0 deletions src/null.rs
Expand Up @@ -22,4 +22,8 @@ impl Watcher for NullWatcher {
fn new<F: crate::EventHandler>(event_handler: F) -> Result<Self> where Self: Sized {
Ok(NullWatcher)
}

fn kind() -> crate::WatcherKind {
crate::WatcherKind::NullWatcher
}
}
4 changes: 4 additions & 0 deletions src/poll.rs
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion src/windows.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit db9841a

Please sign in to comment.