Skip to main content

Watcher

Trait Watcher 

Source
pub trait Watcher {
    // Required methods
    fn new<F: EventHandler>(event_handler: F, config: Config) -> Result<Self>
       where Self: Sized;
    fn watch(&mut self, path: &Path, watch_mode: WatchMode) -> Result<()>;
    fn unwatch(&mut self, path: &Path) -> Result<()>;
    fn kind() -> WatcherKind
       where Self: Sized;

    // Provided methods
    fn paths_mut<'me>(&'me mut self) -> Box<dyn PathsMut + 'me> { ... }
    fn configure(&mut self, _option: Config) -> Result<bool> { ... }
}
Expand description

Type that can deliver file activity notifications

Watcher is implemented per platform using the best implementation available on that platform. In addition to such event driven implementations, a polling implementation is also provided that should work on any platform.

§Creating a watcher

Because Watcher is a trait, Rust usually can’t infer the concrete watcher type when calling Watcher::new(...) directly. Prefer recommended_watcher / RecommendedWatcher (or a specific backend like PollWatcher) when constructing a watcher.

use notify::{Event, Result, WatchMode, Watcher};
use std::{path::Path, sync::mpsc};

fn main() -> Result<()> {
    let (tx, _rx) = mpsc::channel::<Result<Event>>();
    let mut watcher = notify::recommended_watcher(tx)?;
    watcher.watch(Path::new("."), WatchMode::recursive())?;
    Ok(())
}

Required Methods§

Source

fn new<F: EventHandler>(event_handler: F, config: Config) -> Result<Self>
where Self: Sized,

Create a new watcher with an initial Config.

Source

fn watch(&mut self, path: &Path, watch_mode: WatchMode) -> Result<()>

Begin watching a new path.

If the path is a directory, watch_mode.recursive_mode will be evaluated. If watch_mode.recursive_mode is RecursiveMode::Recursive events will be delivered for all files in that tree. Otherwise only the directory and its immediate children will be watched.

If the path is a file, watch_mode.recursive_mode will be ignored and events will be delivered only for the file.

Source

fn unwatch(&mut self, path: &Path) -> Result<()>

Stop watching a path.

§Errors

Returns an error in the case that path has not been watched or if removing the watch fails.

Source

fn kind() -> WatcherKind
where Self: Sized,

Returns the watcher kind, allowing to perform backend-specific tasks

Provided Methods§

Source

fn paths_mut<'me>(&'me mut self) -> Box<dyn PathsMut + 'me>

Add/remove paths to watch.

For some watcher implementations this method provides better performance than multiple calls to Watcher::watch and Watcher::unwatch if you want to add/remove many paths at once.

§Examples
let mut watcher = notify::recommended_watcher(|_event| { /* event handler */ })?;
let mut watcher_paths = watcher.paths_mut();
for path in many_paths_to_add {
    watcher_paths.add(path, WatchMode::recursive())?;
}
watcher_paths.commit()?;
Source

fn configure(&mut self, _option: Config) -> Result<bool>

Configure the watcher at runtime.

See the Config struct for all configuration options.

§Returns
  • Ok(true) on success.
  • Ok(false) if the watcher does not support or implement the option.
  • Err(notify::Error) on failure.

Implementors§