为多个泛型类型实现板条箱本地特征会导致实现冲突

时间:2020-08-17 06:26:19

标签: rust template-specialization specialization

以下代码会导致实现错误:

trait A<S> {}
trait B<S> {}
struct C<T>(T);

impl <T, S> B<S> for T
where T: A<S>
{}

impl<T, S> B<S> for C<T>
where T: B<S>
{}
   Compiling playground v0.0.1 (/playground)
error[E0119]: conflicting implementations of trait `B<_>` for type `C<_>`:
  --> src/lib.rs:11:1
   |
7  | / impl <T, S> B<S> for T
8  | | where T: A<S>
9  | | {}
   | |__- first implementation here
10 | 
11 | / impl<T, S> B<S> for C<T>
12 | | where T: B<S>
13 | | {}
   | |__^ conflicting implementation for `C<_>`
   |
   = note: downstream crates may implement trait `A<_>` for type `C<_>`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0119`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

C是本地类型,专业化可以解决此问题吗?

此外,如果我们将A<S>替换为Fn(S),它是否有效,是编译器漏洞吗?

trait B<S> {}

struct C<T>(T);

impl <T, S> B<S> for T
where T: Fn(S)
{}

impl<T, S> B<S> for C<T>
where T: B<S>
{}

0 个答案:

没有答案