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
use crate::Composer;
#[derive(Debug)]
pub struct Recomposer {
pub(crate) composer: Composer,
}
impl Recomposer {
pub fn new() -> Self {
Recomposer {
composer: Composer::new(),
}
}
pub fn with_capacity(capacity: usize) -> Self {
Recomposer {
composer: Composer::with_capacity(capacity),
}
}
pub fn root<R: 'static>(&self) -> Option<&R> {
self.composer
.tape
.get(0)
.map(|s| &s.data)
.and_then(|n| n.cast_ref::<R>())
}
pub fn root_mut<R: 'static>(&mut self) -> Option<&mut R> {
self.composer
.tape
.get_mut(0)
.map(|s| &mut s.data)
.and_then(|n| n.cast_mut::<R>())
}
pub fn compose<F, T>(&mut self, func: F) -> T
where
F: FnOnce(&mut Composer) -> T,
{
let composer = &mut self.composer;
let id = composer.id;
let curr_cursor = composer.cursor;
composer.composing = true;
let t = func(composer);
assert!(
id == composer.id && composer.composing && composer.tape.len() >= curr_cursor,
"Composer is in inconsistent state"
);
self.finalize();
t
}
fn finalize(&mut self) {
let composer = &mut self.composer;
composer.tape.truncate(composer.cursor);
composer.slot_depth.truncate(composer.cursor);
composer.state_tape.truncate(composer.state_cursor);
composer.recycle_bin.clear();
composer.cursor = 0;
composer.depth = 0;
composer.state_cursor = 0;
composer.composing = false;
}
}
impl Default for Recomposer {
fn default() -> Self {
Self::new()
}
}