-
Notifications
You must be signed in to change notification settings - Fork 218
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
Implement Num from num-traits #480
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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; | ||
|
||
pub use uint::{FromStrRadixErr, FromStrRadixErrKind}; | ||
|
||
/// 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> { | ||
Self::from_str_radix(txt, radix) | ||
} | ||
} | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ use fixed_hash::{construct_fixed_hash, impl_fixed_hash_conversions}; | |
#[cfg(feature = "scale-info")] | ||
use scale_info::TypeInfo; | ||
use uint::{construct_uint, uint_full_mul_reg}; | ||
pub use uint::{FromStrRadixErr, FromStrRadixErrKind}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, I overlooked this line, seems redundant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not super familiar with the macro hygiene rules, but what if the downstream crate did a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, fair point, we can do what |
||
|
||
/// Error type for conversion. | ||
#[derive(Debug, PartialEq, Eq)] | ||
|
@@ -68,6 +69,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::*; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why this is needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bit of a leftover from moving
from_str_radix
into theuint
crate. I don't believe this crate needs it ever, but there is a feature that enablesstd
inuint
andnum-traits
.