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
use std::marker::PhantomData;
use crate::Context;
use crate::context::SlotId;
/// A typed handle to a lazily-computed slot within a [`Context`].
///
/// The handle itself is lightweight (just an id) and does not own the data.
/// All data lives inside the Context.
pub struct SlotHandle<T> {
pub(crate) id: SlotId,
pub(crate) _marker: PhantomData<T>,
}
impl<T> SlotHandle<T> {
pub(crate) fn new(id: SlotId) -> Self {
Self {
id,
_marker: PhantomData,
}
}
/// Get this slot's value through its owning context.
///
/// This is an ergonomic alias for [`Context::get`].
pub fn get(&self, ctx: &Context) -> T
where
T: Clone + 'static,
{
ctx.get(self)
}
/// Clear this slot's cached value and recursively clear all dependents.
///
/// The slot will recompute on the next [`Context::get`] call.
pub fn clear(&self, ctx: &Context) {
ctx.clear_slot(self.id);
ctx.flush_effects_after_invalidation();
}
}
// Handles are Copy/Clone since they're just ids.
impl<T> Clone for SlotHandle<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for SlotHandle<T> {}