These are chat archives for rust-lang/rust

30th
Aug 2018
tandrysyawaludin
@tandrysyawaludin
Aug 30 2018 04:25
Anyone use Rocket Framework? How to debug in Rocket seems like Rails in Ruby?
Vegar Ringdal
@vegarringdal
Aug 30 2018 11:38
Is the videos for rust conf2018 somewhere ?
Derk Bell
@derkbell
Aug 30 2018 16:14

I'm trying to create a vector of structs that implement a trait that has an associated type that must also implement a trait but I'm getting a strange error. Simplified I'm trying this:

trait Trait1 {}
trait Trait2 { type Output: Trait1; }

struct S1 {}
impl Trait1 for S1 {}
struct S2 {}
impl Trait1 for S2 {}
struct B1 {}
impl Trait2 for B1 { type Output = S1; }
struct B2 {}
impl Trait2 for B2 { type Output = S2; }

struct Container<T: Trait1> {
  items: Vec<Box<Trait2<Output=T>>>
}

fn main() {
  let mut c = Container { items: Vec::new() };
  c.items.push(Box::new(B1 {}));
  c.items.push(Box::new(B2 {}));
}

I'm getting an error: expected struct S2, found struct S1

I thought that by specifying the trait associated type as T, implementing Trait1, the Vec should accept any struct implementing Trait1, but it seems to get locked to the specific struct S1 when inserting B1. Did I do something wrong or is Vec unable to handle this?

Guillaume P.
@TeXitoi
Aug 30 2018 16:30
@derkbell your T must be instantiated with a type, and that type can't be S1 or S2, that's 2 different types
Denis Lisov
@tanriol
Aug 30 2018 20:46
@derkbell You may want to have different trait bounds that wraps the Output type with a trait object.
Michael Jansen
@mjjansen
Aug 30 2018 21:02
@tandrysyawaludin I use rocket. you may want to ask here: https://riot.im/app/#/room/#mozilla_#rocket:matrix.org