Skip to content

Commit 65ce87c

Browse files
committed
gen PartialEq for basic enums
1 parent f8a64c0 commit 65ce87c

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ enum Foo {
699699
700700
impl PartialEq for Foo {
701701
$0fn eq(&self, other: &Self) -> bool {
702-
std::mem::discriminant(self) == std::mem::discriminant(other)
702+
core::mem::discriminant(self) == core::mem::discriminant(other)
703703
}
704704
}
705705
"#,
@@ -726,7 +726,7 @@ enum Foo {
726726
727727
impl PartialEq for Foo {
728728
$0fn eq(&self, other: &Self) -> bool {
729-
if std::mem::discriminant(self) == std::mem::discriminant(other) {
729+
if core::mem::discriminant(self) == core::mem::discriminant(other) {
730730
match (self, other) {
731731
(Self::Bar(l0), Self::Bar(r0)) => l0 == r0,
732732
_ => true,
@@ -770,7 +770,7 @@ enum Foo {
770770
771771
impl PartialEq for Foo {
772772
$0fn eq(&self, other: &Self) -> bool {
773-
if std::mem::discriminant(self) == std::mem::discriminant(other) {
773+
if core::mem::discriminant(self) == core::mem::discriminant(other) {
774774
match (self, other) {
775775
(Self::Bar { bin: l_bin }, Self::Bar { bin: r_bin }) => l_bin == r_bin,
776776
(Self::Baz { qux: l_qux, fez: l_fez }, Self::Bar { qux: r_qux, fez: r_fez }) => l_qux == r_qux && l_fez == r_fez,

crates/ide_assists/src/utils/gen_trait_fn_body.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,15 @@ fn gen_hash_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
330330

331331
/// Generate a `PartialEq` impl based on the fields and members of the target type.
332332
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+
333342
// FIXME: return `None` if the trait carries a generic type; we can only
334343
// generate this code `Self` for the time being.
335344

@@ -338,9 +347,16 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
338347
ast::Adt::Union(_) => return None,
339348

340349
// 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+
}
342359
ast::Adt::Struct(strukt) => match strukt.field_list() {
343-
// => self.<field>.hash(state);
344360
Some(ast::FieldList::RecordFieldList(field_list)) => {
345361
let mut expr = None;
346362
for field in field_list.fields() {
@@ -357,7 +373,6 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
357373
make::block_expr(None, expr).indent(ast::edit::IndentLevel(1))
358374
}
359375

360-
// => self.<field_index>.hash(state);
361376
Some(ast::FieldList::TupleFieldList(field_list)) => {
362377
let mut expr = None;
363378
for (i, _) in field_list.fields().enumerate() {

0 commit comments

Comments
 (0)