@@ -8,11 +8,15 @@ use std::{collections::HashSet, sync::mpsc::sync_channel};
88impl SpirvBuilder {
99 /// Watches the module for changes using [`notify`](https://crates.io/crates/notify),
1010 /// 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 > (
1315 & 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 > {
1620 let path_to_crate = self
1721 . path_to_crate
1822 . as_ref ( )
@@ -42,7 +46,8 @@ impl SpirvBuilder {
4246 }
4347 } ;
4448 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) ) ) ;
4651
4752 let builder = self . clone ( ) ;
4853 let thread = std:: thread:: spawn ( move || {
@@ -57,12 +62,24 @@ impl SpirvBuilder {
5762 . parse_metadata_file ( & file)
5863 . expect ( "Metadata file is correct" ) ;
5964 watcher. watch_leaf_deps ( & metadata_file) ;
60- on_compilation_finishes ( metadata) ;
65+ on_compilation_finishes ( metadata, None ) ;
6166 }
6267 }
6368 } ) ;
6469 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) ;
6683 }
6784}
6885
0 commit comments