1
- use crate :: { SpirvBuilder , SpirvBuilderError , leaf_deps} ;
2
- use notify:: { Event , RecommendedWatcher , RecursiveMode , Watcher as _} ;
3
- use rustc_codegen_spirv_types:: CompileResult ;
1
+ use std:: convert:: Infallible ;
4
2
use std:: path:: { Path , PathBuf } ;
5
3
use std:: sync:: mpsc:: Receiver ;
4
+ use std:: thread:: JoinHandle ;
6
5
use std:: { collections:: HashSet , sync:: mpsc:: sync_channel} ;
7
6
7
+ use notify:: { Event , RecommendedWatcher , RecursiveMode , Watcher as _} ;
8
+ use rustc_codegen_spirv_types:: CompileResult ;
9
+
10
+ use crate :: { SpirvBuilder , SpirvBuilderError , leaf_deps} ;
11
+
8
12
impl SpirvBuilder {
9
- /// Watches the module for changes using [`notify`](https://crates.io/crates/notify),
10
- /// and rebuild it upon changes. Calls `on_compilation_finishes` after each
11
- /// successful compilation. The second `Option<AcceptFirstCompile<T>>`
12
- /// param allows you to return some `T` on the first compile, which is
13
- /// then returned by this function (wrapped in Option).
13
+ /// Watches the module for changes using [`notify`], rebuilding it upon changes.
14
+ ///
15
+ /// Calls `on_compilation_finishes` after each successful compilation.
16
+ /// The second `Option<AcceptFirstCompile<T>>` param allows you to return some `T`
17
+ /// on the first compile, which is then returned by this function
18
+ /// in pair with [`JoinHandle`] to the watching thread.
14
19
pub fn watch < T > (
15
20
& self ,
16
21
mut on_compilation_finishes : impl FnMut ( CompileResult , Option < AcceptFirstCompile < ' _ , T > > )
17
22
+ Send
18
23
+ ' static ,
19
- ) -> Result < Option < T > , SpirvBuilderError > {
24
+ ) -> Result < Watch < T > , SpirvBuilderError > {
20
25
let path_to_crate = self
21
26
. path_to_crate
22
27
. as_ref ( )
@@ -46,11 +51,11 @@ impl SpirvBuilder {
46
51
}
47
52
} ;
48
53
let metadata = self . parse_metadata_file ( & metadata_file) ?;
49
- let mut out = None ;
50
- on_compilation_finishes ( metadata, Some ( AcceptFirstCompile ( & mut out ) ) ) ;
54
+ let mut first_compile = None ;
55
+ on_compilation_finishes ( metadata, Some ( AcceptFirstCompile ( & mut first_compile ) ) ) ;
51
56
52
57
let builder = self . clone ( ) ;
53
- let thread = std:: thread:: spawn ( move || {
58
+ let watch_thread = std:: thread:: spawn ( move || {
54
59
let mut watcher = Watcher :: new ( ) ;
55
60
watcher. watch_leaf_deps ( & metadata_file) ;
56
61
@@ -66,8 +71,11 @@ impl SpirvBuilder {
66
71
}
67
72
}
68
73
} ) ;
69
- drop ( thread) ;
70
- Ok ( out)
74
+
75
+ Ok ( Watch {
76
+ first_compile,
77
+ watch_thread,
78
+ } )
71
79
}
72
80
}
73
81
@@ -83,6 +91,19 @@ impl<'a, T> AcceptFirstCompile<'a, T> {
83
91
}
84
92
}
85
93
94
+ /// Result of [watching](SpirvBuilder::watch) a module for changes.
95
+ #[ must_use]
96
+ #[ non_exhaustive]
97
+ pub struct Watch < T > {
98
+ /// Result of the first compile, if any.
99
+ pub first_compile : Option < T > ,
100
+ /// Join handle to the watching thread.
101
+ ///
102
+ /// You can drop it to detach the watching thread,
103
+ /// or [`join()`](JoinHandle::join) it to block the current thread until shutdown of the program.
104
+ pub watch_thread : JoinHandle < Infallible > ,
105
+ }
106
+
86
107
struct Watcher {
87
108
watcher : RecommendedWatcher ,
88
109
rx : Receiver < ( ) > ,
0 commit comments