@@ -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
269162pub 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
331182macro_rules! impl_num_state {
@@ -445,29 +296,30 @@ impl_float_state!(f64);
445296
446297#[ derive( Debug ) ]
447298pub struct States {
448- inner : Slab < StateId , Value < Box < dyn AnyState > > > ,
299+ inner : Slab < StateId , Value < Box < dyn State > > > ,
449300}
450301
451302impl 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}
0 commit comments