Merge pull request #59 from gavofyork/gav

Assign-operators for Uint.
This commit is contained in:
Arkadiy Paronyan 2016-01-16 17:18:35 +01:00
commit a04d5c2af7
4 changed files with 95 additions and 3 deletions

View File

@ -1,4 +1,5 @@
#![feature(op_assign_traits)]
#![feature(augmented_assignments)]
#![feature(associated_consts)]
#![feature(wrapping)]
//! Ethcore-util library

View File

@ -1,6 +1,4 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/** libkeccak-tiny

View File

@ -585,6 +585,68 @@ 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 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;
@ -855,6 +917,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]);