0

I am looking for ways to create a Vec of structs that have fields implementing a specific trait, more specifically the following struct:

struct Animation<T: Add> {
    channel: TypeId,// channel/component to animate
    path: String, // string
    from: T, // T is a type that implements the ADD trait ie : can be either float or vec2, vec4, vec4 etc
    to: T, // T is a type that implements the ADD trait ie : can be either float or vec2, vec4, vec4 etc , same as from
    duration: u64,
    easing: String,
    repeat: i16
}

I am still new to Rust so sorry if this is obvious

  • How would you use Box<Animation> for dynamic dispatch in this case?
  • defining an enum that has a variant for each type (f32, vec3) works but seems very verbose.
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Mark Moissette
  • 169
  • 2
  • 13
  • 3
    I don't think you can really do it. You will need the actual types and `Animation` type is different from `Animation` for example. Maybe what you want is to actually have an `Animated` trait, and then you could have a `Vec>`? – Netwave Mar 29 '21 at 11:24
  • Netwave is correct: fields are not something you can abstract over. Instead, create a new trait. – Shepmaster Mar 29 '21 at 13:01
  • Ok thanks a lot ! I split the struct up into homogeneous ones, works fine now. – Mark Moissette Apr 06 '21 at 08:09

0 Answers0