Skip to content

Commit

Permalink
Uint256: dedup implementation from_u128 in from_uint128; Fix review c…
Browse files Browse the repository at this point in the history
…omments
  • Loading branch information
ueco-jb committed Apr 4, 2022
1 parent 47f9286 commit e08b5c5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
6 changes: 4 additions & 2 deletions packages/std/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ impl Decimal {
pub const MAX: Self = Self(Uint128::MAX);

/// Creates a Decimal(value)
/// This is equivalent to `Decimal::from_atomics(value, 18)` but usable in a const context.
pub const fn new(value: Uint128) -> Self {
Self(value)
}

/// Creates a Decimal(Uint128(value))
/// This is equivalent to `Decimal::from_atomics(value, 18)` but usable in a const context.
pub const fn raw(value: u128) -> Self {
Self(Uint128::new(value))
}
Expand Down Expand Up @@ -491,13 +493,13 @@ mod tests {
#[test]
fn decimal_new() {
let expected = Uint128::from(300u128);
assert_eq!(expected, Decimal::new(expected).0);
assert_eq!(Decimal::new(expected).0, expected);
}

#[test]
fn decimal_raw() {
let value = 300u128;
assert_eq!(value, Decimal::raw(value).0.u128());
assert_eq!(Decimal::raw(value).0.u128(), value);
}

#[test]
Expand Down
10 changes: 6 additions & 4 deletions packages/std/src/math/decimal256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ impl Decimal256 {

pub const MAX: Self = Self(Uint256::MAX);

/// Creates a Decimal from Uint256
/// Creates a Decimal256 from Uint256
/// This is equivalent to `Decimal256::from_atomics(value, 18)` but usable in a const context.
pub const fn new(value: Uint256) -> Self {
Self(value)
}

/// Creates a Decimal from u128
/// Creates a Decimal256 from u128
/// This is equivalent to `Decimal256::from_atomics(value, 18)` but usable in a const context.
pub const fn raw(value: u128) -> Self {
Self(Uint256::from_u128(value))
}
Expand Down Expand Up @@ -504,14 +506,14 @@ mod tests {
#[test]
fn decimal256_new() {
let expected = Uint256::from(300u128);
assert_eq!(expected, Decimal256::new(expected).0);
assert_eq!(Decimal256::new(expected).0, expected);
}

#[test]
fn decimal256_raw() {
let value = 300u128;
let expected = Uint256::from(value);
assert_eq!(expected, Decimal256::raw(value).0);
assert_eq!(Decimal256::raw(value).0, expected);
}

#[test]
Expand Down
8 changes: 1 addition & 7 deletions packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,7 @@ impl Uint256 {
/// A conversion from `Uint128` that, unlike the one provided by the `From` trait,
/// can be used in a `const` context.
pub const fn from_uint128(num: Uint128) -> Self {
let bytes = num.to_le_bytes();

Self::from_le_bytes([
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15],
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
])
Self::from_u128(num.u128())
}

/// Returns a copy of the number as big endian bytes.
Expand Down

0 comments on commit e08b5c5

Please sign in to comment.