【发布时间】:2020-05-06 23:08:56
【问题描述】:
我有一个广泛的基本特征。有些类型只关心其功能的子集,所以我添加了一个子特征,要求用户实现更小的功能集。
此代码失败:
trait Base<T> {
fn foo(arg: bool);
}
// Ext is a narrowing of Base requiring user to provide alternative, simpler interface for the same functionality
trait Ext<T>: Base<T> {
fn bar();
}
// implement Base<T> for all types implementing Ext<T>
impl<T, E> Base<T> for E
where
E: Ext<T>,
{
fn foo(arg: bool) {
Self::bar();
}
}
struct Data<T>;
// error[E0119]: conflicting implementations of trait `Base<_>` for type `Data<_>`:
impl<T> Base<T> for Data<T> {
fn foo(arg: bool) {}
}
出现以下错误:
error[E0119]: conflicting implementations of trait `Base<_>` for type `Data<_>`:
--> src/lib.rs:22:1
|
11 | / impl<T, E> Base<T> for E
12 | | where
13 | | E: Ext<T>,
14 | | {
... |
17 | | }
18 | | }
| |_- first implementation here
...
22 | impl<T> Base<T> for Data<T> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Data<_>`
|
= note: downstream crates may implement trait `Ext<_>` for type `Data<_>`
有趣的是,当我删除T 的一般性时,它会起作用:
trait Base {
fn foo(arg: bool);
}
// Ext is a narrowing of Base requiring user to provide alternative, simpler interface for the same functionality
trait Ext: Base {
fn bar();
}
// implement Base for all types implementing Ext
impl<E> Base for E
where
E: Ext,
{
fn foo(arg: bool) {
Self::bar();
}
}
struct Data;
// works just fine
impl Base for Data {
fn foo(arg: bool) {}
}
stackoverflow 上的其他一些帖子提到了类似的问题,但它们通常存在外来特征(来自标准库的特征)的问题。就我而言,特征和类型都是本地的,因此据我所知,孤儿规则不应该生效。
基本上,错误提到了downstream crates may implement trait 'Ext<_>' for type 'Data<_>',这不是真的,因为Ext 和Data 对于这些板条箱来说都是陌生的。
总而言之,我的问题是:
- 为什么我的一揽子 impl 被拒绝,即使其他 crate 似乎不可能产生冲突。
- 为什么没有
T的版本不会被拒绝,即使它几乎是相同的一揽子实现? - 是否有解决此问题的方法?
【问题讨论】:
-
看来你的问题可能会被“conflicting implementations for trait” when trying to be generic的答案回答; Conflicting implementations of trait in Rust; Resolving trait implementation conflicts; .... 如果不是,请edit 您的问题解释差异。否则,我们可以将此问题标记为已回答。
-
读起来很有趣。孤儿规则是一个相当深的兔子洞。更新了我的问题。