1

I am trying to implement a kind of HashSet that can store which hashes have been encountered (without associating a value).

I'm having trouble with this:

  • I want the structure to be generic over types that implement Div, Rem and Eq (int-like).
  • For it to work, I need the output of Div and Rem to also implement Div, Rem and Eq (I think). I'm willing to disqualify types that don't meet this criteria.
  • The types also need to interact with an integer constant that I can't get to be of a dynamic type.

I've tried to reduce it to a minimal example (playground):

use std::ops::{ Rem, Div };

const X: i32 = 16;

pub trait Int: Rem + Div + Eq + Sized {}

impl Int for i32 {}
impl Int for usize {}
// also other int types

pub fn stuff<T: Int>(arg: T) -> T {
    (arg / X) % X
}

(I also have a more elaborate playground to show what I tried).

But while I understand the errors I get, I don't know how to resolve them:

error[E0308]: mismatched types
  --> main.rs:13:12
   |
13 |     (arg / BINS) % BINS
   |            ^^^^ expected type parameter, found i32
   |
   = note: expected type `T`
              found type `i32`

error[E0369]: binary operation `%` cannot be applied to type `<T as std::ops::Div>::Output`
  --> main.rs:13:5
   |
13 |     (arg / BINS) % BINS
   |     ^^^^^^^^^^^^^^^^^^^
   |
   = note: an implementation of `std::ops::Rem` might be missing for `<T as std::ops::Div>::Output`

Nightly features are fine.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Mark
  • 18,730
  • 7
  • 107
  • 130

0 Answers0