@@ -5,7 +5,7 @@ use syntax::{
5
5
ted,
6
6
} ;
7
7
8
- /// Generate custom trait bodies where possible.
8
+ /// Generate custom trait bodies without default implementation where possible.
9
9
///
10
10
/// Returns `Option` so that we can use `?` rather than `if let Some`. Returning
11
11
/// `None` means that generating a custom trait body failed, and the body will remain
@@ -28,6 +28,7 @@ pub(crate) fn gen_trait_fn_body(
28
28
29
29
/// Generate a `Clone` impl based on the fields and members of the target type.
30
30
fn gen_clone_impl ( adt : & ast:: Adt , func : & ast:: Fn ) -> Option < ( ) > {
31
+ debug_assert ! ( func. name( ) . map_or( false , |name| name. text( ) == "clone" ) ) ;
31
32
fn gen_clone_call ( target : ast:: Expr ) -> ast:: Expr {
32
33
let method = make:: name_ref ( "clone" ) ;
33
34
make:: expr_method_call ( target, method, make:: arg_list ( None ) )
@@ -339,6 +340,7 @@ fn gen_default_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
339
340
340
341
/// Generate a `Hash` impl based on the fields and members of the target type.
341
342
fn gen_hash_impl ( adt : & ast:: Adt , func : & ast:: Fn ) -> Option < ( ) > {
343
+ debug_assert ! ( func. name( ) . map_or( false , |name| name. text( ) == "hash" ) ) ;
342
344
fn gen_hash_call ( target : ast:: Expr ) -> ast:: Stmt {
343
345
let method = make:: name_ref ( "hash" ) ;
344
346
let arg = make:: expr_path ( make:: ext:: ident_path ( "state" ) ) ;
@@ -394,9 +396,7 @@ fn gen_hash_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
394
396
395
397
/// Generate a `PartialEq` impl based on the fields and members of the target type.
396
398
fn gen_partial_eq ( adt : & ast:: Adt , func : & ast:: Fn ) -> Option < ( ) > {
397
- if func. name ( ) . map_or ( false , |name| name. text ( ) == "ne" ) {
398
- return None ;
399
- }
399
+ debug_assert ! ( func. name( ) . map_or( false , |name| name. text( ) == "eq" ) ) ;
400
400
fn gen_eq_chain ( expr : Option < ast:: Expr > , cmp : ast:: Expr ) -> Option < ast:: Expr > {
401
401
match expr {
402
402
Some ( expr) => Some ( make:: expr_bin_op ( expr, BinaryOp :: LogicOp ( LogicOp :: And ) , cmp) ) ,
@@ -573,6 +573,7 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
573
573
}
574
574
575
575
fn gen_partial_ord ( adt : & ast:: Adt , func : & ast:: Fn ) -> Option < ( ) > {
576
+ debug_assert ! ( func. name( ) . map_or( false , |name| name. text( ) == "partial_cmp" ) ) ;
576
577
fn gen_partial_eq_match ( match_target : ast:: Expr ) -> Option < ast:: Stmt > {
577
578
let mut arms = vec ! [ ] ;
578
579
@@ -643,7 +644,7 @@ fn gen_partial_ord(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
643
644
make:: block_expr ( stmts. into_iter ( ) , tail) . indent ( ast:: edit:: IndentLevel ( 1 ) )
644
645
}
645
646
646
- // No fields in the body means there's nothing to hash .
647
+ // No fields in the body means there's nothing to compare .
647
648
None => {
648
649
let expr = make:: expr_literal ( "true" ) . into ( ) ;
649
650
make:: block_expr ( None , Some ( expr) ) . indent ( ast:: edit:: IndentLevel ( 1 ) )
0 commit comments