Skip to content

Commit

Permalink
Implement Num from num-traits
Browse files Browse the repository at this point in the history
  • Loading branch information
SamWilsn committed Dec 17, 2020
1 parent 763c967 commit 4e520df
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions ethereum-types/Cargo.toml
Expand Up @@ -26,3 +26,4 @@ serialize = ["std", "impl-serde", "primitive-types/serde", "ethbloom/serialize"]
arbitrary = ["ethbloom/arbitrary", "fixed-hash/arbitrary", "uint-crate/arbitrary"]
rlp = ["impl-rlp", "ethbloom/rlp", "primitive-types/rlp"]
codec = ["impl-codec", "ethbloom/codec"]
num-traits = ["primitive-types/num-traits"]
2 changes: 2 additions & 0 deletions ethereum-types/src/lib.rs
Expand Up @@ -13,6 +13,8 @@ mod uint;

pub use ethbloom::{Bloom, BloomRef, Input as BloomInput};
pub use hash::{BigEndianHash, H128, H160, H256, H264, H32, H512, H520, H64};
#[cfg(feature = "num-traits")]
pub use primitive_types::{FromStrRadixErr, FromStrRadixErrKind};
pub use uint::{FromDecStrErr, U128, U256, U512, U64};

pub type Address = H160;
Expand Down
2 changes: 2 additions & 0 deletions primitive-types/Cargo.toml
Expand Up @@ -12,6 +12,7 @@ fixed-hash = { version = "0.6", path = "../fixed-hash", default-features = false
uint = { version = "0.8.3", path = "../uint", default-features = false }
impl-serde = { version = "0.3.1", path = "impls/serde", default-features = false, optional = true }
impl-codec = { version = "0.4.1", path = "impls/codec", default-features = false, optional = true }
impl-num-traits = { version = "0.1.0", path = "impls/num-traits", default-features = false, optional = true }
impl-rlp = { version = "0.3", path = "impls/rlp", default-features = false, optional = true }
scale-info = { version = "0.4", features = ["derive"], default-features = false, optional = true }

Expand All @@ -26,6 +27,7 @@ codec = ["impl-codec"]
rlp = ["impl-rlp"]
arbitrary = ["fixed-hash/arbitrary", "uint/arbitrary"]
fp-conversion = ["std"]
num-traits = ["impl-num-traits"]

[[test]]
name = "scale_info"
Expand Down
16 changes: 16 additions & 0 deletions primitive-types/impls/num-traits/Cargo.toml
@@ -0,0 +1,16 @@
[package]
name = "impl-num-traits"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT OR Apache-2.0"
homepage = "https://github.com/paritytech/parity-common"
description = "num-traits implementation for uint."
edition = "2018"

[dependencies]
num-traits = { version = "0.2", default-features = false }
uint = { version = "0.8.5", path = "../../../uint", default-features = false }

[features]
default = ["std"]
std = ["num-traits/std", "uint/std"]
127 changes: 127 additions & 0 deletions primitive-types/impls/num-traits/src/lib.rs
@@ -0,0 +1,127 @@
// Copyright 2020 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! num-traits support for uint.

#![cfg_attr(not(feature = "std"), no_std)]

#[doc(hidden)]
pub use num_traits;

use core::fmt;

#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum FromStrRadixErrKind {
Parse,
UnsupportedRadix,
}

#[derive(Debug)]
enum FromStrRadixErrSrc {
Hex(uint::FromHexError),
Dec(uint::FromDecStrErr),
}

impl fmt::Display for FromStrRadixErrSrc {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FromStrRadixErrSrc::Dec(d) => write!(f, "{}", d),
FromStrRadixErrSrc::Hex(h) => write!(f, "{}", h),
}
}
}

#[derive(Debug)]
pub struct FromStrRadixErr {
kind: FromStrRadixErrKind,
source: Option<FromStrRadixErrSrc>,
}

impl FromStrRadixErr {
pub fn unsupported() -> Self {
Self { kind: FromStrRadixErrKind::UnsupportedRadix, source: None }
}

pub fn kind(&self) -> FromStrRadixErrKind {
self.kind
}
}

impl fmt::Display for FromStrRadixErr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.kind {
FromStrRadixErrKind::UnsupportedRadix => write!(f, "the given radix is not supported"),
FromStrRadixErrKind::Parse => match self.source {
Some(ref src) => write!(f, "{}", src),
None => write!(f, "parsing error"),
},
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for FromStrRadixErr {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self.source {
Some(FromStrRadixErrSrc::Dec(ref d)) => Some(d),
Some(FromStrRadixErrSrc::Hex(ref h)) => Some(h),
None => None,
}
}
}

impl From<uint::FromDecStrErr> for FromStrRadixErr {
fn from(e: uint::FromDecStrErr) -> Self {
Self { kind: FromStrRadixErrKind::Parse, source: Some(FromStrRadixErrSrc::Dec(e)) }
}
}

impl From<uint::FromHexError> for FromStrRadixErr {
fn from(e: uint::FromHexError) -> Self {
Self { kind: FromStrRadixErrKind::Parse, source: Some(FromStrRadixErrSrc::Hex(e)) }
}
}

/// Add num-traits support to an integer created by `construct_uint!`.
#[macro_export]
macro_rules! impl_uint_num_traits {
($name: ident, $len: expr) => {
impl $crate::num_traits::identities::Zero for $name {
#[inline]
fn zero() -> Self {
Self::zero()
}

#[inline]
fn is_zero(&self) -> bool {
self.is_zero()
}
}

impl $crate::num_traits::identities::One for $name {
#[inline]
fn one() -> Self {
Self::one()
}
}

impl $crate::num_traits::Num for $name {
type FromStrRadixErr = $crate::FromStrRadixErr;

fn from_str_radix(txt: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
let parsed = match radix {
10 => Self::from_dec_str(txt)?,
16 => core::str::FromStr::from_str(txt)?,
_ => return Err(Self::FromStrRadixErr::unsupported()),
};

Ok(parsed)
}
}
};
}
12 changes: 12 additions & 0 deletions primitive-types/src/lib.rs
Expand Up @@ -19,6 +19,8 @@ mod fp_conversion;

use core::convert::TryFrom;
use fixed_hash::{construct_fixed_hash, impl_fixed_hash_conversions};
#[cfg(feature = "num-traits")]
pub use impl_num_traits::{FromStrRadixErr, FromStrRadixErrKind};
#[cfg(feature = "scale-info")]
use scale_info::TypeInfo;
use uint::{construct_uint, uint_full_mul_reg};
Expand Down Expand Up @@ -68,6 +70,16 @@ construct_fixed_hash! {
pub struct H512(64);
}

#[cfg(feature = "num-traits")]
mod num_traits {
use super::*;
use impl_num_traits::impl_uint_num_traits;

impl_uint_num_traits!(U128, 2);
impl_uint_num_traits!(U256, 4);
impl_uint_num_traits!(U512, 8);
}

#[cfg(feature = "impl-serde")]
mod serde {
use super::*;
Expand Down

0 comments on commit 4e520df

Please sign in to comment.