0

I have a struct, and ultimately I'd like to be able to access the fields of this struct through a hash for the purposes of comparing individual fields equality with other instances of the same setup.

The documentation for the Eq trait seems to say that Eq is implemented for the primitive types (u8, i16, etc.). My goal is to store references to Eq in a map and then do equality comparison through them.

struct User {
    id: u32,
    age: u8,
    fingers: u8,
    toes: u8,
}

let u1 = User { id: 1234, age: 42, fingers: 10, toes: 10 };
let u2 = User { id: 5678, age: 41, fingers: 10, toes: 10 };

let mut umap1: HashMap<String, &dyn Eq> = HashMap::new();
//                             ^^^^^^^ - Wrong... but I don't know if this is even possible.

What I'd like to do is insert into the hash something like:

umap1["id"] = &u1.id
umap1["age"] = &u1.age

I could then have a second hash (umap2) and do something like this:

if umap1["id"] == umap2["id"]

Possible?

I believe How to test for equality between trait objects? is not really the same thing because it testing for equality between enums. I'd like to be able to take an Eq reference to a primitive type (like a u16, etc.) and compare it to another.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
dicroce
  • 45,396
  • 28
  • 101
  • 140
  • It looks like your question might be answered by the answers of [How to test for equality between trait objects?](https://stackoverflow.com/q/25339603/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Sep 30 '19 at 19:55
  • Why do you believe that enums are materially different from a "primitive"? What error did you get when when you tried the solution before posting your update? – Shepmaster Sep 30 '19 at 20:12
  • Well, for one the method linked to requires deriving a trait for a new type (the enum)... I don't think I should have to change the types of all of the members of my struct. – dicroce Sep 30 '19 at 20:17
  • In that case, what do you think about [Is it possible to have a heterogeneous vector of types that implement Eq?](https://stackoverflow.com/q/40411045/155423)? Your hash can stand in for the vector in the question. The OP there even said *I would like to have a HashMap with heterogeneous keys*. – Shepmaster Oct 01 '19 at 01:59
  • 1
    `String` also implements `Eq`; does that mean you can compare a `String` to a `u16`? – trent Oct 01 '19 at 11:27
  • Well, I don't want to do that... I am only interested in Equality comparing objects of the same type... Since it looks like Eq is implemented for all the primitive types I had hoped this meant that I could store references to Eq in a map. – dicroce Oct 01 '19 at 17:22
  • 1
    So... what do you expect to happen if you *do* write something like `umap1["id"] == umap2["age"]`? Panic? Or what `impl Eq` block do you think would apply there? – trent Oct 01 '19 at 17:27
  • 1
    `Eq` doesn't actually let you do anything, for the record -- `PartialEq` is the trait that provides the `==` operator. `Eq` just requires `PartialEq`. – trent Oct 01 '19 at 17:36

0 Answers0