我试图在Rust上使用特征和关联类型实现某些功能。我不知道如何用文字形成我的问题,所以我会添加一段代码片段,希望能够说明我正在尝试做什么。性状和关联类型
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) {}
我期望上面的代码可以编译,但它不会。
相反,我得到:
error: the trait `Person` is not implemented for the type `<<C as Catalog>::Dir as Directory<<C as Catalog>::Per>>::Per` [E0277]
其中,AFAICT,意味着编译器不能确定该人变量的人物特质。
我使用以下rustc版本:
rustc 1.2.0-dev (a19ed8ad1 2015-06-18)
我缺少的东西?