2

In form of a pseudo example:

trait A {}
trait B {}
trait C {}
struct D<T, X>
where
    if T: A 
        then X: is not B
    else if X: B
        then T: C
{}

I've already found some way to bypass this, but I want a way to do it with language features.

More Explanation

In my project, a small ray tracer, I have a vertex type that has a position and a normal. I have another vertex type with a position, a normal, and a texture coordinate.

I also have some material types which work with specific vertex types.

I want to make a constraint on some of the functions that use vertices and materials together:

fn do_something<V, M>(/* some arguments */) 
    -> /* some returned value */ 
where 
    if V: VertexWithTextureCoordinate 
        then M: MaterialWithTexture 
    else if V: SimpleVertex
        then M: SimpleMaterial,
{
}

This is the simplest condition I face.

1
  • 3
    Can you explain why you want to have such conditions? Usually if you're constraining type parameters it's so that the impls can make use of the traits' methods; with conditionals the implementation can only type check if you use only the overlapping subset. However the recently accepted [Specialisation RFC]( github.com/rust-lang/rfcs/blob/master/text/…) would effectively make at least some conditionals possible. May 25, 2016 at 6:27

1 Answer 1

4

It's not that easily possible. First of all: negative trait bounds (saying not trait Foo) do not exist in Rust yet. There were a few RFCs, but AFAIK there are no specific plans on implementing something like that in near future. However, specialization could enable you to simulate negative trait bounds.

Rust's type system is Turing complete, but as far as I can tell, exactly what you want is not possible without using specialization and negative trait bounds, both of which are not implemented/not stable.

What you ask for is a pretty general case; there are some specific cases where it's possible. If you have this struct D and want to add methods to it which need those trait bounds, you can just write two impl blocks:

impl<T, X> D<T, X>
where
    T: A,
    X: NotB, // assume `NotB` is just another trait
{   
    // ...
}

impl<T, X> D<T, X>
where 
    T: C,
    X: B,
{
    // ...
}

If you think about it, you would have to write two implementations anyway: When T is A, you can use methods of A on objects of T, when T is C, you can use methods of C on objects of T, but you can't just tell the compiler "either A or C".

Solution to your specific problem

In your case, I would create another trait that denotes a combination of vertex and material that works well together. Something like:

trait VertexMaterialPair {
    // ...
}

impl<V, M> VertexMaterialPair for (V, M) 
where
    V: VertexWithTextureCoordinate, 
    M: MaterialWithTexture,
{ /* ... */ }

impl<V, M> VertexMaterialPair for (V, M) 
where
    V: SimpleVertex, 
    M: SimpleMaterial, 
{ /* ... */ }

As you can see, I implemented the trait for a pair (tuple) of a vertex and a material. Thus your function would look like:

fn do_something<V, M>(/* some arguments */) -> /* ... */ 
where
    (V, M): VertexMaterialPair,
{ /* ... */ }

This should work fairly well; however, it might not be the very best solution for your ray tracer...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.