Skip to content

Commit 2be7f98

Browse files
committed
Added "Maybe<T>" value. This replaces Value<Option<T>> with Value<Maybe<T>>
1 parent 98b2611 commit 2be7f98

File tree

25 files changed

+502
-388
lines changed

25 files changed

+502
-388
lines changed

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ anathema-store = { path = "./anathema-store" }
2222
anathema-templates = { path = "./anathema-templates" }
2323
anathema-widgets = { path = "./anathema-widgets" }
2424
anathema-geometry = { path = "./anathema-geometry" }
25-
anathema-strings = { path = "./anathema-strings" }
2625
anathema-value-resolver = { path = "./anathema-value-resolver" }
2726

2827
[features]
2928
default = []
3029
profile = ["anathema-runtime/profile", "anathema-widgets/profile", "anathema-backend/profile"]
31-
debuggy = ["anathema-widgets/debuggy"]
30+
filelog = ["anathema-debug/filelog"]
3231

3332
[lints]
3433
workspace = true
@@ -54,7 +53,6 @@ members = [
5453
"anathema-state",
5554
"anathema-state-derive",
5655
"anathema-store",
57-
"anathema-strings",
5856
"anathema-templates",
5957
"anathema-widgets",
6058
"anathema-value-resolver",

anathema-debug/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ edition.workspace = true
55

66
[dependencies]
77

8+
[features]
9+
default = []
10+
filelog = []
11+
812
[lints]
913
workspace = true

anathema-debug/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,40 @@ impl<O: Write> Debug<O> {
3636
self.0
3737
}
3838
}
39+
40+
#[cfg(feature = "filelog")]
41+
pub mod macros {
42+
#[macro_export]
43+
macro_rules! debug_to_file {
44+
($($arg:tt)*) => {
45+
use ::std::io::Write as _;
46+
let mut file = std::fs::OpenOptions::new().create(true).append(true).open("/tmp/log.lol").unwrap();
47+
let payload = format!($($arg)*);
48+
file.write_all(payload.as_bytes()).unwrap();
49+
file.write(b"\n").unwrap();
50+
file.flush();
51+
}
52+
}
53+
54+
#[macro_export]
55+
macro_rules! debug_tree {
56+
($tree:expr) => {
57+
let mut d = $crate::tree::debug::DebugTree::new();
58+
$tree.apply_visitor(&mut d);
59+
$crate::debug_to_file!("{}", d.output);
60+
};
61+
}
62+
}
63+
64+
#[cfg(not(feature = "filelog"))]
65+
pub mod macros {
66+
#[macro_export]
67+
macro_rules! debug_to_file {
68+
($($arg:tt)*) => {};
69+
}
70+
71+
#[macro_export]
72+
macro_rules! debug_tree {
73+
($tree:expr) => {};
74+
}
75+
}

anathema-runtime/src/runtime/mod.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -439,17 +439,20 @@ impl<'rt, 'bp, G: GlobalEventHandler> Frame<'rt, 'bp, G> {
439439
return;
440440
};
441441

442-
let Some(remote_state) = self.layout_ctx.states.get(event.state) else { return };
443-
let Some(remote_state) = remote_state.shared_state() else { return };
444-
self.with_component(widget_id, state_id, |comp, children, ctx| {
445-
// TODO:
446-
// Create a new event type
447-
// if the event is stopped then return from drain_assoc_events
448-
449-
let event_ident = self.document.strings.get_ref_unchecked(event.external);
450-
comp.dyn_component
451-
.any_receive(children, ctx, event_ident, &*remote_state);
452-
});
442+
panic!(
443+
"this is going to be reworked. this was working while SharedState didn't have a lifetime, it does now so... figure it out"
444+
);
445+
// let Some(remote_state) = self.layout_ctx.states.get(event.state) else { return };
446+
// let Some(remote_state) = remote_state.shared_state() else { return };
447+
// self.with_component(widget_id, state_id, |comp, children, ctx| {
448+
// // TODO:
449+
// // Create a new event type
450+
// // if the event is stopped then return from drain_assoc_events
451+
452+
// let event_ident = self.document.strings.get_ref_unchecked(event.external);
453+
// comp.dyn_component
454+
// .any_receive(children, ctx, event_ident, &*remote_state);
455+
// });
453456
}
454457
}
455458

anathema-state/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use anathema_store::slab::Key;
55

66
pub use crate::colors::{Color, FromColor};
77
pub use crate::numbers::Number;
8-
pub use crate::states::{AnyMap, AnyState, State, StateId, States, TypeId};
8+
pub use crate::states::{AnyMap, State, StateId, States, TypeId};
99
pub use crate::store::watchers::Watcher;
1010
pub use crate::store::{
1111
Change, Changes, SubTo, Subscriber, Watched, clear_all_changes, clear_all_subs, drain_changes, drain_watchers,
1212
};
13-
pub use crate::value::{List, Map, PendingValue, SharedState, Type, Value, ValueRef};
13+
pub use crate::value::{List, Map, Maybe, Nullable, PendingValue, SharedState, Type, Value, ValueRef};
1414

1515
mod colors;
1616
mod numbers;

anathema-state/src/states.rs

Lines changed: 18 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl SlabIndex for StateId {
6363
}
6464
}
6565

66-
pub trait State: 'static {
66+
pub trait State: Any + 'static {
6767
fn type_info(&self) -> Type;
6868

6969
fn as_int(&self) -> Option<i64> {
@@ -101,134 +101,23 @@ pub trait State: 'static {
101101
fn as_any_list(&self) -> Option<&dyn AnyList> {
102102
None
103103
}
104-
}
105-
106-
impl<T: State> AnyState for T {
107-
fn to_any_ref(&self) -> &dyn Any {
108-
self
109-
}
110-
111-
fn to_any_mut(&mut self) -> &mut dyn Any {
112-
self
113-
}
114-
115-
fn type_info(&self) -> Type {
116-
<Self as State>::type_info(self)
117-
}
118-
119-
fn as_int(&self) -> Option<i64> {
120-
<Self as State>::as_int(self)
121-
}
122-
123-
fn as_float(&self) -> Option<f64> {
124-
<Self as State>::as_float(self)
125-
}
126-
127-
fn as_hex(&self) -> Option<Hex> {
128-
<Self as State>::as_hex(self)
129-
}
130-
131-
fn as_color(&self) -> Option<Color> {
132-
<Self as State>::as_color(self)
133-
}
134-
135-
fn as_char(&self) -> Option<char> {
136-
<Self as State>::as_char(self)
137-
}
138-
139-
fn as_str(&self) -> Option<&str> {
140-
<Self as State>::as_str(self)
141-
}
142-
143-
fn as_bool(&self) -> Option<bool> {
144-
<Self as State>::as_bool(self)
145-
}
146-
147-
fn as_any_map(&self) -> Option<&dyn AnyMap> {
148-
<Self as State>::as_any_map(self)
149-
}
150-
151-
fn as_any_list(&self) -> Option<&dyn AnyList> {
152-
<Self as State>::as_any_list(self)
153-
}
154-
}
155-
156-
pub trait AnyState: 'static {
157-
fn type_info(&self) -> Type;
158-
159-
fn to_any_ref(&self) -> &dyn Any;
160-
161-
fn to_any_mut(&mut self) -> &mut dyn Any;
162-
163-
fn as_int(&self) -> Option<i64> {
164-
None
165-
}
166-
167-
fn as_float(&self) -> Option<f64> {
168-
None
169-
}
170-
171-
fn as_hex(&self) -> Option<Hex> {
172-
None
173-
}
174-
175-
fn as_color(&self) -> Option<Color> {
176-
None
177-
}
178-
179-
fn as_char(&self) -> Option<char> {
180-
None
181-
}
182-
183-
fn as_str(&self) -> Option<&str> {
184-
None
185-
}
186104

187-
fn as_bool(&self) -> Option<bool> {
105+
fn as_maybe(&self) -> Option<&dyn AnyMaybe> {
188106
None
189107
}
190-
191-
fn as_any_map(&self) -> Option<&dyn AnyMap> {
192-
None
193-
}
194-
195-
fn as_any_list(&self) -> Option<&dyn AnyList> {
196-
None
197-
}
198-
}
199-
200-
impl dyn AnyState {
201-
pub fn try_to<T: 'static>(&self) -> Option<&T> {
202-
self.to_any_ref().downcast_ref()
203-
}
204-
205-
pub fn to<T: 'static>(&self) -> &T {
206-
match self.try_to() {
207-
Some(val) => val,
208-
None => panic!("invalid type"),
209-
}
210-
}
211108
}
212109

213-
impl Debug for dyn AnyState {
110+
impl Debug for dyn State {
214111
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
215-
write!(f, "<AnyState ({:?})>", self.type_info())
112+
write!(f, "<State ({:?})>", self.type_info())
216113
}
217114
}
218115

219-
impl AnyState for Box<dyn AnyState> {
116+
impl State for Box<dyn State> {
220117
fn type_info(&self) -> Type {
221118
self.as_ref().type_info()
222119
}
223120

224-
fn to_any_ref(&self) -> &dyn Any {
225-
self.as_ref().to_any_ref()
226-
}
227-
228-
fn to_any_mut(&mut self) -> &mut dyn Any {
229-
self.as_mut().to_any_mut()
230-
}
231-
232121
fn as_int(&self) -> Option<i64> {
233122
self.as_ref().as_int()
234123
}
@@ -264,6 +153,10 @@ impl AnyState for Box<dyn AnyState> {
264153
fn as_any_list(&self) -> Option<&dyn AnyList> {
265154
self.as_ref().as_any_list()
266155
}
156+
157+
fn as_maybe(&self) -> Option<&dyn AnyMaybe> {
158+
self.as_ref().as_maybe()
159+
}
267160
}
268161

269162
pub trait AnyMap {
@@ -282,50 +175,8 @@ pub trait AnyList {
282175
}
283176
}
284177

285-
// -----------------------------------------------------------------------------
286-
// - State implementation... -
287-
// State implementation for primitives and non-state types
288-
// -----------------------------------------------------------------------------
289-
impl<T: State + TypeId> State for Option<T> {
290-
fn type_info(&self) -> Type {
291-
T::TYPE
292-
}
293-
294-
fn as_int(&self) -> Option<i64> {
295-
self.as_ref()?.as_int()
296-
}
297-
298-
fn as_float(&self) -> Option<f64> {
299-
self.as_ref()?.as_float()
300-
}
301-
302-
fn as_hex(&self) -> Option<Hex> {
303-
self.as_ref()?.as_hex()
304-
}
305-
306-
fn as_color(&self) -> Option<Color> {
307-
self.as_ref()?.as_color()
308-
}
309-
310-
fn as_char(&self) -> Option<char> {
311-
self.as_ref()?.as_char()
312-
}
313-
314-
fn as_str(&self) -> Option<&str> {
315-
self.as_ref()?.as_str()
316-
}
317-
318-
fn as_bool(&self) -> Option<bool> {
319-
self.as_ref()?.as_bool()
320-
}
321-
322-
fn as_any_map(&self) -> Option<&dyn AnyMap> {
323-
self.as_ref()?.as_any_map()
324-
}
325-
326-
fn as_any_list(&self) -> Option<&dyn AnyList> {
327-
self.as_ref()?.as_any_list()
328-
}
178+
pub trait AnyMaybe {
179+
fn get(&self) -> Option<PendingValue>;
329180
}
330181

331182
macro_rules! impl_num_state {
@@ -445,29 +296,30 @@ impl_float_state!(f64);
445296

446297
#[derive(Debug)]
447298
pub struct States {
448-
inner: Slab<StateId, Value<Box<dyn AnyState>>>,
299+
inner: Slab<StateId, Value<Box<dyn State>>>,
449300
}
450301

451302
impl States {
452303
pub fn new() -> Self {
453304
Self { inner: Slab::empty() }
454305
}
455306

456-
pub fn insert(&mut self, state: Value<Box<dyn AnyState>>) -> StateId {
307+
pub fn insert(&mut self, state: Box<dyn State>) -> StateId {
308+
let state = Value::from_box(state);
457309
self.inner.insert(state)
458310
}
459311

460-
pub fn get(&self, state_id: impl Into<StateId>) -> Option<&Value<Box<dyn AnyState>>> {
312+
pub fn get(&self, state_id: impl Into<StateId>) -> Option<&Value<Box<dyn State>>> {
461313
self.inner.get(state_id.into()).map(|b| b)
462314
}
463315

464-
pub fn get_mut(&mut self, state_id: impl Into<StateId>) -> Option<&mut Value<Box<dyn AnyState>>> {
316+
pub fn get_mut(&mut self, state_id: impl Into<StateId>) -> Option<&mut Value<Box<dyn State>>> {
465317
self.inner.get_mut(state_id.into())
466318
}
467319

468320
pub fn with_mut<F, U>(&mut self, index: impl Into<StateId>, f: F) -> U
469321
where
470-
F: FnOnce(&mut dyn AnyState, &mut Self) -> U,
322+
F: FnOnce(&mut dyn State, &mut Self) -> U,
471323
{
472324
let mut ticket = self.inner.checkout(index.into());
473325
let ret = f(&mut *ticket.to_mut(), self);
@@ -480,7 +332,7 @@ impl States {
480332
/// # Panics
481333
///
482334
/// Will panic if the state does not exist.
483-
pub fn remove(&mut self, state_id: StateId) -> Value<Box<dyn AnyState>> {
335+
pub fn remove(&mut self, state_id: StateId) -> Value<Box<dyn State>> {
484336
self.inner.remove(state_id)
485337
}
486338
}

anathema-state/src/store/change.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub(crate) fn changed(key: ValueKey, change: Change) {
3434
return;
3535
}
3636

37+
anathema_debug::debug_to_file!("changed {:?}", key);
3738
CHANGES.with_borrow_mut(|changes| {
3839
changes.push((subscribers, change));
3940
});

anathema-state/src/store/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ impl ValueKey {
5353
9 => Type::Composite,
5454
10 => Type::Unit,
5555
11 => Type::Color,
56+
12 => Type::Maybe,
5657
_ => unreachable!("corrupt type information"),
5758
}
5859
}

0 commit comments

Comments
 (0)