2017-05-03 38 views
0

約束我有以下代碼:核心特質毯實現由本地定義的公共特質

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,所以的總括不應該適用於&_。我的箱子的消費者無法執行核心類型GetIdentifier,所以在那裏沒有問題。我在這裏錯過了什麼?爲什麼&_甚至在這裏涉及 - 我沒有把?Sized綁定在我的特質上,所以不應該考慮引用....對嗎?

回答

1

您的箱子的消費者可能執行GetIdentifierTheirType並同時執行HashTheirType

現在你可能會說那是他們的問題,但是想象一下,與性狀Foo也確實impl<T: Foo> Hash for T {}另一個箱子,和TheirType實施FooGetIdentifier。突然他們無法實現任何特質。

&_發生錯誤的原因是STDLIB IMPL說,任何類型T實施Hash原因&T,也能實現Hash

+0

根據我的理解,OP指出'GetIdentifier'是一個本地特徵,因此不能從外部實現。 –

+1

我認爲這個特質是公開的,因爲否則就不需要區分核心類型和自定義類型 –

+0

@ E_net4我的意思是本地定義的公共特質 - 我將更新問題 – user1935361