This repository has been archived by the owner on Dec 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathscope.rs
159 lines (135 loc) · 5.21 KB
/
scope.rs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use crate::prelude::*;
use crate::dirty;
use crate::data::function::callback::*;
use crate::display::symbol::attribute as attr;
use crate::display::symbol::attribute::IsAttribute;
use crate::display::symbol::attribute::Shape;
use crate::system::web::fmt;
use crate::system::web::group;
use crate::system::web::Logger;
use crate::closure;
use crate::data::opt_vec::OptVec;
#[derive(Derivative)]
#[derivative(Debug(bound="Ix: Debug"))]
pub struct TypedIndex<Ix, T> {
pub ix : Ix,
phantom : PhantomData<T>
}
impl<Ix, T> TypedIndex<Ix, T> {
pub fn unsafe_new(ix: Ix) -> Self {
let phantom = PhantomData;
Self { ix, phantom }
}
}
// =============
// === Scope ===
// =============
// === Definition ===
#[derive(Derivative)]
#[derivative(Debug(bound=""))]
pub struct Scope <OnDirty> {
pub attributes : OptVec<AnyAttribute<OnDirty>>,
pub attribute_dirty : AttributeDirty<OnDirty>,
pub shape_dirty : ShapeDirty<OnDirty>,
pub name_map : HashMap <AttributeName, AnyAttributeIndex>,
pub logger : Logger,
}
// === Types ===
pub type AnyAttributeIndex = usize;
pub type AttributeIndex <T, OnDirty> = TypedIndex<usize, Attribute<T, OnDirty>>;
pub type AttributeName = String;
pub type AttributeDirty <OnDirty> = dirty::SharedBitField<u64, OnDirty>;
pub type ShapeDirty <OnDirty> = dirty::SharedBool<OnDirty>;
pub type Attribute<T, OnDirty> = attr::Attribute
< T
, Closure_attribute_on_set_handler<OnDirty>
, Closure_attribute_on_resize_handler<OnDirty>
>;
pub type AnyAttribute<OnDirty> = attr::AnyAttribute
< Closure_attribute_on_set_handler<OnDirty>
, Closure_attribute_on_resize_handler<OnDirty>
>;
// === Callbacks ===
closure!(attribute_on_set_handler<Callback: Callback0>
(dirty: AttributeDirty<Callback>, ix: usize) || { dirty.set(ix) });
closure!(attribute_on_resize_handler<Callback: Callback0>
(dirty: ShapeDirty<Callback>) || { dirty.set() });
// === Implementation ===
impl<OnDirty: Clone> Scope<OnDirty> {
pub fn new(logger: Logger, on_dirty: OnDirty) -> Self {
logger.info("Initializing.");
let on_dirty2 = on_dirty.clone();
let attr_logger = logger.sub("attr_dirty");
let shape_logger = logger.sub("shape_dirty");
let attribute_dirty = AttributeDirty::new(on_dirty2, attr_logger);
let shape_dirty = ShapeDirty::new(on_dirty, shape_logger);
let attributes = default();
let name_map = default();
Self { attributes, attribute_dirty, shape_dirty, name_map, logger }
}
}
impl<OnDirty: Callback0 + 'static> Scope<OnDirty> {
pub fn add_attribute<Name: Str, T: Shape>
( &mut self
, name: Name
, bldr: attr::Builder<T>
) -> AttributeIndex<T, OnDirty>
where AnyAttribute<OnDirty>: From<Attribute<T, OnDirty>> {
let ix = self._add_attribute(name, bldr);
AttributeIndex::<T, OnDirty>::unsafe_new(ix)
}
fn _add_attribute<Name: Str, T: Shape>
(&mut self, name: Name, bldr: attr::Builder<T>) -> AnyAttributeIndex
where AnyAttribute<OnDirty>: From<Attribute<T, OnDirty>> {
let name = name.as_ref().to_string();
let bldr = bldr.logger(self.logger.sub(&name));
let ix = self.attributes.len();
let attr_dirty = self.attribute_dirty.clone();
let shape_dirty = self.shape_dirty.clone();
let logger = &self.logger;
let ix = self.attributes.insert_with_ix(|ix| {
group!(logger, format!("Adding attribute '{}' at index {}.", name, ix), {
let on_set = attribute_on_set_handler(attr_dirty, ix);
let on_resize = attribute_on_resize_handler(shape_dirty);
let attr = Attribute::build(bldr, on_set, on_resize);
AnyAttribute::from(attr)
})
});
self.name_map.insert(name, ix);
self.shape_dirty.set();
ix
}
pub fn add_instance(&mut self) {
// self.attributes.iter_mut().for_each(|attr| attr.add_element());
// let max_size = self.attributes.iter().fold(0, |s, t| s + t.len());
unimplemented!()
}
}
// impl<T, OnDirty> Index<AttributeIndex<T, OnDirty>> for Scope<OnDirty> {
// type Output = Attribute<T, OnDirty>;
// fn index(&self, ix: AttributeIndex<T, OnDirty>) -> &Self::Output {
// unimplemented!()
// // self.attributes.index(ix)
// }
// }
impl<T, OnDirty>
Index<TypedIndex<usize, T>> for Scope<OnDirty>
where for<'t> &'t T: TryFrom<&'t AnyAttribute<OnDirty>> {
type Output = T;
fn index(&self, t: TypedIndex<usize, T>) -> &Self::Output {
self.attributes.index(t.ix).try_into().ok().unwrap()
}
}
impl<T, OnDirty>
IndexMut<TypedIndex<usize, T>> for Scope<OnDirty>
where for<'t> &'t T: TryFrom<&'t AnyAttribute<OnDirty>>,
for<'t> &'t mut T: TryFrom<&'t mut AnyAttribute<OnDirty>> {
fn index_mut(&mut self, t: TypedIndex<usize, T>) -> &mut Self::Output {
self.attributes.index_mut(t.ix).try_into().ok().unwrap()
}
}
// impl IndexMut<usize> for WorldData {
// fn index_mut(&mut self, ix: usize) -> &mut Self::Output {
// self.workspaces.index_mut(ix)
// }
// }