Merge pull request #32 from paritytech/nv

Fix docs, remove outdated comments, remove overflowing divs/rems.
This commit is contained in:
Nikolay Volf 2018-08-22 18:09:55 +03:00 committed by GitHub
commit 772149e2c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 27 deletions

View File

@ -350,7 +350,6 @@ macro_rules! construct_uint {
/// Little-endian large integer type /// Little-endian large integer type
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone, Eq, PartialEq, Hash)] #[derive(Copy, Clone, Eq, PartialEq, Hash)]
// TODO: serialize stuff? #[cfg_attr(feature="serialize", derive(Serialize, Deserialize))]
pub struct $name(pub [u64; $n_words]); pub struct $name(pub [u64; $n_words]);
impl AsRef<$name> for $name { impl AsRef<$name> for $name {
@ -366,7 +365,10 @@ macro_rules! construct_uint {
} }
impl $name { impl $name {
/// Maximum value.
pub const MAX: $name = $name([u64::max_value(); $n_words]); pub const MAX: $name = $name([u64::max_value(); $n_words]);
/// Convert from a decimal string. /// Convert from a decimal string.
pub fn from_dec_str(value: &str) -> Result<Self, $crate::FromDecStrErr> { pub fn from_dec_str(value: &str) -> Result<Self, $crate::FromDecStrErr> {
if !value.bytes().all(|b| b >= 48 && b <= 57) { if !value.bytes().all(|b| b >= 48 && b <= 57) {
@ -395,7 +397,7 @@ macro_rules! construct_uint {
arr[0] as u32 arr[0] as u32
} }
/// Conversion to u64 /// Low word (u64)
#[inline] #[inline]
pub fn low_u64(&self) -> u64 { pub fn low_u64(&self) -> u64 {
let &$name(ref arr) = self; let &$name(ref arr) = self;
@ -420,7 +422,7 @@ macro_rules! construct_uint {
/// ///
/// # Panics /// # Panics
/// ///
/// Panics if the number is larger than 2^64. /// Panics if the number is larger than u64::max_value().
#[inline] #[inline]
pub fn as_u64(&self) -> u64 { pub fn as_u64(&self) -> u64 {
let &$name(ref arr) = self; let &$name(ref arr) = self;
@ -608,8 +610,7 @@ macro_rules! construct_uint {
x * y x * y
} }
/// Fast exponentation by squaring /// Fast exponentation by squaring. Returns result and overflow flag.
/// https://en.wikipedia.org/wiki/Exponentiation_by_squaring
pub fn overflowing_pow(self, expon: Self) -> (Self, bool) { pub fn overflowing_pow(self, expon: Self) -> (Self, bool) {
if expon.is_zero() { return (Self::one(), false) } if expon.is_zero() { return (Self::one(), false) }
@ -635,13 +636,13 @@ macro_rules! construct_uint {
(res, overflow) (res, overflow)
} }
/// Optimized instructions /// Add with overflow.
#[inline(always)] #[inline(always)]
pub fn overflowing_add(self, other: $name) -> ($name, bool) { pub fn overflowing_add(self, other: $name) -> ($name, bool) {
uint_overflowing_add!($name, $n_words, self, other) uint_overflowing_add!($name, $n_words, self, other)
} }
/// Addition which saturates at the maximum value. /// Addition which saturates at the maximum value (Self::max_value()).
pub fn saturating_add(self, other: $name) -> $name { pub fn saturating_add(self, other: $name) -> $name {
match self.overflowing_add(other) { match self.overflowing_add(other) {
(_, true) => $name::max_value(), (_, true) => $name::max_value(),
@ -701,11 +702,6 @@ macro_rules! construct_uint {
} }
} }
/// Division with overflow
pub fn overflowing_div(self, other: $name) -> ($name, bool) {
(self / other, false)
}
/// Checked division. Returns `None` if `other == 0`. /// Checked division. Returns `None` if `other == 0`.
pub fn checked_div(self, other: $name) -> Option<$name> { pub fn checked_div(self, other: $name) -> Option<$name> {
if other.is_zero() { if other.is_zero() {
@ -715,11 +711,6 @@ macro_rules! construct_uint {
} }
} }
/// Modulus with overflow.
pub fn overflowing_rem(self, other: $name) -> ($name, bool) {
(self % other, false)
}
/// Checked modulus. Returns `None` if `other == 0`. /// Checked modulus. Returns `None` if `other == 0`.
pub fn checked_rem(self, other: $name) -> Option<$name> { pub fn checked_rem(self, other: $name) -> Option<$name> {
if other.is_zero() { if other.is_zero() {
@ -746,14 +737,13 @@ macro_rules! construct_uint {
} }
} }
/// Multiplication by u32 /// Multiplication by u32.
#[deprecated(note = "Use Mul<u32> instead.")] #[deprecated(note = "Use Mul<u32> instead.")]
pub fn mul_u32(self, other: u32) -> Self { pub fn mul_u32(self, other: u32) -> Self {
self * other self * other
} }
/// Overflowing multiplication by u32 /// Overflowing multiplication by u32.
#[allow(dead_code)] // not used when multiplied with inline assembly
fn overflowing_mul_u32(self, other: u32) -> (Self, bool) { fn overflowing_mul_u32(self, other: u32) -> (Self, bool) {
let $name(ref arr) = self; let $name(ref arr) = self;
let mut ret = [0u64; $n_words]; let mut ret = [0u64; $n_words];
@ -771,9 +761,7 @@ macro_rules! construct_uint {
impl_std_for_uint_internals!($name, $n_words); impl_std_for_uint_internals!($name, $n_words);
/// Converts from big endian representation bytes in memory /// Converts from big endian representation bytes in memory.
/// Can also be used as (&slice).into(), as it is default `From`
/// slice implementation for U256
pub fn from_big_endian(slice: &[u8]) -> Self { pub fn from_big_endian(slice: &[u8]) -> Self {
assert!($n_words * 8 >= slice.len()); assert!($n_words * 8 >= slice.len());
@ -792,7 +780,7 @@ macro_rules! construct_uint {
$name(ret) $name(ret)
} }
/// Converts from little endian representation bytes in memory /// Converts from little endian representation bytes in memory.
pub fn from_little_endian(slice: &[u8]) -> Self { pub fn from_little_endian(slice: &[u8]) -> Self {
assert!($n_words * 8 >= slice.len()); assert!($n_words * 8 >= slice.len());
@ -1018,9 +1006,7 @@ macro_rules! construct_uint {
impl<T> ::core::ops::DivAssign<T> for $name where T: Into<$name> { impl<T> ::core::ops::DivAssign<T> for $name where T: Into<$name> {
fn div_assign(&mut self, other: T) { fn div_assign(&mut self, other: T) {
let (result, overflow) = self.overflowing_div(other.into()); *self = *self / other.into();
panic_on_overflow!(overflow);
*self = result
} }
} }