Skip to content

Commit 6cd6785

Browse files
tuguzTLegNeato
authored andcommitted
Return JoinHandle of watching thread to the caller
1 parent 929112a commit 6cd6785

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

crates/spirv-builder/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ use thiserror::Error;
9191
pub use rustc_codegen_spirv_types::Capability;
9292
pub use rustc_codegen_spirv_types::{CompileResult, ModuleResult};
9393

94+
#[cfg(feature = "watch")]
95+
pub use self::watch::Watch;
96+
9497
#[cfg(feature = "include-target-specs")]
9598
pub use rustc_codegen_spirv_target_specs::TARGET_SPEC_DIR_PATH;
9699

crates/spirv-builder/src/watch.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
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;
42
use std::path::{Path, PathBuf};
53
use std::sync::mpsc::Receiver;
4+
use std::thread::JoinHandle;
65
use 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+
812
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.
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+
86107
struct Watcher {
87108
watcher: RecommendedWatcher,
88109
rx: Receiver<()>,

examples/runners/wgpu/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ fn maybe_watch(
187187
}
188188
})
189189
.expect("Configuration is correct for watching")
190+
.first_compile
190191
.unwrap()
191192
} else {
192193
handle_compile_result(builder.build().unwrap())

0 commit comments

Comments
 (0)