私は newtype を持っていて、実装したいOrd
:
use std::cmp::{Ord, Ordering};
struct MyType(isize);
impl Ord for MyType {
fn cmp(&self, &other: Self) -> Ordering {
let MyType(ref lhs) = *self;
let MyType(ref rhs) = *other;
lhs.cmp(rhs)
}
}
私のタイプの 2 つの変数を比較しようとすると、エラーが発生します。
error[E0277]: the trait bound `MyType: std::cmp::PartialOrd` is not satisfied
--> src/main.rs:5:6
|
5 | impl Ord for MyType {
| ^^^ can't compare `MyType` with `MyType`
|
= help: the trait `std::cmp::PartialOrd` is not implemented for `MyType`
error[E0277]: the trait bound `MyType: std::cmp::Eq` is not satisfied
--> src/main.rs:5:6
|
5 | impl Ord for MyType {
| ^^^ the trait `std::cmp::Eq` is not implemented for `MyType`
PartialEq
、Eq
and PartialOrd
( gt()
、lt()
、eq()
、ge()
、など)を実装するとle()
、すべて正常に動作しますが、 を指定すると、 and !cmp
のような関数を推測できます。これは冗長です!私はこれが好きではありません!lt()
eq()
docsを見ると、 の定義にOrd
次のように表示されます。
pub trait Ord: Eq + PartialOrd<Self>
Eq
これは、特性がおよびから継承しているように見えますPartialOrd
。cmp
関数を使用して、継承されたトレイトから必要なメソッドのデフォルトの実装をトレイトが提供できないのはなぜですか? 特性の継承がどのように機能するかはわかりませんが、検索しても何も役に立ちませんでしたが、これは可能であるべきだと思います。
これはRustでどのように行われますか? このようにならないことを願っています...