Skip to content

Commit 29376ad

Browse files
committed
replace old config and allow initialization configuration
This also allows correct configuration of PollWatcher via notify-debouncer-mini
1 parent ae9134c commit 29376ad

18 files changed

Lines changed: 128 additions & 151 deletions

File tree

README.md

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,43 +25,6 @@ As used by: [alacritty], [cargo watch], [cobalt], [docket], [mdBook], [pax],
2525
[rdiff], [rust-analyzer], [timetrack], [watchexec], [xi-editor], [watchfiles],
2626
and others.
2727

28-
## Base Installation
29-
30-
```toml
31-
[dependencies]
32-
notify = "5.0.0-pre.15"
33-
```
34-
35-
## Usage
36-
37-
A basic example
38-
39-
```rust
40-
use notify::{RecommendedWatcher, RecursiveMode, Result, watcher};
41-
use std::time::Duration;
42-
43-
fn main() -> Result<()> {
44-
// Automatically select the best implementation for your platform.
45-
// You can also access each implementation directly e.g. INotifyWatcher.
46-
let mut watcher = watcher(Duration::from_secs(2))?;
47-
48-
// Add a path to be watched. All files and directories at that path and
49-
// below will be monitored for changes.
50-
watcher.watch("/home/test/notify", RecursiveMode::Recursive)?;
51-
52-
// This is a simple loop, but you may want to use more complex logic here,
53-
// for example to handle I/O.
54-
for event in &watcher {
55-
match event {
56-
Ok(event) => println!("changed: {:?}", event.path),
57-
Err(err) => println!("watch error: {:?}", err),
58-
};
59-
}
60-
61-
Ok(())
62-
}
63-
```
64-
6528
## Platforms
6629

6730
- Linux / Android: inotify

examples/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ path = "monitor_raw.rs"
2121
name = "debounced"
2222
path = "debounced.rs"
2323

24+
[[example]]
25+
name = "debounced_custom"
26+
path = "debounced_full_custom.rs"
27+
2428
[[example]]
2529
name = "poll_sysfs"
2630
path = "poll_sysfs.rs"

examples/async_monitor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use futures::{
22
channel::mpsc::{channel, Receiver},
33
SinkExt, StreamExt,
44
};
5-
use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher};
5+
use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher, Config};
66
use std::path::Path;
77

88
/// Async, futures channel based event watching
@@ -28,7 +28,7 @@ fn async_watcher() -> notify::Result<(RecommendedWatcher, Receiver<notify::Resul
2828
futures::executor::block_on(async {
2929
tx.send(res).await.unwrap();
3030
})
31-
})?;
31+
}, Config::default())?;
3232

3333
Ok((watcher, rx))
3434
}

examples/debounced.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{path::Path, time::Duration};
22

3-
use notify::{RecursiveMode, Watcher};
3+
use notify::{RecursiveMode};
44
use notify_debouncer_mini::new_debouncer;
55

66
/// Example for debouncer

examples/debounced_full_custom.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{path::Path, time::Duration};
22

3-
use notify::{RecursiveMode, Watcher};
4-
use notify_debouncer_mini::new_debouncer;
3+
use notify::{RecursiveMode, Config};
4+
use notify_debouncer_mini::new_debouncer_opt;
55

66
/// Debouncer with custom backend and waiting for exit
77
fn main() {
@@ -18,7 +18,7 @@ fn main() {
1818
// setup debouncer
1919
let (tx, rx) = std::sync::mpsc::channel();
2020
// select backend via fish operator, here PollWatcher backend
21-
let mut debouncer = new_debouncer_opt::<_,notify::PollWatcher>(Duration::from_secs(2), None, tx).unwrap();
21+
let mut debouncer = new_debouncer_opt::<_,notify::PollWatcher>(Duration::from_secs(2), None, tx, Config::default()).unwrap();
2222

2323
debouncer
2424
.watcher()

examples/hot_reload_tide/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async fn main() -> tide::Result<()> {
3838
Err(error) => println!("Error reloading config: {:?}", error),
3939
}
4040
}
41-
})?;
41+
},notify::Config::default())?;
4242

4343
watcher.watch(Path::new(CONFIG_PATH), RecursiveMode::Recursive)?;
4444

examples/monitor_raw.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
1+
use notify::{RecommendedWatcher, RecursiveMode, Watcher, Config};
22
use std::path::Path;
33

44
fn main() {
@@ -16,7 +16,7 @@ fn watch<P: AsRef<Path>>(path: P) -> notify::Result<()> {
1616

1717
// Automatically select the best implementation for your platform.
1818
// You can also access each implementation directly e.g. INotifyWatcher.
19-
let mut watcher = RecommendedWatcher::new(tx)?;
19+
let mut watcher = RecommendedWatcher::new(tx, Config::default())?;
2020

2121
// Add a path to be watched. All files and directories at that path and
2222
// below will be monitored for changes.

examples/poll_sysfs.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
/// This example can't be demonstrated under windows, it might be relevant for network shares
44
#[cfg(not(target_os = "windows"))]
55
fn not_windows_main() -> notify::Result<()> {
6-
use notify::poll::PollWatcherConfig;
7-
use notify::{PollWatcher, RecursiveMode, Watcher};
6+
use notify::{PollWatcher, RecursiveMode, Watcher, Config};
87
use std::path::Path;
98
use std::time::Duration;
109

@@ -27,13 +26,12 @@ fn not_windows_main() -> notify::Result<()> {
2726

2827
println!("watching {:?}...", paths);
2928
// configure pollwatcher backend
30-
let config = PollWatcherConfig {
31-
compare_contents: true, // crucial part for pseudo filesystems
32-
poll_interval: Duration::from_secs(2),
33-
};
29+
let config = Config::default()
30+
.with_compare_contents(true) // crucial part for pseudo filesystems
31+
.with_poll_interval(Duration::from_secs(2));
3432
let (tx, rx) = std::sync::mpsc::channel();
3533
// create pollwatcher backend
36-
let mut watcher = PollWatcher::with_config(tx, config)?;
34+
let mut watcher = PollWatcher::new(tx, config)?;
3735
for path in paths {
3836
// watch all paths
3937
watcher.watch(&path, RecursiveMode::Recursive)?;

examples/watcher_kind.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
use std::{path::Path, time::Duration};
2+
use notify::*;
23

3-
use notify::{poll::PollWatcherConfig, *};
4+
// exampale of detecting the recommended watcher kind
45
fn main() {
56
let (tx, rx) = std::sync::mpsc::channel();
7+
// This example is a little bit misleading as you can just create one Config and use it for all watchers.
8+
// That way the pollwatcher specific stuff is still configured, if it should be used.
69
let mut watcher: Box<dyn Watcher> = if RecommendedWatcher::kind() == WatcherKind::PollWatcher {
710
// custom config for PollWatcher kind
8-
let config = PollWatcherConfig {
9-
poll_interval: Duration::from_secs(1),
10-
..Default::default()
11-
};
12-
Box::new(PollWatcher::with_config(tx, config).unwrap())
11+
// you
12+
let config = Config::default()
13+
.with_poll_interval(Duration::from_secs(1));
14+
Box::new(PollWatcher::new(tx, config).unwrap())
1315
} else {
1416
// use default config for everything else
15-
Box::new(RecommendedWatcher::new(tx).unwrap())
17+
Box::new(RecommendedWatcher::new(tx, Config::default()).unwrap())
1618
};
1719

1820
// watch some stuff

notify-debouncer-mini/src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//!
55
//! ```toml
66
//! [dependencies]
7-
//! notify = "5.0.0-pre.15"
87
//! notify-debouncer-mini = "0.1"
98
//! ```
109
//!
@@ -13,10 +12,12 @@
1312
//! ```rust,no_run
1413
//! # use std::path::Path;
1514
//! # use std::time::Duration;
16-
//! use notify::{Watcher, RecursiveMode, Result};
17-
//! use notify_debouncer_mini::{new_debouncer,DebounceEventResult};
15+
//! use notify_debouncer_mini::{notify::*,new_debouncer,DebounceEventResult};
1816
//!
1917
//! # fn main() {
18+
//! // setup initial watcher backend config
19+
//! let config = Config::default();
20+
//!
2021
//! // Select recommended watcher for debouncer.
2122
//! // Using a callback here, could also be a channel.
2223
//! let mut debouncer = new_debouncer(Duration::from_secs(2), None, |res: DebounceEventResult| {
@@ -50,6 +51,7 @@ use std::{
5051
time::{Duration, Instant},
5152
};
5253

54+
pub use notify;
5355
use notify::{Error, ErrorKind, Event, RecommendedWatcher, Watcher};
5456

5557
/// The set of requirements for watcher debounce event handling functions.
@@ -259,6 +261,7 @@ pub fn new_debouncer_opt<F: DebounceEventHandler, T: Watcher>(
259261
timeout: Duration,
260262
tick_rate: Option<Duration>,
261263
mut event_handler: F,
264+
config: notify::Config
262265
) -> Result<Debouncer<T>, Error> {
263266
let data = DebounceData::default();
264267

@@ -320,7 +323,7 @@ pub fn new_debouncer_opt<F: DebounceEventHandler, T: Watcher>(
320323
// can't have multiple TX, so we need to pipe that through our debouncer
321324
Err(e) => lock.add_error(e),
322325
}
323-
})?;
326+
}, config)?;
324327

325328
let guard = Debouncer {
326329
watcher,
@@ -339,7 +342,7 @@ pub fn new_debouncer_opt<F: DebounceEventHandler, T: Watcher>(
339342
pub fn new_debouncer<F: DebounceEventHandler>(
340343
timeout: Duration,
341344
tick_rate: Option<Duration>,
342-
event_handler: F,
345+
event_handler: F
343346
) -> Result<Debouncer<RecommendedWatcher>, Error> {
344-
new_debouncer_opt::<F, RecommendedWatcher>(timeout, tick_rate, event_handler)
347+
new_debouncer_opt::<F, RecommendedWatcher>(timeout, tick_rate, event_handler, notify::Config::default())
345348
}

0 commit comments

Comments
 (0)