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
use std::io;
use crate::{CircuitDescription, FixedText, Source};
pub type ContextUnit = Context<()>;
#[derive(Debug, Default)]
pub struct Context<S> {
source_cache_id: Option<usize>,
cdf: Option<CircuitDescription<S>>,
}
impl ContextUnit {
pub const fn unit() -> Self {
Self {
source_cache_id: None,
cdf: None,
}
}
}
impl<S> Context<S> {
pub fn with_source_cache_id(&mut self, id: usize) -> &mut Self {
self.source_cache_id.replace(id);
self
}
pub fn take_source_cache_id(&mut self) -> Option<usize> {
self.source_cache_id.take()
}
pub fn with_cdf(cdf: CircuitDescription<S>) -> Self {
Self {
source_cache_id: None,
cdf: Some(cdf),
}
}
pub fn source(&mut self) -> Option<&mut S> {
self.cdf.as_deref_mut()
}
}
impl<S> Context<S>
where
S: io::Read + io::Seek,
{
pub fn fetch_source_path(&mut self, idx: usize) -> io::Result<FixedText<{ Source::PATH_LEN }>> {
self.cdf
.as_mut()
.expect("unreachable empty cdf backend")
.fetch_source(idx)
}
}