由于.max()
对于f64
无效,我正在编写一个ForceOrd
结构体来确保参数不是NaN
。预期使用方法如下:
let m = xs.iter().map(|&x| ForceOrd(x)).max().unwrap().into();
然而,我无法编译出符合
Into
特性的实现,出现以下错误:conflicting implementations of trait `std::convert::Into<_>` for type `ForceOrd<_>`
这里是代码(在线示例):
#[derive(PartialEq, PartialOrd)]
pub struct ForceOrd<X: PartialEq + PartialOrd>(pub X);
impl<X: PartialEq + PartialOrd> Eq for ForceOrd<X> { }
impl<X: PartialEq + PartialOrd> Ord for ForceOrd<X> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.partial_cmp(&other.0).unwrap()
}
}
/// doesn't work
impl<X: PartialEq + PartialOrd> Into<X> for ForceOrd<X> {
fn into(x: Self) -> X { x.0 }
}
/// doesn't work either
impl<X: PartialEq + PartialOrd> From<ForceOrd<X>> for X {
fn from(x: ForceOrd<X>) -> Self { x.0 }
}
From
,那么这应该是你得到的错误)。 - Simon Whitehead