Cleanup the ExpandedDifficulty PartialOrd impl (#1466)

This commit is contained in:
teor 2020-12-14 11:25:50 +10:00 committed by GitHub
parent 394634c933
commit 66e300be0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 15 deletions

View File

@ -451,28 +451,26 @@ impl PartialOrd<block::Hash> for ExpandedDifficulty {
impl PartialEq<ExpandedDifficulty> for block::Hash {
/// Is `self` equal to `other`?
///
/// See `partial_cmp` for details.
/// See `<ExpandedDifficulty as PartialOrd<block::Hash>::partial_cmp`
/// for details.
fn eq(&self, other: &ExpandedDifficulty) -> bool {
other.eq(self)
}
}
impl PartialOrd<ExpandedDifficulty> for block::Hash {
/// `block::Hash`es are compared with `ExpandedDifficulty` thresholds by
/// converting the hash to a 256-bit integer in little-endian order.
/// How does `self` compare to `other`?
///
/// See `<ExpandedDifficulty as PartialOrd<block::Hash>::partial_cmp`
/// for details.
fn partial_cmp(&self, other: &ExpandedDifficulty) -> Option<Ordering> {
use Ordering::*;
// Use the base implementation, but reverse the order.
match other
.partial_cmp(self)
.expect("difficulties and hashes have a total order")
{
Less => Greater,
Greater => Less,
Equal => Equal,
}
.into()
Some(
// Use the canonical implementation, but reverse the order
other
.partial_cmp(self)
.expect("difficulties and hashes have a total order")
.reverse(),
)
}
}