22
33use iced:: { Task , keyboard, widget} ;
44
5+ use std:: cell:: LazyCell ;
56use std:: collections:: HashMap ;
67
78mod apps;
@@ -30,18 +31,18 @@ struct State {
3031 text_value : String ,
3132 text_id : widget:: Id ,
3233 window_id : Option < iced:: window:: Id > ,
33- modules : HashMap < String , Box < dyn Module > > ,
34+ modules : HashMap < String , LazyCell < Box < dyn Module > > > ,
3435}
3536
3637impl State {
3738 fn new_multi_modal ( ) -> Self {
3839 let start = std:: time:: Instant :: now ( ) ;
39- let mut modules: HashMap < String , Box < dyn Module > > = HashMap :: new ( ) ;
40- modules. insert ( "=" . to_string ( ) , Box :: new ( Calc :: new ( ) ) ) ;
40+ let mut modules: HashMap < String , LazyCell < Box < dyn Module > > > = HashMap :: new ( ) ;
41+ modules. insert ( "=" . to_string ( ) , LazyCell :: new ( || Box :: new ( Calc :: new ( ) ) ) ) ;
4142
42- modules. insert ( "!" . to_string ( ) , Box :: new ( Web :: new ( ) ) ) ;
43+ modules. insert ( "!" . to_string ( ) , LazyCell :: new ( || Box :: new ( Web :: new ( ) ) ) ) ;
4344
44- modules. insert ( "" . to_string ( ) , Box :: new ( AppModule :: new ( ) ) ) ;
45+ modules. insert ( "" . to_string ( ) , LazyCell :: new ( || Box :: new ( AppModule :: new ( ) ) ) ) ;
4546
4647 log:: info!( "Time to initialise modules: {:#?}" , start. elapsed( ) ) ;
4748 State {
@@ -54,15 +55,19 @@ impl State {
5455
5556 fn new_drun ( ) -> Self {
5657 let start = iced:: debug:: time ( "load modules" ) ;
57- let mut modules: HashMap < String , Box < dyn Module > > = HashMap :: new ( ) ;
58-
59- let stdin = std:: io:: stdin ( ) ;
60- let mut lines = Vec :: new ( ) ;
61- for line in stdin. lines ( ) {
62- lines. push ( line. expect ( "Can read line from stdin" ) ) ;
63- }
64-
65- modules. insert ( "" . to_string ( ) , Box :: new ( Drun :: new ( lines) ) ) ;
58+ let mut modules: HashMap < String , LazyCell < Box < dyn Module > > > = HashMap :: new ( ) ;
59+
60+ modules. insert (
61+ "" . to_string ( ) ,
62+ LazyCell :: new ( || {
63+ let stdin = std:: io:: stdin ( ) ;
64+ let mut lines = Vec :: new ( ) ;
65+ for line in stdin. lines ( ) {
66+ lines. push ( line. expect ( "Can read line from stdin" ) ) ;
67+ }
68+ Box :: new ( Drun :: new ( lines) )
69+ } ) ,
70+ ) ;
6671
6772 start. finish ( ) ;
6873
@@ -152,20 +157,24 @@ impl State {
152157 }
153158
154159 #[ allow( clippy:: borrowed_box) ]
155- fn find_module ( & self ) -> Option < ( & Box < dyn Module > , usize ) > {
160+ fn find_module ( & self ) -> Option < ( & LazyCell < Box < dyn Module > > , usize ) > {
156161 self . modules
157162 . iter ( )
158163 . filter ( |( k, _) | self . text_value . starts_with ( k. as_str ( ) ) )
159164 . max_by_key ( |( prefix, _) | prefix. len ( ) )
160165 . map ( |( prefix, m) | ( m, prefix. len ( ) ) )
161166 }
162167
163- fn find_module_mut ( & mut self ) -> Option < ( & mut Box < dyn Module > , usize ) > {
164- self . modules
168+ fn find_module_mut ( & mut self ) -> Option < ( & mut LazyCell < Box < dyn Module > > , usize ) > {
169+ let start = iced:: debug:: time ( "find_module_mut" ) ;
170+ let res = self
171+ . modules
165172 . iter_mut ( )
166173 . filter ( |( k, _) | self . text_value . starts_with ( k. as_str ( ) ) )
167174 . max_by_key ( |( prefix, _) | prefix. len ( ) )
168- . map ( |( prefix, m) | ( m, prefix. len ( ) ) )
175+ . map ( |( prefix, m) | ( m, prefix. len ( ) ) ) ;
176+ start. finish ( ) ;
177+ res
169178 }
170179}
171180
0 commit comments