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
use std::{borrow::Borrow, hash::Hash};
use derivative::Derivative;
use fnv::FnvHashMap;
use crate::{Handle, WeakHandle};
#[derive(Derivative)]
#[derivative(Default(bound = ""))]
pub struct Cache<A> {
map: FnvHashMap<String, WeakHandle<A>>,
}
impl<A> Cache<A>
where
A: Clone,
{
pub fn new() -> Self {
Default::default()
}
pub fn insert<K: Into<String>>(&mut self, key: K, asset: &Handle<A>) -> Option<WeakHandle<A>> {
self.map.insert(key.into(), asset.downgrade())
}
pub fn get<K>(&self, key: &K) -> Option<Handle<A>>
where
K: ?Sized + Hash + Eq,
String: Borrow<K>,
{
self.map.get(key).and_then(WeakHandle::upgrade)
}
pub fn clear_dead<F>(&mut self) {
self.map.retain(|_, h| !h.is_dead());
}
pub fn clear_all(&mut self) {
self.map.clear();
}
}