@@ -339,6 +339,34 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
339
339
fn_name
340
340
}
341
341
342
+ fn gen_eq_chain ( expr : Option < ast:: Expr > , cmp : ast:: Expr ) -> Option < ast:: Expr > {
343
+ match expr {
344
+ Some ( expr) => Some ( make:: expr_op ( ast:: BinOp :: BooleanAnd , expr, cmp) ) ,
345
+ None => Some ( cmp) ,
346
+ }
347
+ }
348
+
349
+ fn gen_record_pat_field ( field_name : & str , pat_name : & str ) -> ast:: RecordPatField {
350
+ let pat = make:: ext:: simple_ident_pat ( make:: name ( & pat_name) ) ;
351
+ let name_ref = make:: name_ref ( field_name) ;
352
+ let field = make:: record_pat_field ( name_ref, pat. into ( ) ) ;
353
+ field
354
+ }
355
+
356
+ fn gen_record_pat (
357
+ record_name : ast:: Path ,
358
+ r_fields : Vec < ast:: RecordPatField > ,
359
+ ) -> ast:: RecordPat {
360
+ let list = make:: record_pat_field_list ( r_fields) ;
361
+ make:: record_pat_with_fields ( record_name, list)
362
+ }
363
+
364
+ fn gen_variant_path ( variant : & ast:: Variant ) -> Option < ast:: Path > {
365
+ let first = make:: ext:: ident_path ( "Self" ) ;
366
+ let second = make:: path_from_text ( & variant. name ( ) ?. to_string ( ) ) ;
367
+ let record_name = make:: path_concat ( first, second) ;
368
+ Some ( record_name)
369
+ }
342
370
// FIXME: return `None` if the trait carries a generic type; we can only
343
371
// generate this code `Self` for the time being.
344
372
@@ -364,52 +392,31 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
364
392
let mut expr = None ;
365
393
let mut l_fields = vec ! [ ] ;
366
394
let mut r_fields = vec ! [ ] ;
367
- // let mut fields = vec![];
368
-
369
- // !! make::record_pat_field{list, etc};
370
395
371
396
for field in list. fields ( ) {
372
397
let field_name = field. name ( ) ?. to_string ( ) ;
373
398
374
399
let l_name = & format ! ( "l_{}" , field_name) ;
375
- let pat = make:: ext:: simple_ident_pat ( make:: name ( & l_name) ) ;
376
- let name_ref = make:: name_ref ( & field_name) ;
377
- let field = make:: record_pat_field ( name_ref, pat. into ( ) ) ;
378
- l_fields. push ( field) ;
400
+ l_fields. push ( gen_record_pat_field ( & field_name, & l_name) ) ;
379
401
380
- let r_name = & format ! ( "r_{}" , field_name) ;
381
- let pat = make:: ext:: simple_ident_pat ( make:: name ( & r_name) ) ;
382
- let name_ref = make:: name_ref ( & field_name) ;
383
- let field = make:: record_pat_field ( name_ref, pat. into ( ) ) ;
384
- r_fields. push ( field) ;
402
+ let r_name = & format ! ( "l_{}" , field_name) ;
403
+ r_fields. push ( gen_record_pat_field ( & field_name, & r_name) ) ;
385
404
386
405
let lhs = make:: expr_path ( make:: ext:: ident_path ( l_name) ) ;
387
406
let rhs = make:: expr_path ( make:: ext:: ident_path ( r_name) ) ;
388
407
let cmp = make:: expr_op ( ast:: BinOp :: EqualityTest , lhs, rhs) ;
389
- expr = match expr {
390
- Some ( expr) => {
391
- Some ( make:: expr_op ( ast:: BinOp :: BooleanAnd , expr, cmp) )
392
- }
393
- None => Some ( cmp) ,
394
- } ;
408
+ expr = gen_eq_chain ( expr, cmp) ;
395
409
}
396
- let first = make:: ext:: ident_path ( "Self" ) ;
397
- let second = make:: path_from_text ( & variant. name ( ) ?. to_string ( ) ) ;
398
- let record_name = make:: path_concat ( first, second) ;
399
- let list = make:: record_pat_field_list ( l_fields) ;
400
- let l_record = make:: record_pat_with_fields ( record_name, list) ;
401
-
402
- let first = make:: ext:: ident_path ( "Self" ) ;
403
- let second = make:: path_from_text ( & variant. name ( ) ?. to_string ( ) ) ;
404
- let record_name = make:: path_concat ( first, second) ;
405
- let list = make:: record_pat_field_list ( r_fields) ;
406
- let r_record = make:: record_pat_with_fields ( record_name, list) ;
407
410
411
+ let l_record = gen_record_pat ( gen_variant_path ( & variant) ?, l_fields) ;
412
+ let r_record = gen_record_pat ( gen_variant_path ( & variant) ?, r_fields) ;
408
413
let tuple = make:: tuple_pat ( vec ! [ l_record. into( ) , r_record. into( ) ] ) ;
414
+
409
415
if let Some ( expr) = expr {
410
416
arms. push ( make:: match_arm ( Some ( tuple. into ( ) ) , None , expr) ) ;
411
417
}
412
418
}
419
+
413
420
// todo!("implement tuple record iteration")
414
421
Some ( ast:: FieldList :: TupleFieldList ( list) ) => {
415
422
todo ! ( "implement tuple enum iteration" )
@@ -453,10 +460,7 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
453
460
let rhs = make:: expr_path ( make:: ext:: ident_path ( "other" ) ) ;
454
461
let rhs = make:: expr_field ( rhs, & field. name ( ) ?. to_string ( ) ) ;
455
462
let cmp = make:: expr_op ( ast:: BinOp :: EqualityTest , lhs, rhs) ;
456
- expr = match expr {
457
- Some ( expr) => Some ( make:: expr_op ( ast:: BinOp :: BooleanAnd , expr, cmp) ) ,
458
- None => Some ( cmp) ,
459
- } ;
463
+ expr = gen_eq_chain ( expr, cmp) ;
460
464
}
461
465
make:: block_expr ( None , expr) . indent ( ast:: edit:: IndentLevel ( 1 ) )
462
466
}
@@ -470,10 +474,7 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
470
474
let rhs = make:: expr_path ( make:: ext:: ident_path ( "other" ) ) ;
471
475
let rhs = make:: expr_field ( rhs, & idx) ;
472
476
let cmp = make:: expr_op ( ast:: BinOp :: EqualityTest , lhs, rhs) ;
473
- expr = match expr {
474
- Some ( expr) => Some ( make:: expr_op ( ast:: BinOp :: BooleanAnd , expr, cmp) ) ,
475
- None => Some ( cmp) ,
476
- } ;
477
+ expr = gen_eq_chain ( expr, cmp) ;
477
478
}
478
479
make:: block_expr ( None , expr) . indent ( ast:: edit:: IndentLevel ( 1 ) )
479
480
}
0 commit comments