@@ -11,44 +11,40 @@ type Selector = String;
1111#[ derive( Debug , Clone ) ]
1212pub struct SingleTestGasInfo {
1313 pub gas_used : GasVector ,
14- pub report_data : ReportData ,
14+ pub report_data : Option < ReportData > ,
1515}
1616
1717#[ derive( Debug , Clone , Default ) ]
1818pub struct ReportData ( BTreeMap < ContractName , ContractInfo > ) ;
1919
2020#[ derive( Debug , Clone , Default ) ]
2121pub struct ContractInfo {
22- pub gas_used : GasVector ,
23- pub functions : BTreeMap < Selector , SelectorReportData > ,
22+ pub ( super ) gas_used : GasVector ,
23+ pub ( super ) functions : BTreeMap < Selector , SelectorReportData > ,
2424}
2525
2626#[ derive( Debug , Clone , Default ) ]
2727pub struct SelectorReportData {
28- pub gas_stats : GasStats ,
29- pub n_calls : u64 ,
30- pub records : Vec < u64 > ,
28+ pub ( super ) gas_stats : GasStats ,
29+ pub ( super ) n_calls : u64 ,
30+ pub ( super ) records : Vec < u64 > ,
3131}
3232
3333impl SingleTestGasInfo {
3434 #[ must_use]
35- pub fn new ( gas_used : GasVector ) -> Self {
35+ pub ( crate ) fn new ( gas_used : GasVector ) -> Self {
3636 Self {
3737 gas_used,
38- report_data : ReportData :: default ( ) ,
38+ report_data : None ,
3939 }
4040 }
4141
42- #[ must_use]
43- pub fn new_with_report (
44- gas_used : GasVector ,
45- call_trace : & CallTrace ,
42+ pub ( crate ) fn get_with_report_data (
43+ self ,
44+ trace : & CallTrace ,
4645 contracts_data : & ContractsDataStore ,
4746 ) -> Self {
48- Self :: new ( gas_used) . collect_gas_data ( call_trace, contracts_data)
49- }
50-
51- fn collect_gas_data ( mut self , trace : & CallTrace , contracts_data : & ContractsDataStore ) -> Self {
47+ let mut report_data = ReportData :: default ( ) ;
5248 let mut stack = trace. nested_calls . clone ( ) ;
5349
5450 while let Some ( call_trace_node) = stack. pop ( ) {
@@ -66,21 +62,27 @@ impl SingleTestGasInfo {
6662 . expect ( "Gas report data must be updated after test execution" )
6763 . get_gas ( ) ;
6864
69- self . update_entry ( contract_name, selector, gas) ;
65+ report_data . update_entry ( contract_name, selector, gas) ;
7066 stack. extend ( call. nested_calls . clone ( ) ) ;
7167 }
7268 }
73- self . finalize ( ) ;
74- self
69+ report_data. finalize ( ) ;
70+
71+ Self {
72+ gas_used : self . gas_used ,
73+ report_data : Some ( report_data) ,
74+ }
7575 }
76+ }
7677
78+ impl ReportData {
7779 fn update_entry (
7880 & mut self ,
7981 contract_name : ContractName ,
8082 selector : Selector ,
8183 gas_used : GasVector ,
8284 ) {
83- let contract_info = self . report_data . 0 . entry ( contract_name) . or_default ( ) ;
85+ let contract_info = self . 0 . entry ( contract_name) . or_default ( ) ;
8486
8587 let current_gas = contract_info. gas_used ;
8688 contract_info. gas_used = current_gas. checked_add ( gas_used) . unwrap_or_else ( || {
@@ -93,7 +95,7 @@ impl SingleTestGasInfo {
9395 }
9496
9597 fn finalize ( & mut self ) {
96- for contract_info in self . report_data . 0 . values_mut ( ) {
98+ for contract_info in self . 0 . values_mut ( ) {
9799 for gas_info in contract_info. functions . values_mut ( ) {
98100 gas_info. gas_stats = GasStats :: new ( & gas_info. records ) ;
99101 }
0 commit comments