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
//! Action types kept per thread and generation of app code: the one
//! [`ActionType`] every item offering an action shares, built the first time
//! an item offers it.
use std::cell::RefCell;
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Arc;
use std::thread::LocalKey;
use tilo_actions::ActionType;
/// The kept types of one kind of key, for one generation of app code
/// ([`reset_type_schemas`](crate::reset_type_schemas) starts the next).
pub(crate) struct KeptTypes<K, V = Arc<ActionType>> {
epoch: u64,
types: HashMap<K, V>,
}
impl<K, V> Default for KeptTypes<K, V> {
fn default() -> Self {
Self {
epoch: 0,
types: HashMap::new(),
}
}
}
/// The type `key` names in `kept`, built by `build` when this thread has
/// none of this generation. Built outside the borrow: building one may keep
/// another.
pub(crate) fn kept_type<K: Hash + Eq, V: Clone>(
kept: &'static LocalKey<RefCell<KeptTypes<K, V>>>,
key: K,
build: impl FnOnce() -> V,
) -> V {
let epoch = crate::resource::words_epoch();
let found = kept.with(|kept| {
let kept = kept.borrow();
if kept.epoch == epoch {
kept.types.get(&key).cloned()
} else {
None
}
});
found.unwrap_or_else(|| {
let ty = build();
kept.with(|kept| {
let mut kept = kept.borrow_mut();
if kept.epoch != epoch {
*kept = KeptTypes {
epoch,
types: HashMap::new(),
};
}
kept.types.insert(key, ty.clone());
});
ty
})
}
/// Types kept per vocabulary and dotted path or field: `None` where the
/// path offers no such action, so it is not looked for again.
pub(crate) type KeptByPath = KeptTypes<(std::any::TypeId, String), Option<Arc<ActionType>>>;