1- use std:: collections:: { BTreeMap , BTreeSet } ;
1+ use std:: {
2+ collections:: { BTreeMap , BTreeSet } ,
3+ fmt:: { self , Display , Formatter } ,
4+ } ;
25
36use utils:: ToUsize ;
47
@@ -62,7 +65,7 @@ impl TryInto<VarOrConstMallocAccess> for SimpleExpr {
6265 malloc_label,
6366 offset,
6467 } ) ,
65- _ => Err ( ( ) ) ,
68+ Self :: Constant ( _ ) => Err ( ( ) ) ,
6669 }
6770 }
6871}
@@ -1242,6 +1245,11 @@ fn replace_vars_by_const_in_lines(lines: &mut [Line], map: &BTreeMap<Var, F>) {
12421245 }
12431246 Line :: FunctionCall {
12441247 args, return_data, ..
1248+ }
1249+ | Line :: Precompile {
1250+ precompile : _,
1251+ args,
1252+ res : return_data,
12451253 } => {
12461254 for arg in args {
12471255 replace_vars_by_const_in_expr ( arg, map) ;
@@ -1285,18 +1293,6 @@ fn replace_vars_by_const_in_lines(lines: &mut [Line], map: &BTreeMap<Var, F>) {
12851293 replace_vars_by_const_in_expr ( ret, map) ;
12861294 }
12871295 }
1288- Line :: Precompile {
1289- precompile : _,
1290- args,
1291- res : return_data,
1292- } => {
1293- for arg in args {
1294- replace_vars_by_const_in_expr ( arg, map) ;
1295- }
1296- for r in return_data {
1297- assert ! ( !map. contains_key( r) , "Return variable {r} is a constant" ) ;
1298- }
1299- }
13001296 Line :: Print { content, .. } => {
13011297 for var in content {
13021298 replace_vars_by_const_in_expr ( var, map) ;
@@ -1315,21 +1311,21 @@ fn replace_vars_by_const_in_lines(lines: &mut [Line], map: &BTreeMap<Var, F>) {
13151311 }
13161312}
13171313
1318- impl ToString for SimpleLine {
1319- fn to_string ( & self ) -> String {
1320- self . to_string_with_indent ( 0 )
1314+ impl Display for SimpleLine {
1315+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt :: Result {
1316+ f . write_str ( & self . to_string_with_indent ( 0 ) )
13211317 }
13221318}
13231319
1324- impl ToString for VarOrConstMallocAccess {
1325- fn to_string ( & self ) -> String {
1320+ impl Display for VarOrConstMallocAccess {
1321+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt :: Result {
13261322 match self {
1327- Self :: Var ( var) => var . to_string ( ) ,
1323+ Self :: Var ( var) => f . write_str ( var ) ,
13281324 Self :: ConstMallocAccess {
13291325 malloc_label,
13301326 offset,
13311327 } => {
1332- format ! ( "ConstMallocAccess({malloc_label}, {offset})" )
1328+ write ! ( f , "ConstMallocAccess({malloc_label}, {offset})" )
13331329 }
13341330 }
13351331 }
@@ -1345,7 +1341,7 @@ impl SimpleLine {
13451341 arg0,
13461342 arg1,
13471343 } => {
1348- format ! ( "{} = {} {} {}" , var . to_string ( ) , arg0 , operation , arg1 )
1344+ format ! ( "{var } = {arg0 } {operation } {arg1}" )
13491345 }
13501346 Self :: DecomposeBits {
13511347 var : result,
@@ -1463,45 +1459,46 @@ impl SimpleLine {
14631459 }
14641460}
14651461
1466- impl ToString for SimpleFunction {
1467- fn to_string ( & self ) -> String {
1462+ impl Display for SimpleFunction {
1463+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt :: Result {
14681464 let args_str = self
14691465 . arguments
14701466 . iter ( )
14711467 . map ( std:: string:: ToString :: to_string)
14721468 . collect :: < Vec < _ > > ( )
14731469 . join ( ", " ) ;
14741470
1475- let instructions_str = self
1476- . instructions
1477- . iter ( )
1478- . map ( |line| line. to_string_with_indent ( 1 ) )
1479- . collect :: < Vec < _ > > ( )
1480- . join ( "\n " ) ;
1481-
14821471 if self . instructions . is_empty ( ) {
1483- format ! (
1472+ write ! (
1473+ f,
14841474 "fn {}({}) -> {} {{}}" ,
14851475 self . name, args_str, self . n_returned_vars
14861476 )
14871477 } else {
1488- format ! (
1489- "fn {}({}) -> {} {{\n {}\n }}" ,
1490- self . name, args_str, self . n_returned_vars, instructions_str
1491- )
1478+ writeln ! (
1479+ f,
1480+ "fn {}({}) -> {} {{" ,
1481+ self . name, args_str, self . n_returned_vars
1482+ ) ?;
1483+ for ( i, line) in self . instructions . iter ( ) . enumerate ( ) {
1484+ if i > 0 {
1485+ writeln ! ( f) ?;
1486+ }
1487+ write ! ( f, "{}" , line. to_string_with_indent( 1 ) ) ?;
1488+ }
1489+ write ! ( f, "\n }}" )
14921490 }
14931491 }
14941492}
14951493
1496- impl ToString for SimpleProgram {
1497- fn to_string ( & self ) -> String {
1498- let mut result = String :: new ( ) ;
1494+ impl Display for SimpleProgram {
1495+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
14991496 for ( i, function) in self . functions . values ( ) . enumerate ( ) {
15001497 if i > 0 {
1501- result . push ( '\n' ) ;
1498+ writeln ! ( f ) ? ;
15021499 }
1503- result . push_str ( & function. to_string ( ) ) ;
1500+ write ! ( f , "{ function}" ) ? ;
15041501 }
1505- result
1502+ Ok ( ( ) )
15061503 }
15071504}
0 commit comments