Skip to content

Commit

Permalink
make u128 function as const (#92)
Browse files Browse the repository at this point in the history
* make u128 function as const

* impl iter sum for U128
  • Loading branch information
laizy authored Sep 13, 2020
1 parent e7abb1d commit 44c1285
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions ontio-std/src/types/num.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::fmt::{Debug, Display, Result};
use core::fmt::{Debug, Display, Formatter, Result};
use core::iter::Sum;
use core::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
use fixed_hash::static_assertions::_core::fmt::Formatter;

#[derive(Clone, Copy, PartialOrd, PartialEq, Eq, Default)]
pub struct U128(u128);
Expand All @@ -16,15 +16,15 @@ impl I128 {
U128(self.0 as u128)
}

pub fn from_le_bytes(bs: [u8; 16]) -> Self {
pub const fn from_le_bytes(bs: [u8; 16]) -> Self {
I128(i128::from_le_bytes(bs))
}

pub fn to_le_bytes(self) -> [u8; 16] {
pub const fn to_le_bytes(self) -> [u8; 16] {
self.0.to_le_bytes()
}

pub fn raw(self) -> i128 {
pub const fn raw(self) -> i128 {
self.0
}
}
Expand All @@ -33,27 +33,33 @@ impl U128 {
pub const fn new(val: u128) -> Self {
U128(val)
}
pub fn from_le_bytes(bs: [u8; 16]) -> Self {
pub const fn from_le_bytes(bs: [u8; 16]) -> Self {
U128(u128::from_le_bytes(bs))
}

pub fn is_zero(self) -> bool {
pub const fn is_zero(self) -> bool {
self.0 == 0
}

pub fn to_le_bytes(self) -> [u8; 16] {
pub const fn to_le_bytes(self) -> [u8; 16] {
self.0.to_le_bytes()
}

pub fn raw(self) -> u128 {
pub const fn raw(self) -> u128 {
self.0
}

pub fn to_i128(self) -> I128 {
pub const fn to_i128(self) -> I128 {
I128(self.0 as i128)
}
}

impl Sum for U128 {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(U128::new(0), Add::add)
}
}

impl Add<U128> for U128 {
type Output = U128;

Expand Down

0 comments on commit 44c1285

Please sign in to comment.