162
162
163
163
#![ deny( missing_docs) ]
164
164
165
- pub use config:: { Config , RecursiveMode } ;
166
- pub use error:: { Error , ErrorKind , Result , UpdateWatchesError } ;
165
+ pub use config:: { Config , PathOp , RecursiveMode , WatchPathConfig } ;
166
+ pub use error:: { Error , ErrorKind , Result , UpdatePathsError } ;
167
167
pub use notify_types:: event:: { self , Event , EventKind } ;
168
- use std:: path:: { Path , PathBuf } ;
168
+ use std:: path:: Path ;
169
169
170
170
pub ( crate ) type StdResult < T , E > = std:: result:: Result < T , E > ;
171
171
pub ( crate ) type Receiver < T > = std:: sync:: mpsc:: Receiver < T > ;
@@ -294,16 +294,47 @@ pub enum WatcherKind {
294
294
NullWatcher ,
295
295
}
296
296
297
- /// An operation to apply to a watcher
297
+ /// Providing methods for adding and removing paths to watch.
298
298
///
299
- /// See [`Watcher::update_watches`] for more information
300
- #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
301
- pub enum WatchOp {
302
- /// Path should be watcher
303
- Watch ( PathBuf , RecursiveMode ) ,
304
-
305
- /// Path should be unwatched
306
- Unwatch ( PathBuf ) ,
299
+ /// `Box<dyn PathsMut>` is created by [`Watcher::paths_mut`]. See its documentation for more.
300
+ #[ deprecated(
301
+ since = "9.0.0" ,
302
+ note = "paths_mut has been replaced with update_paths"
303
+ ) ]
304
+ pub trait PathsMut {
305
+ /// Add a new path to watch. See [`Watcher::watch`] for more.
306
+ fn add ( & mut self , path : & Path , recursive_mode : RecursiveMode ) -> Result < ( ) > ;
307
+
308
+ /// Remove a path from watching. See [`Watcher::unwatch`] for more.
309
+ fn remove ( & mut self , path : & Path ) -> Result < ( ) > ;
310
+
311
+ /// Ensure added/removed paths are applied.
312
+ fn commit ( self : Box < Self > ) -> Result < ( ) > ;
313
+ }
314
+
315
+ struct PathsMutInternal < ' a , T : ?Sized > {
316
+ ops : Vec < PathOp > ,
317
+ watcher : & ' a mut T ,
318
+ }
319
+
320
+ #[ allow( deprecated) ]
321
+ impl < ' a , T : Watcher + ?Sized > PathsMut for PathsMutInternal < ' a , T > {
322
+ fn add ( & mut self , path : & Path , recursive_mode : RecursiveMode ) -> Result < ( ) > {
323
+ self . ops . push ( PathOp :: Watch (
324
+ path. to_path_buf ( ) ,
325
+ WatchPathConfig :: new ( recursive_mode) ,
326
+ ) ) ;
327
+ Ok ( ( ) )
328
+ }
329
+
330
+ fn remove ( & mut self , path : & Path ) -> Result < ( ) > {
331
+ self . ops . push ( PathOp :: Unwatch ( path. to_path_buf ( ) ) ) ;
332
+ Ok ( ( ) )
333
+ }
334
+
335
+ fn commit ( self : Box < Self > ) -> Result < ( ) > {
336
+ self . watcher . update_paths ( self . ops ) . map_err ( |v| v. source )
337
+ }
307
338
}
308
339
309
340
/// Type that can deliver file activity notifications
@@ -341,6 +372,19 @@ pub trait Watcher {
341
372
/// fails.
342
373
fn unwatch ( & mut self , path : & Path ) -> Result < ( ) > ;
343
374
375
+ /// deprecated
376
+ #[ deprecated(
377
+ since = "9.0.0" ,
378
+ note = "paths_mut has been replaced with update_paths"
379
+ ) ]
380
+ #[ allow( deprecated) ]
381
+ fn paths_mut < ' me > ( & ' me mut self ) -> Box < dyn PathsMut + ' me > {
382
+ Box :: new ( PathsMutInternal {
383
+ watcher : self ,
384
+ ops : Default :: default ( ) ,
385
+ } )
386
+ }
387
+
344
388
/// Add/remove paths to watch in batch.
345
389
///
346
390
/// For some [`Watcher`] implementations this method provides better performance than multiple
@@ -349,31 +393,31 @@ pub trait Watcher {
349
393
/// # Examples
350
394
///
351
395
/// ```
352
- /// # use notify::{Watcher, RecursiveMode, WatchOp };
353
- /// # use std::path::Path;
396
+ /// # use notify::{Watcher, RecursiveMode, PathOp };
397
+ /// # use std::path::{ Path, PathBuf} ;
354
398
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
355
- /// # let many_paths_to_add = vec![];
356
- /// # let many_paths_to_remove = vec![];
399
+ /// # let many_paths_to_add: Vec<PathBuf> = vec![];
400
+ /// # let many_paths_to_remove: Vec<PathBuf> = vec![];
357
401
/// let mut watcher = notify::NullWatcher;
358
402
/// let mut batch = Vec::new();
359
403
///
360
- /// for ( path, recursive_mode) in many_paths_to_add {
361
- /// batch.push(WatchOp::Watch (path, recursive_mode ));
404
+ /// for path in many_paths_to_add {
405
+ /// batch.push(PathOp::watch_recursive (path));
362
406
/// }
363
407
///
364
408
/// for path in many_paths_to_remove {
365
- /// batch.push(WatchOp::Unwatch (path));
409
+ /// batch.push(PathOp::unwatch (path));
366
410
/// }
367
411
///
368
412
/// // real work is done there
369
- /// watcher.update_watches (batch)?;
413
+ /// watcher.update_paths (batch)?;
370
414
/// # Ok(())
371
415
/// # }
372
416
/// ```
373
- fn update_watches ( & mut self , ops : Vec < WatchOp > ) -> StdResult < ( ) , UpdateWatchesError > {
374
- update_watches ( ops, |op| match op {
375
- WatchOp :: Watch ( path, recursive_mode ) => self . watch ( & path, recursive_mode) ,
376
- WatchOp :: Unwatch ( path) => self . unwatch ( & path) ,
417
+ fn update_paths ( & mut self , ops : Vec < PathOp > ) -> StdResult < ( ) , UpdatePathsError > {
418
+ update_paths ( ops, |op| match op {
419
+ PathOp :: Watch ( path, config ) => self . watch ( & path, config . recursive_mode ( ) ) ,
420
+ PathOp :: Unwatch ( path) => self . unwatch ( & path) ,
377
421
} )
378
422
}
379
423
@@ -438,17 +482,14 @@ where
438
482
RecommendedWatcher :: new ( event_handler, Config :: default ( ) )
439
483
}
440
484
441
- pub ( crate ) fn update_watches < F > (
442
- ops : Vec < WatchOp > ,
443
- mut apply : F ,
444
- ) -> StdResult < ( ) , UpdateWatchesError >
485
+ pub ( crate ) fn update_paths < F > ( ops : Vec < PathOp > , mut apply : F ) -> StdResult < ( ) , UpdatePathsError >
445
486
where
446
- F : FnMut ( WatchOp ) -> Result < ( ) > ,
487
+ F : FnMut ( PathOp ) -> Result < ( ) > ,
447
488
{
448
489
let mut iter = ops. into_iter ( ) ;
449
490
while let Some ( op) = iter. next ( ) {
450
491
if let Err ( source) = apply ( op) {
451
- return Err ( UpdateWatchesError {
492
+ return Err ( UpdatePathsError {
452
493
source,
453
494
remaining : iter. collect ( ) ,
454
495
} ) ;
@@ -557,7 +598,7 @@ mod tests {
557
598
}
558
599
559
600
#[ test]
560
- fn test_update_watches ( ) -> std:: result:: Result < ( ) , Box < dyn std:: error:: Error > > {
601
+ fn test_update_paths ( ) -> std:: result:: Result < ( ) , Box < dyn std:: error:: Error > > {
561
602
let dir = tempdir ( ) ?;
562
603
563
604
let dir_a = dir. path ( ) . join ( "a" ) ;
@@ -570,9 +611,15 @@ mod tests {
570
611
let mut watcher = RecommendedWatcher :: new ( tx, Config :: default ( ) ) ?;
571
612
572
613
// start watching a and b
573
- watcher. update_watches ( vec ! [
574
- WatchOp :: Watch ( dir_a. clone( ) , RecursiveMode :: Recursive ) ,
575
- WatchOp :: Watch ( dir_b. clone( ) , RecursiveMode :: Recursive ) ,
614
+ watcher. update_paths ( vec ! [
615
+ PathOp :: Watch (
616
+ dir_a. clone( ) ,
617
+ WatchPathConfig :: new( RecursiveMode :: Recursive ) ,
618
+ ) ,
619
+ PathOp :: Watch (
620
+ dir_b. clone( ) ,
621
+ WatchPathConfig :: new( RecursiveMode :: Recursive ) ,
622
+ ) ,
576
623
] ) ?;
577
624
578
625
// create file1 in both a and b
@@ -599,7 +646,7 @@ mod tests {
599
646
assert ! ( b_file1_encountered, "Did not receive event of {b_file1:?}" ) ;
600
647
601
648
// stop watching a
602
- watcher. update_watches ( vec ! [ WatchOp :: Unwatch ( dir_a. clone ( ) ) ] ) ?;
649
+ watcher. update_paths ( vec ! [ PathOp :: unwatch ( & dir_a) ] ) ?;
603
650
604
651
// create file2 in both a and b
605
652
let a_file2 = dir_a. join ( "file2" ) ;
0 commit comments