Traits and Related Types

I am trying to implement something in Rust with attributes and related types. I'm not sure how to form my question with words, so I will add a piece of code that I hope will illustrate what I'm trying to do.

pub trait Person {}

pub trait Directory<P: Person> {
    type Per = P;
    fn get_person(&self) -> Self::Per;
}

pub trait Catalog {
    type Per : Person;
    type Dir : Directory<Self::Per>;

    fn get_directory(&self) -> Self::Dir;
}

fn do_something<C>(catalog: C) where C: Catalog {
    let directory : C::Dir = catalog.get_directory();

    // let person : C::Per = directory.get_person();
    // The code above fails with:
    //  error: mismatched types:
    //   expected `<C as Catalog>::Per`,
    //      found `<<C as Catalog>::Dir as Directory<<C as Catalog>::Per>>::Per`
    //   (expected trait `Catalog`,
    //       found trait `Directory`) [E0308]

    let person = directory.get_person();
    do_something_with_person(person);
}

fn do_something_with_person<P: Person>(p: P) {}

I would expect the code above to compile, but that is not the case.

Instead, I get:

error: the trait `Person` is not implemented for the type `<<C as Catalog>::Dir as Directory<<C as Catalog>::Per>>::Per` [E0277]

Which, AFAICT, means that the compiler cannot determine that a person variable has the Person trait.

I am using the following version of rustc:

rustc 1.2.0-dev (a19ed8ad1 2015-06-18)

Did I miss something?

+4
source share
2 answers

There is a fix here:

pub trait Directory<P: Person> {
    type Per : Person = P;
    fn get_person(&self) -> Self::Per;
}

Per Directory . , Self::Per ( Per ) Person, Person.

+5

:

pub trait Person {}

pub trait Directory {
    type Person: Person;
    fn get_person(&self) -> Self::Person;
}

pub trait Catalog {
    type Dir: Directory;

    fn get_directory(&self) -> Self::Dir;
}

fn do_something<C: Catalog>(catalog: C) {
    let directory = catalog.get_directory();
    let person = directory.get_person();
    do_something_with_person(person);
}

fn do_something_with_person<P: Person>(p: P) {}

<P: Person> generics. .

; Per, Person. (Directory::Person, C::Person, & c.), . (Dir Directory, , . Id, , Directory.)

Person, Catalog, ; Self::Dir::Person .

+4

All Articles