Skip to content

Commit

Permalink
Use primitive_to_wrapped_int for Uint64, Uint128, Uint256 and Uint512
Browse files Browse the repository at this point in the history
(cherry picked from commit e2b5cc2)
  • Loading branch information
webmaster128 committed Jan 29, 2025
1 parent 298bdc6 commit 1a64cf1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 120 deletions.
44 changes: 10 additions & 34 deletions packages/std/src/math/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
Uint256, Uint64,
};

use super::conversion::{forward_try_from, wrapped_int_to_primitive};
use super::conversion::{forward_try_from, primitive_to_wrapped_int, wrapped_int_to_primitive};
use super::impl_int_serde;
use super::num_consts::NumConsts;

Expand Down Expand Up @@ -322,45 +322,21 @@ impl_mul_fraction!(Uint128);
// https://stackoverflow.com/questions/63136970/how-do-i-work-around-the-upstream-crates-may-add-a-new-impl-of-trait-error

// uint to Uint
primitive_to_wrapped_int!(u8, Uint128);
primitive_to_wrapped_int!(u16, Uint128);
primitive_to_wrapped_int!(u32, Uint128);
primitive_to_wrapped_int!(u64, Uint128);
primitive_to_wrapped_int!(u128, Uint128);

// Uint to uint
wrapped_int_to_primitive!(Uint128, u128);

impl From<Uint64> for Uint128 {
fn from(val: Uint64) -> Self {
val.u64().into()
}
}

impl From<u128> for Uint128 {
fn from(val: u128) -> Self {
Uint128(val)
}
}

impl From<u64> for Uint128 {
fn from(val: u64) -> Self {
Uint128(val.into())
}
}

impl From<u32> for Uint128 {
fn from(val: u32) -> Self {
Uint128(val.into())
}
}

impl From<u16> for Uint128 {
fn from(val: u16) -> Self {
Uint128(val.into())
}
}

impl From<u8> for Uint128 {
fn from(val: u8) -> Self {
Uint128(val.into())
}
}

// Uint to uint
wrapped_int_to_primitive!(Uint128, u128);

forward_try_from!(Uint128, Uint64);

// Int to Uint
Expand Down
39 changes: 8 additions & 31 deletions packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
/// the implementation in the future.
use bnum::types::U256;

use super::conversion::{forward_try_from, try_from_int_to_uint};
use super::conversion::{forward_try_from, primitive_to_wrapped_int, try_from_int_to_uint};
use super::impl_int_serde;
use super::num_consts::NumConsts;

Expand Down Expand Up @@ -385,6 +385,13 @@ impl NumConsts for Uint256 {

impl_mul_fraction!(Uint256);

// uint to Uint
primitive_to_wrapped_int!(u8, Uint256);
primitive_to_wrapped_int!(u16, Uint256);
primitive_to_wrapped_int!(u32, Uint256);
primitive_to_wrapped_int!(u64, Uint256);
primitive_to_wrapped_int!(u128, Uint256);

impl From<Uint128> for Uint256 {
fn from(val: Uint128) -> Self {
val.u128().into()
Expand All @@ -397,36 +404,6 @@ impl From<Uint64> for Uint256 {
}
}

impl From<u128> for Uint256 {
fn from(val: u128) -> Self {
Uint256(val.into())
}
}

impl From<u64> for Uint256 {
fn from(val: u64) -> Self {
Uint256(val.into())
}
}

impl From<u32> for Uint256 {
fn from(val: u32) -> Self {
Uint256(val.into())
}
}

impl From<u16> for Uint256 {
fn from(val: u16) -> Self {
Uint256(val.into())
}
}

impl From<u8> for Uint256 {
fn from(val: u8) -> Self {
Uint256(val.into())
}
}

forward_try_from!(Uint256, Uint128);
forward_try_from!(Uint256, Uint64);

Expand Down
39 changes: 8 additions & 31 deletions packages/std/src/math/uint512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
/// the implementation in the future.
use bnum::types::U512;

use super::conversion::{forward_try_from, try_from_int_to_uint};
use super::conversion::{forward_try_from, primitive_to_wrapped_int, try_from_int_to_uint};
use super::impl_int_serde;
use super::num_consts::NumConsts;

Expand Down Expand Up @@ -348,6 +348,13 @@ impl NumConsts for Uint512 {
const MIN: Self = Self::MIN;
}

// uint to Uint
primitive_to_wrapped_int!(u8, Uint512);
primitive_to_wrapped_int!(u16, Uint512);
primitive_to_wrapped_int!(u32, Uint512);
primitive_to_wrapped_int!(u64, Uint512);
primitive_to_wrapped_int!(u128, Uint512);

impl From<Uint256> for Uint512 {
fn from(val: Uint256) -> Self {
let mut bytes = [0u8; 64];
Expand All @@ -369,36 +376,6 @@ impl From<Uint64> for Uint512 {
}
}

impl From<u128> for Uint512 {
fn from(val: u128) -> Self {
Uint512(val.into())
}
}

impl From<u64> for Uint512 {
fn from(val: u64) -> Self {
Uint512(val.into())
}
}

impl From<u32> for Uint512 {
fn from(val: u32) -> Self {
Uint512(val.into())
}
}

impl From<u16> for Uint512 {
fn from(val: u16) -> Self {
Uint512(val.into())
}
}

impl From<u8> for Uint512 {
fn from(val: u8) -> Self {
Uint512(val.into())
}
}

impl TryFrom<Uint512> for Uint256 {
type Error = ConversionOverflowError;

Expand Down
29 changes: 5 additions & 24 deletions packages/std/src/math/uint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
Uint128,
};

use super::conversion::{forward_try_from, wrapped_int_to_primitive};
use super::conversion::{forward_try_from, primitive_to_wrapped_int, wrapped_int_to_primitive};
use super::impl_int_serde;
use super::num_consts::NumConsts;

Expand Down Expand Up @@ -318,29 +318,10 @@ impl_mul_fraction!(Uint64);
// https://stackoverflow.com/questions/63136970/how-do-i-work-around-the-upstream-crates-may-add-a-new-impl-of-trait-error

// uint to Uint
impl From<u64> for Uint64 {
fn from(val: u64) -> Self {
Uint64(val)
}
}

impl From<u32> for Uint64 {
fn from(val: u32) -> Self {
Uint64(val.into())
}
}

impl From<u16> for Uint64 {
fn from(val: u16) -> Self {
Uint64(val.into())
}
}

impl From<u8> for Uint64 {
fn from(val: u8) -> Self {
Uint64(val.into())
}
}
primitive_to_wrapped_int!(u8, Uint64);
primitive_to_wrapped_int!(u16, Uint64);
primitive_to_wrapped_int!(u32, Uint64);
primitive_to_wrapped_int!(u64, Uint64);

// Uint to uint
wrapped_int_to_primitive!(Uint64, u64);
Expand Down

0 comments on commit 1a64cf1

Please sign in to comment.