@@ -8,11 +8,15 @@ use std::{collections::HashSet, sync::mpsc::sync_channel};
8
8
impl SpirvBuilder {
9
9
/// Watches the module for changes using [`notify`](https://crates.io/crates/notify),
10
10
/// and rebuild it upon changes. Calls `on_compilation_finishes` after each
11
- /// successful compilation.
12
- pub fn watch (
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).
14
+ pub fn watch < T > (
13
15
& self ,
14
- mut on_compilation_finishes : impl FnMut ( CompileResult ) + Send + ' static ,
15
- ) -> Result < ( ) , SpirvBuilderError > {
16
+ mut on_compilation_finishes : impl FnMut ( CompileResult , Option < AcceptFirstCompile < ' _ , T > > )
17
+ + Send
18
+ + ' static ,
19
+ ) -> Result < Option < T > , SpirvBuilderError > {
16
20
let path_to_crate = self
17
21
. path_to_crate
18
22
. as_ref ( )
@@ -42,7 +46,8 @@ impl SpirvBuilder {
42
46
}
43
47
} ;
44
48
let metadata = self . parse_metadata_file ( & metadata_file) ?;
45
- on_compilation_finishes ( metadata) ;
49
+ let mut out = None ;
50
+ on_compilation_finishes ( metadata, Some ( AcceptFirstCompile ( & mut out) ) ) ;
46
51
47
52
let builder = self . clone ( ) ;
48
53
let thread = std:: thread:: spawn ( move || {
@@ -57,12 +62,24 @@ impl SpirvBuilder {
57
62
. parse_metadata_file ( & file)
58
63
. expect ( "Metadata file is correct" ) ;
59
64
watcher. watch_leaf_deps ( & metadata_file) ;
60
- on_compilation_finishes ( metadata) ;
65
+ on_compilation_finishes ( metadata, None ) ;
61
66
}
62
67
}
63
68
} ) ;
64
69
drop ( thread) ;
65
- Ok ( ( ) )
70
+ Ok ( out)
71
+ }
72
+ }
73
+
74
+ pub struct AcceptFirstCompile < ' a , T > ( & ' a mut Option < T > ) ;
75
+
76
+ impl < ' a , T > AcceptFirstCompile < ' a , T > {
77
+ pub fn new ( write : & ' a mut Option < T > ) -> Self {
78
+ Self ( write)
79
+ }
80
+
81
+ pub fn submit ( self , t : T ) {
82
+ * self . 0 = Some ( t) ;
66
83
}
67
84
}
68
85
0 commit comments