gpt4 book ai didi

rust - Impl Into 所有类型的 impl Into 特性

转载 作者:行者123 更新时间:2023-12-05 04:47:27 25 4
gpt4 key购买 nike

我想实现一个转换特性,涵盖支持现有转换的所有类型。我认为这可以通过以下方式完成:

impl<T> Into<B> for T
where
T: Into<A>,
{
fn into(self) -> B {
let a: A = self.into();
B::CaseA(a)
}
}

但是编译器会抛出以下错误:

error[E0119]: conflicting implementations of trait `std::convert::Into<block::ItemContent>`
--> yrs\src\block.rs:945:1
|
945 | / impl<T> Into<B> for T
946 | | where
947 | | T: Into<A>,
948 | | {
... |
952 | | }
953 | | }
| |_^
|
= note: conflicting implementation in crate `core`:
- impl<T, U> Into<U> for T
where U: From<T>;

error[E0210]: type parameter `T` must be covered by another type when it appears before the first local type (`B`)
--> yrs\src\block.rs:945:6
|
945 | impl<T> Into<B> for T
| ^ type parameter `T` must be covered by another type when it appears before the first local type (`B`)
|
= note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local, and no uncovered type parameters appear before that first local type
= note: in this case, 'before' refers to the following order: `impl<..> ForeignTrait<T1, ..., Tn> for T0`, where `T0` is the first and `Tn` is the last

这有可能实现吗?我正在寻找一种方法来为所有实例自动扩展新类型的转换,支持转换为另一种现有类型。

最佳答案

关于rust - Impl Into<B> 所有类型的 impl Into<A> 特性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68464252/

25 4 0
文章推荐: javascript - Node ssh2 : TypeError: is not a constructor