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

Add Watcher::kind() #364

Merged
merged 1 commit into from
Sep 30, 2021
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we'll add/remove watchers in the future (actually, kqueue has been added recently) so it should make sense to add the #[non_exhaustive] attribute.

/// 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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