From 512eee04cf53c1863ccb560e96e80d28c391475c Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 16 Jan 2016 16:26:46 +0100 Subject: [PATCH 1/2] Assign-operators for Uint. --- src/lib.rs | 1 + src/network/host.rs | 2 +- src/tinykeccak.c | 2 -- src/uint.rs | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1a7d873fe2d..4bc47e61cfe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![feature(op_assign_traits)] +#![feature(augmented_assignments)] #![feature(associated_consts)] #![feature(wrapping)] //! Ethcore-util library diff --git a/src/network/host.rs b/src/network/host.rs index 32ac713ef4b..90cbfdaaddf 100644 --- a/src/network/host.rs +++ b/src/network/host.rs @@ -567,7 +567,7 @@ impl IoHandler> for Host where Messa self.add_node("enode://de471bccee3d042261d52e9bff31458daecc406142b401d4cd848f677479f73104b9fdeb090af9583d3391b7f10cb2ba9e26865dd5fca4fcdc0fb1e3b723c786@54.94.239.50:30303"); // BR self.add_node("enode://1118980bf48b0a3640bdba04e0fe78b1add18e1cd99bf22d53daac1fd9972ad650df52176e7c7d89d1114cfef2bc23a2959aa54998a46afcf7d91809f0855082@52.74.57.123:30303"); // SG // ETH/DEV cpp-ethereum (poc-9.ethdev.com) - self.add_node("enode://979b7fa28feeb35a4741660a16076f1943202cb72b6af70d327f053e248bab9ba81760f39d0701ef1d8f89cc1fbd2cacba0710a12cd5314d5e0c9021aa3637f9@5.1.83.226:30303"); + self.add_node("enode://979b7fa28feeb35a4741660a16076f1943202cb72b6af70d327f053e248bab9ba81760f39d0701ef1d8f89cc1fbd2cacba0710a12cd5314d5e0c9021aa3637f9@5.1.83.226:30303"); } fn stream_hup<'s>(&'s mut self, io: &mut IoContext<'s, NetworkIoMessage>, stream: StreamToken) { diff --git a/src/tinykeccak.c b/src/tinykeccak.c index ddc3f5be251..46683891378 100644 --- a/src/tinykeccak.c +++ b/src/tinykeccak.c @@ -1,6 +1,4 @@ #include -#include -#include #include /** libkeccak-tiny diff --git a/src/uint.rs b/src/uint.rs index 5073747ebd1..89834a85a6c 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -474,6 +474,38 @@ macro_rules! construct_uint { } } + // TODO: optimise and traitify. + + impl<'a> AddAssign<&'a $name> for $name { + fn add_assign(&mut self, other: &'a Self) { + *self = self.add(*other); + } + } + + impl<'a> SubAssign<&'a $name> for $name { + fn sub_assign(&mut self, other: &'a Self) { + *self = self.sub(*other); + } + } + + impl<'a> MulAssign<&'a $name> for $name { + fn mul_assign(&mut self, other: &'a Self) { + *self = self.mul(*other); + } + } + + impl<'a> DivAssign<&'a $name> for $name { + fn div_assign(&mut self, other: &'a Self) { + *self = self.div(*other); + } + } + + impl<'a> RemAssign<&'a $name> for $name { + fn rem_assign(&mut self, other: &'a Self) { + *self = self.rem(*other); + } + } + impl BitAnd<$name> for $name { type Output = $name; From 0c2869d5420c3f1273d5203633bad931371cbaa2 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 16 Jan 2016 16:52:59 +0100 Subject: [PATCH 2/2] Tesys for U256 assign ops. --- src/uint.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/uint.rs b/src/uint.rs index 89834a85a6c..00865387d4e 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -506,6 +506,36 @@ macro_rules! construct_uint { } } + impl AddAssign<$name> for $name { + fn add_assign(&mut self, other: Self) { + *self = self.add(other); + } + } + + impl SubAssign<$name> for $name { + fn sub_assign(&mut self, other: Self) { + *self = self.sub(other); + } + } + + impl MulAssign<$name> for $name { + fn mul_assign(&mut self, other: Self) { + *self = self.mul(other); + } + } + + impl DivAssign<$name> for $name { + fn div_assign(&mut self, other: Self) { + *self = self.div(other); + } + } + + impl RemAssign<$name> for $name { + fn rem_assign(&mut self, other: Self) { + *self = self.rem(other); + } + } + impl BitAnd<$name> for $name { type Output = $name; @@ -776,6 +806,37 @@ mod tests { use std::str::FromStr; use std::num::wrapping::OverflowingOps; + #[test] + pub fn assign_ops() { + let x: U256 = x!(69); + let y: U256 = x!(42); + { + let mut z = x; + z += y; + assert_eq!(z, x + y); + } + { + let mut z = x; + z -= y; + assert_eq!(z, x - y); + } + { + let mut z = x; + z *= y; + assert_eq!(z, x * y); + } + { + let mut z = x; + z /= y; + assert_eq!(z, x / y); + } + { + let mut z = x; + z %= y; + assert_eq!(z, x % y); + } + } + #[test] pub fn uint256_from() { let e = U256([10, 0, 0, 0]);