1
1
use crate :: ast:: Expr ;
2
2
use crate :: ast:: ExprParams ;
3
3
use ast:: EvaluateResult ;
4
+ use cached:: proc_macro:: cached;
4
5
use lalrpop_util:: lexer:: Token ;
5
6
use lalrpop_util:: ParseError ;
6
7
use num_complex:: Complex64 ;
@@ -83,7 +84,10 @@ fn basic_execution_test() {
83
84
assert ! ( formula_parser:: FormulaParser :: new( ) . parse( "(22)" ) . is_err( ) ) ;
84
85
}
85
86
86
- fn parse_ast < ' a > ( formula : & ' a str ) -> Result < Box < Expr > , ParseError < usize , Token < ' a > , & ' a str > > {
87
+ #[ cached]
88
+ fn parse_ast (
89
+ formula : & ' static str ,
90
+ ) -> Result < Box < Expr < ' static > > , ParseError < usize , Token < ' static > , & ' static str > > {
87
91
formula_parser:: FormulaParser :: new ( ) . parse ( formula)
88
92
}
89
93
@@ -94,7 +98,7 @@ fn parse<'a>(
94
98
single_params : & ' a HashMap < & str , f64 > ,
95
99
rep_params : & ' a HashMap < & str , Vec < f64 > > ,
96
100
) -> Result < Array1 < Complex64 > , Box < dyn error:: Error + ' a > > {
97
- let ast = parse_ast ( formula) ?;
101
+ let ast = parse_ast ( Box :: leak ( formula. to_string ( ) . into_boxed_str ( ) ) ) ?;
98
102
match ast. evaluate ( & mut ExprParams {
99
103
x_axis_name : & x_axis_name,
100
104
x_axis_values : & x_axis_values,
@@ -114,7 +118,7 @@ fn formula_dispersion(_py: Python, m: &PyModule) -> PyResult<()> {
114
118
#[ pyfn( m) ]
115
119
#[ pyo3( name = "get_representation" ) ]
116
120
fn get_representation_py < ' py > ( formula : & str ) -> PyResult < & ' py str > {
117
- let ast = match parse_ast ( formula) {
121
+ let ast = match parse_ast ( Box :: leak ( formula. to_string ( ) . into_boxed_str ( ) ) ) {
118
122
Ok ( ast) => ast,
119
123
Err ( err) => return Err ( PyErr :: new :: < PyTypeError , _ > ( err. to_string ( ) ) ) ,
120
124
} ;
0 commit comments