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
pub struct VecCache<'a,O,V=O>
where
O: Into<V>,
{
pub(crate) cache: Vec<V>,
f: *mut (dyn Fn(&mut Self, usize) -> O + 'a),
}
impl<'a,O,V> VecCache<'a,O,V>
where
O: Into<V>,
{
pub fn new<F>(f: F) -> Self
where
F: Fn(&mut Self, usize) -> O + 'a
{
VecCache {
cache: Vec::default(),
f: Box::into_raw(Box::from(f)),
}
}
pub fn get(&mut self, input: usize) -> &V {
let len = self.cache.len();
if len <= input {
self.cache.reserve(input - len + 1);
}
while self.cache.len() <= input {
let next = self.cache.len();
let next_val = self.function(next).into();
self.cache.push(next_val);
}
self.cache.get(input).unwrap()
}
fn function(&mut self, input: usize) -> V {
unsafe { (*self.f)(self, input).into() }
}
}
#[doc(hidden)]
impl<'a,O,V> Drop for VecCache<'a,O,V>
where
O: Into<V>,
{
fn drop(&mut self) {
#[allow(unused_must_use)]
unsafe { Box::from_raw(self.f); }
}
}