受本地定义 public 特征约束的核心特征的全面实施
Blanket implementation of core traits constrained by locally-defined public trait
我有以下代码:
pub trait GetIdentifier {
//...
}
impl<T: GetIdentifier> Hash for T {
fn hash(&self) -> //....
}
我收到以下错误:
error[E0119]: conflicting implementations of trait `std::hash::Hash` for type `&_`:
--> <anon>:18:1
|
18 | / impl<T: GetIdentifier> Hash for T {
19 | | }
| |_^
|
= note: conflicting implementation in crate `core`
error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g. `MyStruct<T>`); only traits defined in the current crate can be implemented for a type parameter
--> <anon>:18:1
|
18 | / impl<T: GetIdentifier> Hash for T {
19 | | }
| |_^
为什么?我还没有为 &_
实施 GetIdentifier
,所以一揽子 impl
不应该适用于 &_
。我箱子的消费者也无法为核心类型实现 GetIdentifier
,所以这没问题。我在这里错过了什么?为什么 &_
甚至涉及到这里——我没有对我的特征进行 ?Sized
约束,所以甚至不应该考虑引用....对吗?
您 crate 的消费者可能会为 TheirType
实施 GetIdentifier
并同时为 TheirType
.
实施 Hash
现在你可能会说这是他们的问题,但想象一下另一个具有特征 Foo
的板条箱也执行 impl<T: Foo> Hash for T {}
和 TheirType
实现 Foo
和 GetIdentifier
.突然之间,他们无法实现任何一个特征。
&_
发生错误的原因是 stdlib impl 说任何类型 T
实现 Hash
导致 &T
也实现 Hash
。
我有以下代码:
pub trait GetIdentifier {
//...
}
impl<T: GetIdentifier> Hash for T {
fn hash(&self) -> //....
}
我收到以下错误:
error[E0119]: conflicting implementations of trait `std::hash::Hash` for type `&_`:
--> <anon>:18:1
|
18 | / impl<T: GetIdentifier> Hash for T {
19 | | }
| |_^
|
= note: conflicting implementation in crate `core`
error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g. `MyStruct<T>`); only traits defined in the current crate can be implemented for a type parameter
--> <anon>:18:1
|
18 | / impl<T: GetIdentifier> Hash for T {
19 | | }
| |_^
为什么?我还没有为 &_
实施 GetIdentifier
,所以一揽子 impl
不应该适用于 &_
。我箱子的消费者也无法为核心类型实现 GetIdentifier
,所以这没问题。我在这里错过了什么?为什么 &_
甚至涉及到这里——我没有对我的特征进行 ?Sized
约束,所以甚至不应该考虑引用....对吗?
您 crate 的消费者可能会为 TheirType
实施 GetIdentifier
并同时为 TheirType
.
Hash
现在你可能会说这是他们的问题,但想象一下另一个具有特征 Foo
的板条箱也执行 impl<T: Foo> Hash for T {}
和 TheirType
实现 Foo
和 GetIdentifier
.突然之间,他们无法实现任何一个特征。
&_
发生错误的原因是 stdlib impl 说任何类型 T
实现 Hash
导致 &T
也实现 Hash
。