@@ -330,6 +330,15 @@ fn gen_hash_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
330
330
331
331
/// Generate a `PartialEq` impl based on the fields and members of the target type.
332
332
fn gen_partial_eq ( adt : & ast:: Adt , func : & ast:: Fn ) -> Option < ( ) > {
333
+ fn gen_discriminant ( ) -> ast:: Expr {
334
+ let root = make:: ext:: ident_path ( "core" ) ;
335
+ let submodule = make:: ext:: ident_path ( "mem" ) ;
336
+ let fn_name = make:: ext:: ident_path ( "discriminant" ) ;
337
+ let fn_name = make:: path_concat ( submodule, fn_name) ;
338
+ let fn_name = make:: expr_path ( make:: path_concat ( root, fn_name) ) ;
339
+ fn_name
340
+ }
341
+
333
342
// FIXME: return `None` if the trait carries a generic type; we can only
334
343
// generate this code `Self` for the time being.
335
344
@@ -338,9 +347,16 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
338
347
ast:: Adt :: Union ( _) => return None ,
339
348
340
349
// FIXME: generate trait variants
341
- ast:: Adt :: Enum ( _) => todo ! ( ) ,
350
+ ast:: Adt :: Enum ( enum_) => {
351
+ // => std::mem::discriminant(self) == std::mem::discriminant(other)
352
+ let lhs = make:: expr_path ( make:: ext:: ident_path ( "self" ) ) ;
353
+ let lhs = make:: expr_call ( gen_discriminant ( ) , make:: arg_list ( Some ( lhs) ) ) ;
354
+ let rhs = make:: expr_path ( make:: ext:: ident_path ( "other" ) ) ;
355
+ let rhs = make:: expr_call ( gen_discriminant ( ) , make:: arg_list ( Some ( rhs) ) ) ;
356
+ let cmp = make:: expr_op ( ast:: BinOp :: EqualityTest , lhs, rhs) ;
357
+ make:: block_expr ( None , Some ( cmp) ) . indent ( ast:: edit:: IndentLevel ( 1 ) )
358
+ }
342
359
ast:: Adt :: Struct ( strukt) => match strukt. field_list ( ) {
343
- // => self.<field>.hash(state);
344
360
Some ( ast:: FieldList :: RecordFieldList ( field_list) ) => {
345
361
let mut expr = None ;
346
362
for field in field_list. fields ( ) {
@@ -357,7 +373,6 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
357
373
make:: block_expr ( None , expr) . indent ( ast:: edit:: IndentLevel ( 1 ) )
358
374
}
359
375
360
- // => self.<field_index>.hash(state);
361
376
Some ( ast:: FieldList :: TupleFieldList ( field_list) ) => {
362
377
let mut expr = None ;
363
378
for ( i, _) in field_list. fields ( ) . enumerate ( ) {
0 commit comments