Generics 类型不匹配:找到了预期的关联类型结构
我有以下示例代码:Generics 类型不匹配:找到了预期的关联类型结构,generics,rust,associated-types,Generics,Rust,Associated Types,我有以下示例代码: trait Manager: Sized { type Item: Item<Manager=Self>; } trait Item: Sized { type Manager: Manager<Item=Self> = DefaultManager<Self>; } struct DefaultManager<T: Item<Manager=DefaultManager<T>>>(:
trait Manager: Sized {
type Item: Item<Manager=Self>;
}
trait Item: Sized {
type Manager: Manager<Item=Self> = DefaultManager<Self>;
}
struct DefaultManager<T: Item<Manager=DefaultManager<T>>>(::std::marker::PhantomData<T>);
impl<T: Item<Manager=DefaultManager<T>>> Manager for DefaultManager<T> {
type Item = T;
}
到
在这种情况下,DefaultManager
不应是有效的类型,因为::Manager
是FooManager
,而不是DefaultManager
导致类型不匹配的原因是什么?我如何向编译器澄清是否满足类型约束
编辑:为了澄清,我使用了不稳定的
关联的\u type_defaults
功能,这样我就可以为Item::Manager
提供默认值,所以这个示例目前只建立在夜间的基础上。@Shepmaster很好,我添加了一个澄清。
<anon>:9:5: 9:61 warning: type mismatch resolving `<Self as Item>::Manager == DefaultManager<Self>`: expected associated type, found struct `DefaultManager` [E0271]
<anon>:9 type Manager: Manager<Item=Self> = DefaultManager<Self>;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:9:5: 9:61 help: see the detailed explanation for E0271
<anon>:9:5: 9:61 note: this warning results from recent bug fixes and clarifications; it will become a HARD ERROR in the next release. See RFC 1214 for details.
<anon>:9 type Manager: Manager<Item=Self> = DefaultManager<Self>;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:9:5: 9:61 note: required by `DefaultManager`
struct DefaultManager<T: Item<Manager=DefaultManager<T>>>(...)
struct DefaultMananger<T: Item>
struct Foo;
impl Item for Foo { type Manager = FooManager; }
struct FooManager;
impl Manager for FooManager { type Item = Foo; }
type DefaultFooManager = DefaultManager<Foo>;