Add a bunch of Ord/PartialOrd impls for stuff

This commit is contained in:
Andrew Poelstra 2015-12-20 15:38:02 -06:00
parent e05e6d2215
commit 16f5878a03
4 changed files with 24 additions and 20 deletions

View File

@ -1,7 +1,7 @@
[package]
name = "bitcoin"
version = "0.5.0"
version = "0.5.1"
authors = ["Andrew Poelstra <apoelstra@wpsoftware.net>"]
license = "CC0-1.0"
homepage = "https://github.com/apoelstra/rust-bitcoin/"

View File

@ -33,7 +33,7 @@ use network::encodable::ConsensusEncodable;
use network::serialize::BitcoinHash;
/// A reference to a transaction output
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct TxOutRef {
/// The referenced transaction's txid
pub txid: Sha256dHash,

View File

@ -115,6 +115,28 @@ macro_rules! impl_array_newtype {
impl Eq for $thing {}
impl PartialOrd for $thing {
#[inline]
fn partial_cmp(&self, other: &$thing) -> Option<::std::cmp::Ordering> {
Some(self.cmp(&other))
}
}
impl Ord for $thing {
#[inline]
fn cmp(&self, other: &$thing) -> ::std::cmp::Ordering {
// manually implement comparison to get little-endian ordering
// (we need this for our numeric types; non-numeric ones shouldn't
// be ordered anyway except to put them in BTrees or whatever, and
// they don't care how we order as long as we're consisistent).
for i in 0..$len {
if self[$len - 1 - i] < other[$len - 1 - i] { return ::std::cmp::Ordering::Less; }
if self[$len - 1 - i] > other[$len - 1 - i] { return ::std::cmp::Ordering::Greater; }
}
::std::cmp::Ordering::Equal
}
}
impl Clone for $thing {
#[inline]
fn clone(&self) -> $thing {

View File

@ -307,24 +307,6 @@ macro_rules! construct_uint {
}
}
impl ::std::cmp::Ord for $name {
fn cmp(&self, other: &$name) -> ::std::cmp::Ordering {
let &$name(ref me) = self;
let &$name(ref you) = other;
for i in 0..$n_words {
if me[$n_words - 1 - i] < you[$n_words - 1 - i] { return ::std::cmp::Ordering::Less; }
if me[$n_words - 1 - i] > you[$n_words - 1 - i] { return ::std::cmp::Ordering::Greater; }
}
::std::cmp::Ordering::Equal
}
}
impl ::std::cmp::PartialOrd for $name {
fn partial_cmp(&self, other: &$name) -> Option<::std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl fmt::Debug for $name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &$name(ref data) = self;