Restrict Trait type to equal other type

Hi,

I have a trait "NumberSystem" typically representing SIMD vectors of different lengths and types. It has a type "Scalar" that represents the underlying number, i.e.

trait NumberSystem: LotsOfStuff {
  type S: Scalar;
  ...
}

Now i also have a struct (interpolation grid) which only is distinct based on the Scalar type:

struct Grid<S: Scalar> {
  data: Vec<S>
}

However, i want to have a method on "Grid" that allows me to be called with a value of any "NumberSystem" that has a scalar that matches the scalar of the Grid, i.e. something like:

impl<S> Grid<S> where S: Scalar {
   pub fn lookup<N>(x: N) -> N 
    where N: NumberSystem,
       N::S === S // THIS IS THE PROBLEM
{...}
}

Now, I struggle to express that requirement in the lookup function, that N::S of the generic on the function should equal the concrete S in the Grid struct.

Is this even possible to express?

Thanks in advance

If I understood you correctly, where N: NumberSystem<S = S> should work as a bound, no?

5 Likes

You would not believe how many variants I have tried but never thought of something that obvious....

Thanks!

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.