How to add restrictions on generics

I am trying to implement a OneToMany relationship in Rust. This is implemented as a pair of HashMap

s.

I am having trouble figuring out how I constrain the generic types of OneToMany structure. It seems like O and M should implement traits core::cmp::Eq

and collections::hash::Hash

. I could not find the required syntax in the docs.

struct OneToMany<O,M> { 
  manyByOne: HashMap<O,Vec<M>>,
  oneByMany: HashMap<M,O>
}

impl<O,M> OneToMany<O,M> {
  fn new(one: O, many: M) -> OneToMany<O,M> {
    OneToMany {
      manyByOne: HashMap::new(),
      oneByMany: HashMap::new(),
    }
  }
}

      

Compiler errors:

$ cargo build
   Compiling chat v0.1.0 (file:///home/chris/rust/chat)
src/chat.rs:45:18: 45:30 error: the trait `core::cmp::Eq` is not implemented for the type `O`
src/chat.rs:45       manyByOne: HashMap::new(),
                                ^~~~~~~~~~~~
src/chat.rs:45:18: 45:30 note: the trait `core::cmp::Eq` must be implemented because it is required by `std::collections::hashmap::map::HashMap<K, V, RandomSipHasher>::new`
src/chat.rs:45       manyByOne: HashMap::new(),
                                ^~~~~~~~~~~~
src/chat.rs:45:18: 45:30 error: the trait `collections::hash::Hash` is not implemented for the type `O`
src/chat.rs:45       manyByOne: HashMap::new(),
                                ^~~~~~~~~~~~
src/chat.rs:45:18: 45:30 note: the trait `collections::hash::Hash` must be implemented because it is required by `std::collections::hashmap::map::HashMap<K, V, RandomSipHasher>::new`
src/chat.rs:45       manyByOne: HashMap::new(),
                                ^~~~~~~~~~~~
src/chat.rs:46:18: 46:30 error: the trait `core::cmp::Eq` is not implemented for the type `M`
src/chat.rs:46       oneByMany: HashMap::new(),
                                ^~~~~~~~~~~~
src/chat.rs:46:18: 46:30 note: the trait `core::cmp::Eq` must be implemented because it is required by `std::collections::hashmap::map::HashMap<K, V, RandomSipHasher>::new`
src/chat.rs:46       oneByMany: HashMap::new(),
                                ^~~~~~~~~~~~
src/chat.rs:46:18: 46:30 error: the trait `collections::hash::Hash` is not implemented for the type `M`
src/chat.rs:46       oneByMany: HashMap::new(),
                                ^~~~~~~~~~~~
src/chat.rs:46:18: 46:30 note: the trait `collections::hash::Hash` must be implemented because it is required by `std::collections::hashmap::map::HashMap<K, V, RandomSipHasher>::new`
src/chat.rs:46       oneByMany: HashMap::new(),
                                ^~~~~~~~~~~~
error: aborting due to 4 previous errors
Could not compile `chat`.

      

Where can you add constraints on O and M?

+3


source to share


1 answer


You can add constraints to the impl:

impl<O: Eq + Hash, M: Eq + Hash> OneToMany<O,M>

or with the new "where" syntax



impl<O, M> OneToMany<O,M> where O: Eq + Hash, M: Eq + Hash

You can refer to the Properties Guide for a more detailed description of the restrictions.

+3


source







All Articles