gpt4 book ai didi

rust - 与单一实现冲突的实现错误

转载 作者:行者123 更新时间:2023-12-03 11:41:49 25 4
gpt4 key购买 nike

在下面的程序中,PartialOrdPartialEq为所有具有特征 Area 的类型实现。这样,在定义Rectangle时,我只需要实现Area<运算符(operator)正在工作。

trait Area {
fn get_area(&self) -> i32;
}

impl<T: Area> PartialOrd for T {
fn partial_cmp(&self, other: &T) -> Option<std::cmp::Ordering> {
self.get_area().partial_cmp(&other.get_area())
}
}

impl<T: Area> PartialEq for T {
fn eq(&self, other: &T) -> bool {
self.get_area() == other.get_area()
}
}

struct Rectangle {
width: i32,
height: i32
}

impl Area for Rectangle {
fn get_area(&self) -> i32 {
self.width * self.height
}
}


fn main() {
let r1 = Rectangle { width:10, height:10 };
let r2 = Rectangle { width:11, height:9 };
if r1 > r2 {
println!("r1 is bigger.")
} else {
println!("r2 is bigger.")
}
}

但是,我遇到以下错误:

error[E0119]: conflicting implementations of trait `std::cmp::PartialOrd<&_>` for type `&_`:
--> src/main.rs:23:1
|
17 | impl<T: Area> PartialOrd<T> for T {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: conflicting implementation in crate `core`:
- impl<A, B> std::cmp::PartialEq<&B> for &A
where A: std::cmp::PartialEq<B>, A: ?Sized, B: ?Sized;
= note: downstream crates may implement trait `Area` for type `&_`

E0119 的文档给出了一个示例,其中作者编写了两个重叠的实现。但这种情况只有一种实现。甚至错误消息的格式也很奇怪,因为它开始枚举只有一个条目的实现(带有破折号)。这是什么意思?

我还收到另一个错误:

error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
--> src/main.rs:23:6
|
23 | impl<T: Area> PartialEq<T> for T {
| ^ type parameter `T` must be used as the type parameter for some local type
|
= note: only traits defined in the current crate can be implemented for a type parameter

E0210 的文档再次给出了本地类型未覆盖类型参数的示例,但在这种情况下 T 仅限于所有区域。这是什么意思?

最佳答案

正如编译器错误消息中指出的那样,在这种情况下确实有不止一种实现:

impl<A, B> std::cmp::PartialEq<&B> for &A
where A: std::cmp::PartialEq<B>, A: ?Sized, B: ?Sized;

您的 impl 绑定(bind)到 Area。但核心库的实现更加通用,可以覆盖任何 &A,包括那些 &A: Area。基本上,您不应该为您的 crate 实现外来的特征,例如任何可能与您的 crate 不同的类型的 PartialOrd 。即使您将它们绑定(bind)到 Area,它们仍然可以使用您的 crate 在其他 crate(与您无关)中定义。

在我看来,您想说任何实现 Area 的类型都可以/必须实现 PartialOrd 和 PartialEq ,并且这些函数会有默认实现。以下解决方案强制实现这些特征并提供默认函数。尽管实现者需要选择并使用提供的函数作为附加的本地步骤:


struct Rectangle {
width: i32,
height: i32
}

trait Area: PartialOrd+PartialEq {
fn get_area(&self) -> i32;

fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.get_area().partial_cmp(&other.get_area())
}

fn eq(&self, other: &Self) -> bool {
self.get_area().eq(&other.get_area())
}
}

impl Area for Rectangle {
fn get_area(&self) -> i32 {
self.width * self.height
}
}

impl PartialOrd for Rectangle {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
<Self as Area>::partial_cmp(self, other)
}
}

impl PartialEq for Rectangle {
fn eq(&self, other: &Self) -> bool {
<Self as Area>::eq(self, other)
}
}

fn main() {
let r1 = Rectangle { width:10, height:10 };
let r2 = Rectangle { width:11, height:9 };
if r1 > r2 {
println!("r1 is bigger.")
} else {
println!("r2 is bigger.")
}
}

关于rust - 与单一实现冲突的实现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62904267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com