4
struct Vector {
    data: [f32; 2]
}

impl Vector {
   //many methods
}

Now I want to create a Normal which will almost behave exactly like a Vector but I need to differentiate the type. Because for example transforming a normal is different than transforming a vector. You need to transform it with the tranposed(inverse) matrix for example.

Now I could do it like this:

struct Normal {
    v: Vector
}

And then reimplement all the functionality

impl Normal {
    fn dot(self, other: Normal) -> f32 {
        Vector::dot(self.v, other.v)
    }
    ....
}

I think I could also do it with PhantomData

struct VectorType;
struct NormalType;
struct PointType;
struct Vector<T = VectorType> {
    data: [f32; 2],
    _type: PhantomData<T>,
}
type Normal = Vector<NormalType>;

But then I also need a way to implement functionality for specific types.

It should be easy to implement for example add for everything so that it is possible to add point + vector.

Or functionality specific to some type

impl Vector<NormalType> {..} // Normal specific stuff

Not sure how I would implement functionality for a subrange. For example maybe the dot product only makes sense for normals and vectors but not points.

Is it possible to express boolean expression for trait bounds?

trait NormalTrait;
trait VectorTrait;
impl NormalTrait for NormalType {}
impl VectorTrait for VectorType {}

impl<T: PointTrait or VectorTrait> for Vector<T> {
    fn dot(self, other: Vector<T>) -> f32 {..} 
}

What are my alternatives?

1 Answer 1

3

Your question is pretty broad and touches many topics. But your PhantomData idea could be a good solution. You can write different impl blocks for different generic types. I added a few things to your code:

struct VectorType;
struct NormalType;
struct PointType;
struct Vector<T = VectorType> {
    data: [f32; 2],
    _type: PhantomData<T>,
}
type Normal = Vector<NormalType>;
type Point = Vector<PointType>;

// --- above this line is old code --------------------

trait Pointy {}
impl Pointy for VectorType {}
impl Pointy for PointType {}

// implement for all vector types
impl<T> Vector<T> {
    fn new() -> Self {
        Vector {
            data: [0.0, 0.0],
            _type: PhantomData,
        }
    }
}

// impl for 'pointy' vector types
impl<T: Pointy> Vector<T> {
    fn add<R>(&mut self, other: Vector<R>) {}
    fn transform(&mut self) { /* standard matrix multiplication */ }
}

// impl for normals
impl Vector<NormalType> {
    fn transform(&mut self) { /* tranposed inversed matrix stuff */ }
}

fn main() {
    let mut n = Normal::new();
    let mut p = Point::new();
    n.transform();
    p.transform();

    // n.add(Normal::new());  // ERROR:
    // no method named `add` found for type `Vector<NormalType>` in the current scope
    p.add(Point::new());
}

Is it possible to express boolean expression for trait bounds?

No (not yet). But you can solve it in this case as shown above: you create a new trait (Pointy) and implement it for the types in your "or"-condition. Then you bound with that trait.

5
  • 1
    There's no need for PhantomData<T>. Just replace it with T.
    – Shepmaster
    Jul 4, 2016 at 13:56
  • (I think). Something like how byteorder does it.
    – Shepmaster
    Jul 4, 2016 at 14:16
  • Is there an RFC for boolean expressions in trait bounds?
    – Maik Klein
    Jul 4, 2016 at 17:00
  • @MaikKlein not exactly about the case in question, but with this RFC my answer would change a lot anyway. Jul 4, 2016 at 17:22
  • @Shepmaster Without using PhantomData we have to construct an object of type T which is a bit problematic... so we had to bound T: Default or anything. I think :P Jul 4, 2016 at 17:27

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.