Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uint: make const matching work again #421

Merged
merged 4 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions uint/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog].

## [Unreleased]

## [0.8.5] - 2020-08-12
- Make const matching work again. [#421](https://github.com/paritytech/parity-common/pull/421)

## [0.8.4] - 2020-08-03
- Added a manual impl of `Eq` and `Hash`. [#390](https://github.com/paritytech/parity-common/pull/390)
- Removed some unsafe code and added big-endian support. [#407](https://github.com/paritytech/parity-common/pull/407)
Expand Down
2 changes: 1 addition & 1 deletion uint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ homepage = "http://parity.io"
repository = "https://github.com/paritytech/parity-common"
license = "MIT OR Apache-2.0"
name = "uint"
version = "0.8.4"
version = "0.8.5"
authors = ["Parity Technologies <admin@parity.io>"]
readme = "README.md"
edition = "2018"
Expand Down
25 changes: 4 additions & 21 deletions uint/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ macro_rules! uint_overflowing_binop {
}

($name(ret), carry > 0)
}};
}};
}

#[macro_export]
Expand Down Expand Up @@ -244,7 +244,7 @@ macro_rules! panic_on_overflow {
($name: expr) => {
if $name {
panic!("arithmetic operation overflow")
}
}
};
}

Expand Down Expand Up @@ -445,7 +445,7 @@ macro_rules! construct_uint {
/// Little-endian large integer type
#[repr(C)]
$(#[$attr])*
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
niklasad1 marked this conversation as resolved.
Show resolved Hide resolved
$visibility struct $name (pub [u64; $n_words]);

/// Get a reference to the underlying little-endian words.
Expand Down Expand Up @@ -1475,26 +1475,9 @@ macro_rules! construct_uint {
}
}

// We implement `Eq` and `Hash` manually to workaround
// https://github.com/rust-lang/rust/issues/61415
impl $crate::core_::cmp::PartialEq for $name {
fn eq(&self, other: &$name) -> bool {
self.as_ref() == other.as_ref()
}
}

impl $crate::core_::cmp::Eq for $name {}

impl $crate::core_::hash::Hash for $name {
fn hash<H: $crate::core_::hash::Hasher>(&self, state: &mut H) {
// use the impl as slice &[u64]
self.as_ref().hash(state);
}
}

impl $crate::core_::cmp::Ord for $name {
fn cmp(&self, other: &$name) -> $crate::core_::cmp::Ordering {
self.as_ref().iter().rev().cmp(other.as_ref().iter().rev())
self.as_ref().iter().rev().cmp(other.as_ref().iter().rev())
}
}

Expand Down
10 changes: 10 additions & 0 deletions uint/tests/uint_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ fn hash_impl_is_the_same_as_for_a_slice() {
assert_eq!(uint_hash, slice_hash);
}

// https://github.com/paritytech/parity-common/issues/420
#[test]
fn const_matching_works() {
const ONE: U256 = U256([1, 0, 0, 0]);
match U256::zero() {
ONE => unreachable!(),
_ => {}
}
}

#[test]
fn u128_conversions() {
let mut a = U256::from(u128::max_value());
Expand Down