1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/// Holds a value that can be recomputed asynchronously in a background thread.
///
/// Use [`spawn`](Self::spawn) to kick off a computation and [`poll`](Self::poll)
/// to check for completion. The current value remains accessible while a new one
/// is being computed.
pub struct ComputedState<T> {
value: T,
in_flight: Option<std::thread::JoinHandle<T>>,
}
impl<T> ComputedState<T> {
/// Creates a new `ComputedState` with the given initial value.
pub fn new(value: T) -> Self {
Self {
value,
in_flight: None,
}
}
/// Returns a shared reference to the current value.
pub fn value(&self) -> &T {
&self.value
}
/// Returns a mutable reference to the current value.
pub fn value_mut(&mut self) -> &mut T {
&mut self.value
}
/// Replaces the current value and cancels any in-flight computation.
pub fn set(&mut self, value: T) {
self.value = value;
self.in_flight = None;
}
/// Returns `true` if a background computation is in progress.
pub fn is_running(&self) -> bool {
self.in_flight.is_some()
}
/// Checks whether the in-flight computation has finished.
/// If so, updates the stored value and returns `true`.
pub fn poll(&mut self) -> bool
where
T: Send + 'static,
{
let Some(handle) = self.in_flight.as_ref() else {
return false;
};
if !handle.is_finished() {
return false;
}
let handle = self.in_flight.take().expect("in-flight handle missing");
self.value = handle.join().unwrap();
true
}
/// Spawns `action` on a background thread if no computation is already running.
/// Returns a mutable reference to the current (potentially stale) value.
pub fn spawn(&mut self, action: impl FnOnce() -> T + Send + 'static) -> &mut T
where
T: Send + 'static,
{
self.poll();
if !self.is_running() {
self.in_flight = Some(std::thread::spawn(action));
}
&mut self.value
}
}
impl<T: Default> Default for ComputedState<T> {
fn default() -> Self {
Self::new(T::default())
}
}
impl<T: std::fmt::Debug> std::fmt::Debug for ComputedState<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ComputedState")
.field("value", &self.value)
.field("running", &self.is_running())
.finish()
}
}