简体   繁体   English

如果两个特征都是为引用实现的,那么如何为实现特征A的所有类型实现特征B?

[英]How can I implement trait B for all types that implement trait A if both traits are implemented for references?

We have two traits A and B for which 我们有两个特征AB

  1. I can trivially implement B for all types that implement A . 我可以为实现A所有类型简单地实现B
  2. I can trivially implement A for references to any type implementing A . 我可以简单地实现A来引用任何实现A类型。
  3. I can trivially implement B for references to any type implementing B . 我可以简单地实现B以引用任何实现B类型。

Actually doing all three leads to a conflict, because now references to types that implement A would have two implementations of B for them. 实际上,所有三个都会导致冲突,因为现在对实现A类型的引用将为它们提供两个B实现。 One due to the impl<T: A> A for &T and one transitively due to the impl<T: A> B for T (and then the impl<T: B> B for &T . 一个是由于impl<T: A> A for &T而一个是由于impl<T: A> B for T (然后是impl<T: B> B for &T

I can't remove the impl<T: B> B for &T , because there might be types that implement B but not A 我无法删除impl<T: B> B for &Timpl<T: B> B for &T ,因为可能存在实现B而不是A类型

Here's an example code exhibiting the behaviour. 这是展示行为的示例代码。

trait A {}
trait B {}

impl<'a, T: A> A for &'a T {}
impl<T: A> B for T {}

impl<'a, T: B> B for &'a T {}

which results in the following error: 这会导致以下错误:

error[E0119]: conflicting implementations of trait `B` for type `&_`:
  |
  | impl<T: A> B for T {}
  | --------------------- first implementation here
  | impl<'a, T: B> B for &'a T {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `&_`

Is it possible to use the Rust typesystem in a way to ensure that when there is an impl B for &A , we don't create one for &B ? 是否有可能以某种方式使用Rust类型系统以确保当有impl B for &Aimpl B for &A ,我们不为&B创建一个?

There has been some discussion on the Rust internals forum on this topic, starting with a blog post by Nicholas Matsakis about how to handle the issue of overlapping trait implementations. 关于这个主题的Rust内部论坛已经有一些讨论 ,首先是Nicholas Matsakis关于如何处理重叠特征实现问题的博客文章

Today, (unstable) Rust has some impl specialisation , but that only works for strictly more specific impls of a more generic one. 今天,(不稳定)Rust有一些impl专门化 ,但这只适用于更通用的更严格的更具体的impl。

So I think the answer is that there isn't a good way to do it today, but at some point in the future there's a good chance that Rust will evolve to allow expressing overlapping trait impls. 所以我认为答案是今天没有一个好方法可以做到这一点,但是在将来的某个时候,Rust很有可能会发展为允许表达重叠的特质动作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为实现该特征的现有类型的枚举实现范围内的特征? - How can I implement a trait in scope for an enum of existing types for which the trait is implemented? 如何为实现特征的所有类型实现From特质,但对某些类型使用特定的实现? - How can I implement the From trait for all types implementing a trait but use a specific implementation for certain types? 无法为实现我拥有的特征的所有类型实现我不拥有的特征 - Can't implement a trait I don't own for all types that implement a trait I do own 为实现 trait 的所有类型实现 trait - Implement a trait for all types implementing a trait 如何为实现特定特征的所有类型批量实现反序列化? - How can I mass implement Deserialize for all types that implement a specific trait? 如何获得在Rust中实现特定特征的类型列表? - How can I get a list of types that implement a particular trait in Rust? 如何使用单个宏而不是使用 generics 为所有数字类型(包括引用)实现特征? - How to implement a trait for all numeric types (including references) with a single macro instead of using generics? 如何在 Rust 的内部特征上实现外部特征? - how can I implement an external trait on an internal trait in Rust? 如何为特征实现“默认迭代器”? - How can I implement a “default iterator” for a trait? 什么时候我不应该实现引用该特征的实现者的特征? - When should I not implement a trait for references to implementors of that trait?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM