@@ -4,14 +4,14 @@ use std::path::PathBuf;
4
4
use std:: vec:: Vec ;
5
5
6
6
extern crate kqueue;
7
+ use kqueue:: FilterFlag ;
7
8
use kqueue:: Watcher as KQueue ;
8
- use kqueue:: FilterFlag as FilterFlag ;
9
9
10
10
const FILTER : kqueue:: EventFilter = kqueue:: EventFilter :: EVFILT_VNODE ;
11
11
12
12
pub struct FileWatcherImpl {
13
13
kq : KQueue ,
14
- watches : Vec < FileWatchImpl >
14
+ watches : Vec < FileWatchImpl > ,
15
15
}
16
16
17
17
pub struct FileWatchImpl {
@@ -25,57 +25,51 @@ impl FileWatcherImpl {
25
25
Err ( msg) => return Result :: Err ( msg) ,
26
26
} ;
27
27
28
- return Result :: Ok ( FileWatcherImpl {
28
+ Ok ( FileWatcherImpl {
29
29
kq,
30
- watches : vec ! [ ]
31
- } ) ;
30
+ watches : vec ! [ ] ,
31
+ } )
32
32
}
33
33
34
34
pub fn add_watch ( & mut self , file_path : & PathBuf ) -> Result < & FileWatchImpl > {
35
- let flags: FilterFlag =
36
- FilterFlag :: NOTE_WRITE |
37
- FilterFlag :: NOTE_EXTEND |
38
- FilterFlag :: NOTE_RENAME |
39
- FilterFlag :: NOTE_DELETE |
40
- FilterFlag :: NOTE_LINK ;
35
+ let flags: FilterFlag = FilterFlag :: NOTE_WRITE
36
+ | FilterFlag :: NOTE_EXTEND
37
+ | FilterFlag :: NOTE_RENAME
38
+ | FilterFlag :: NOTE_DELETE
39
+ | FilterFlag :: NOTE_LINK ;
41
40
42
41
let file = File :: open ( file_path) ?;
43
- let _ = match self . kq . add_file ( & file, FILTER , flags) {
44
- Ok ( w) => w,
45
- Err ( msg) => return Result :: Err ( msg) ,
46
- } ;
42
+ self . kq . add_file ( & file, FILTER , flags) ?;
47
43
48
- let fw = FileWatchImpl {
49
- file
50
- } ;
44
+ let fw = FileWatchImpl { file } ;
51
45
52
46
self . watches . push ( fw) ;
53
- return Result :: Ok ( & self . watches . last ( ) . unwrap ( ) ) ;
47
+ Ok ( self . watches . last ( ) . unwrap ( ) )
54
48
}
55
49
56
50
pub fn rm_watch ( & mut self , fw : & FileWatchImpl ) -> Result < ( ) > {
57
51
for i in 0 ..self . watches . len ( ) {
58
52
let item_ref = self . watches . get ( i) . unwrap ( ) ;
59
- if item_ref as * const FileWatchImpl == fw as * const FileWatchImpl {
53
+ if std :: ptr :: eq ( item_ref, fw ) {
60
54
let item = self . watches . remove ( i) ;
61
55
return self . kq . remove_file ( & item. file , FILTER ) ;
62
56
}
63
57
}
64
58
65
- return Result :: Err ( Error :: new (
59
+ Err ( Error :: new (
66
60
ErrorKind :: InvalidInput ,
67
- "Passed FileWatch does not belong to this FileWatcher instance"
68
- ) ) ;
61
+ "Passed FileWatch does not belong to this FileWatcher instance" ,
62
+ ) )
69
63
}
70
64
71
65
pub fn start ( & mut self ) -> Result < ( ) > {
72
- return self . kq . watch ( ) ;
66
+ self . kq . watch ( )
73
67
}
74
68
75
69
pub fn any_events ( & mut self ) -> Result < bool > {
76
70
match self . kq . poll ( None ) {
77
- Some ( _) => return Result :: Ok ( true ) ,
78
- None => return Result :: Ok ( false ) ,
71
+ Some ( _) => Ok ( true ) ,
72
+ None => Ok ( false ) ,
79
73
}
80
74
}
81
75
}
0 commit comments