Skip to content

AdEx v5: Issue #384 Unified precision - primitives::UnifiedNum #386

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

Merged
merged 8 commits into from
Mar 23, 2021
84 changes: 70 additions & 14 deletions primitives/src/big_num.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use std::convert::TryFrom;
use std::fmt;
use std::iter::Sum;
use std::ops::{Add, AddAssign, Div, Mul, Sub};
use std::str::FromStr;

use num::rational::Ratio;
use num::{BigUint, CheckedSub, Integer};
use std::{
convert::TryFrom,
fmt,
iter::Sum,
ops::{Add, AddAssign, Div, Mul, Sub},
str::FromStr,
};

use num::{pow::Pow, rational::Ratio, BigUint, CheckedSub, Integer};
use num_derive::{Num, NumOps, One, Zero};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::UnifiedNum;

#[derive(
Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, NumOps, One, Zero, Num, Default,
)]
Expand Down Expand Up @@ -58,6 +61,12 @@ impl fmt::Debug for BigNum {
}
}

impl fmt::Display for BigNum {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

impl Integer for BigNum {
fn div_floor(&self, other: &Self) -> Self {
self.0.div_floor(&other.0).into()
Expand Down Expand Up @@ -98,6 +107,46 @@ impl Integer for BigNum {
}
}

impl Pow<BigNum> for BigNum {
type Output = BigNum;

fn pow(self, rhs: BigNum) -> Self::Output {
Self(self.0.pow(rhs.0))
}
}

impl Pow<&BigNum> for BigNum {
type Output = BigNum;

fn pow(self, rhs: &BigNum) -> Self::Output {
BigNum(self.0.pow(&rhs.0))
}
}

impl Pow<BigNum> for &BigNum {
type Output = BigNum;

fn pow(self, rhs: BigNum) -> Self::Output {
BigNum(Pow::pow(&self.0, rhs.0))
}
}

impl Pow<&BigNum> for &BigNum {
type Output = BigNum;

fn pow(self, rhs: &BigNum) -> Self::Output {
BigNum(Pow::pow(&self.0, &rhs.0))
}
}

impl Pow<u8> for BigNum {
type Output = BigNum;

fn pow(self, rhs: u8) -> Self::Output {
BigNum(self.0.pow(rhs))
}
}

impl Add<&BigNum> for &BigNum {
type Output = BigNum;

Expand Down Expand Up @@ -209,12 +258,6 @@ impl FromStr for BigNum {
}
}

impl ToString for BigNum {
fn to_string(&self) -> String {
self.0.to_str_radix(10)
}
}

impl From<u64> for BigNum {
fn from(value: u64) -> Self {
Self(BigUint::from(value))
Expand All @@ -227,6 +270,12 @@ impl From<BigUint> for BigNum {
}
}

impl<'a> Sum<&'a UnifiedNum> for BigNum {
fn sum<I: Iterator<Item = &'a UnifiedNum>>(iter: I) -> BigNum {
BigNum(iter.map(|unified| BigUint::from(unified.to_u64())).sum())
}
}

fn biguint_from_str<'de, D>(deserializer: D) -> Result<BigUint, D::Error>
where
D: Deserializer<'de>,
Expand Down Expand Up @@ -297,4 +346,11 @@ mod test {
let expected: BigNum = 11.into();
assert_eq!(expected, &big_num * &ratio);
}
#[test]
fn bignum_formatting() {
let bignum: BigNum = 5000.into();

assert_eq!("5000", &bignum.to_string());
assert_eq!("BigNum(radix: 10; 5000)", &format!("{:?}", &bignum));
}
}
11 changes: 7 additions & 4 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![deny(rust_2018_idioms)]
#![deny(clippy::all)]
use std::{error, fmt};

pub use self::{
ad_slot::AdSlot,
ad_unit::AdUnit,
Expand All @@ -10,28 +12,32 @@ pub use self::{
config::Config,
event_submission::EventSubmission,
ipfs::IPFS,
unified_num::UnifiedNum,
validator::{ValidatorDesc, ValidatorId},
};
use std::{error, fmt};

mod ad_slot;
mod ad_unit;
pub mod adapter;
pub mod address;
pub mod analytics;
pub mod balances_map;
pub mod big_num;
pub mod campaign;
pub mod channel;
pub mod channel_v5;
pub mod channel_validator;
pub mod config;
mod eth_checksum;
pub mod event_submission;
pub mod ipfs;
pub mod market;
pub mod merkle_tree;
pub mod sentry;
pub mod supermarket;
pub mod targeting;
mod unified_num;
pub mod validator;

pub mod util {
pub use api::ApiUrl;
Expand All @@ -52,9 +58,6 @@ pub mod util {

pub mod logging;
}
pub mod analytics;
mod eth_checksum;
pub mod validator;

#[derive(Debug, PartialEq, Eq)]
pub enum DomainError {
Expand Down
Loading