Skip to content

Commit 45f0ab1

Browse files
JohnTitorsapphi-red
authored andcommitted
docs: improve watcher docs
cherry-picked from notify-rs@63578c2
1 parent 9f281fa commit 45f0ab1

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

notify/src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,24 @@ pub trait PathsMut {
344344
/// `Watcher` is implemented per platform using the best implementation available on that platform.
345345
/// In addition to such event driven implementations, a polling implementation is also provided
346346
/// that should work on any platform.
347+
///
348+
/// # Creating a watcher
349+
///
350+
/// Because `Watcher` is a trait, Rust usually can't infer the concrete watcher type when calling
351+
/// `Watcher::new(...)` directly. Prefer [`recommended_watcher`] / [`RecommendedWatcher`] (or a
352+
/// specific backend like [`PollWatcher`]) when constructing a watcher.
353+
///
354+
/// ```no_run
355+
/// use notify::{Event, Result, WatchMode, Watcher};
356+
/// use std::{path::Path, sync::mpsc};
357+
///
358+
/// fn main() -> Result<()> {
359+
/// let (tx, _rx) = mpsc::channel::<Result<Event>>();
360+
/// let mut watcher = notify::recommended_watcher(tx)?;
361+
/// watcher.watch(Path::new("."), WatchMode::recursive())?;
362+
/// Ok(())
363+
/// }
364+
/// ```
347365
pub trait Watcher {
348366
/// Create a new watcher with an initial Config.
349367
fn new<F: EventHandler>(event_handler: F, config: config::Config) -> Result<Self>
@@ -462,6 +480,20 @@ pub type RecommendedWatcher = KqueueWatcher;
462480
pub type RecommendedWatcher = PollWatcher;
463481

464482
/// Convenience method for creating the [`RecommendedWatcher`] for the current platform.
483+
///
484+
/// This is often the most ergonomic way to construct a watcher, because calling `Watcher::new(...)`
485+
/// requires specifying the concrete watcher type.
486+
///
487+
/// ```no_run
488+
/// use notify::{Result, WatchMode, Watcher};
489+
/// use std::path::Path;
490+
///
491+
/// fn main() -> Result<()> {
492+
/// let mut watcher = notify::recommended_watcher(|res| println!("event: {res:?}"))?;
493+
/// watcher.watch(Path::new("."), WatchMode::recursive())?;
494+
/// Ok(())
495+
/// }
496+
/// ```
465497
pub fn recommended_watcher<F>(event_handler: F) -> Result<RecommendedWatcher>
466498
where
467499
F: EventHandler,

0 commit comments

Comments
 (0)