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 ;
42use std:: path:: { Path , PathBuf } ;
53use std:: sync:: mpsc:: Receiver ;
4+ use std:: thread:: JoinHandle ;
65use std:: { collections:: HashSet , sync:: mpsc:: sync_channel} ;
76
7+ use notify:: { Event , RecommendedWatcher , RecursiveMode , Watcher as _} ;
8+ use rustc_codegen_spirv_types:: CompileResult ;
9+
10+ use crate :: { SpirvBuilder , SpirvBuilderError , leaf_deps} ;
11+
812impl 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.
1419 pub fn watch < T > (
1520 & self ,
1621 mut on_compilation_finishes : impl FnMut ( CompileResult , Option < AcceptFirstCompile < ' _ , T > > )
1722 + Send
1823 + ' static ,
19- ) -> Result < Option < T > , SpirvBuilderError > {
24+ ) -> Result < Watch < T > , SpirvBuilderError > {
2025 let path_to_crate = self
2126 . path_to_crate
2227 . as_ref ( )
@@ -46,11 +51,11 @@ impl SpirvBuilder {
4651 }
4752 } ;
4853 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 ) ) ) ;
5156
5257 let builder = self . clone ( ) ;
53- let thread = std:: thread:: spawn ( move || {
58+ let watch_thread = std:: thread:: spawn ( move || {
5459 let mut watcher = Watcher :: new ( ) ;
5560 watcher. watch_leaf_deps ( & metadata_file) ;
5661
@@ -66,8 +71,11 @@ impl SpirvBuilder {
6671 }
6772 }
6873 } ) ;
69- drop ( thread) ;
70- Ok ( out)
74+
75+ Ok ( Watch {
76+ first_compile,
77+ watch_thread,
78+ } )
7179 }
7280}
7381
@@ -83,6 +91,19 @@ impl<'a, T> AcceptFirstCompile<'a, T> {
8391 }
8492}
8593
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+
86107struct Watcher {
87108 watcher : RecommendedWatcher ,
88109 rx : Receiver < ( ) > ,
0 commit comments