enum data {
val = 2
}
enum v {
v1 = 2
}
impl PartialEq for data {
fn eq(&self,other:&data) -> bool {
self == other
}
}
fn main() {
let mut x = data::val;
let mut y = v::v1;
if x == y {
println!("equal");
}
// println!("{}",x.val);
}
Compiling playground v0.0.1 (/playground)
warning: type `data` should have an upper camel case name
--> src/main.rs:2:6
|
2 | enum data {
| ^^^^ help: convert the identifier to upper camel case: `Data`
|
= note: `#[warn(non_camel_case_types)]` on by default
warning: variant `val` should have an upper camel case name
--> src/main.rs:3:5
|
3 | val = 2
| ^^^ help: convert the identifier to upper camel case (notice the capitalization): `Val`
warning: type `v` should have an upper camel case name
--> src/main.rs:6:6
|
6 | enum v {
| ^ help: convert the identifier to upper camel case (notice the capitalization): `V`
warning: variant `v1` should have an upper camel case name
--> src/main.rs:7:5
|
7 | v1 = 2
| ^^ help: convert the identifier to upper camel case (notice the capitalization): `V1`
error[E0308]: mismatched types
--> src/main.rs:21:13
|
21 | if x == y {
| ^ expected enum `data`, found enum `v`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.
Your PartialEq allows you to compare data with data. It doesn't allow comparison of data with v. Additionally you are using == in your implementation, which will cause an infinite loop, since == just uses the PartialEq implementation.
Here's how you can allow comparison between the different kinds of enums:
impl PartialEq<V> for Data {
fn eq(&self, other: &V) -> bool {
*self as u32 == *other as u32
}
}
impl PartialEq<Data> for V {
fn eq(&self, other: &Data) -> bool {
*self as u32 == *other as u32
}
}
Yes, it turns a &Data into a Data, which is required before we can cast the enum to an integer. Note that it depends on the #[derive(Copy)], and without it, you could not do that dereference.
enum Data {
val = 2
}
enum V {
v1 = 2
}
impl PartialEq<V> for Data {
fn eq(&self, other: &V) -> bool {
*self as u32 == *other as u32
}
}
impl PartialEq<Data> for V {
fn eq(&self, other: &Data) -> bool {
*self as u32 == *other as u32
}
}
fn main() {
let mut x = Data::val;
let mut y = V::v1;
if x == y {
println!("equal");
}
// println!("{}",x.val);
}
error[E0507]: cannot move out of `*self` which is behind a shared reference
--> src/main.rs:11:9
|
11 | *self as u32 == *other as u32
| ^^^^^ move occurs because `*self` has type `Data`, which does not implement the `Copy` trait
error[E0507]: cannot move out of `*other` which is behind a shared reference
--> src/main.rs:11:25
|
11 | *self as u32 == *other as u32
| ^^^^^^ move occurs because `*other` has type `V`, which does not implement the `Copy` trait
error[E0507]: cannot move out of `*self` which is behind a shared reference
--> src/main.rs:17:9
|
17 | *self as u32 == *other as u32
| ^^^^^ move occurs because `*self` has type `V`, which does not implement the `Copy` trait
error[E0507]: cannot move out of `*other` which is behind a shared reference
--> src/main.rs:17:25
|
17 | *self as u32 == *other as u32
| ^^^^^^ move occurs because `*other` has type `Data`, which does not implement the `Copy` trait
If you click my link, you will see that I added #[derive(Copy, Clone)] to the enums. As I mentioned in the previous comment, this is necessary, as you otherwise can't copy them with a dereference.