Skip to content

Commit

Permalink
CReate and use primitive_to_wrapped_int to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Jan 22, 2025
1 parent 3b8071b commit bd2c1ef
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 212 deletions.
14 changes: 14 additions & 0 deletions packages/std/src/math/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ macro_rules! wrapped_int_to_primitive {
}
pub(crate) use wrapped_int_to_primitive;

/// Helper macro to implement `From` for a type that is just a wrapper around another type.
/// This can be used for all our integer conversions where `bnum` implements `From`.
macro_rules! primitive_to_wrapped_int {
($input: ty, $output: ty) => {
impl From<$input> for $output {
fn from(value: $input) -> Self {
// By convention all our Uint*/Int* types store the value in .0
Self(value.into())
}
}
};
}
pub(crate) use primitive_to_wrapped_int;

/// Helper macro to implement `TryFrom` for a conversion from a bigger signed int to a smaller one.
/// This is needed because `bnum` does not implement `TryFrom` for those conversions
/// because of limitations of const generics.
Expand Down
65 changes: 12 additions & 53 deletions packages/std/src/math/int128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use crate::{
__internal::forward_ref_partial_eq,
};

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

Expand Down Expand Up @@ -306,29 +308,10 @@ forward_try_from!(Uint256, Int128);
forward_try_from!(Uint512, Int128);

// uint to Int
impl From<u64> for Int128 {
fn from(val: u64) -> Self {
Int128(val.into())
}
}

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

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

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

// Int to Int
impl From<Int64> for Int128 {
Expand All @@ -341,35 +324,11 @@ try_from_int_to_int!(Int256, Int128);
try_from_int_to_int!(Int512, Int128);

// int to Int
impl From<i128> for Int128 {
fn from(val: i128) -> Self {
Int128(val)
}
}

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

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

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

impl From<i8> for Int128 {
fn from(val: i8) -> Self {
Int128(val.into())
}
}
primitive_to_wrapped_int!(i8, Int128);
primitive_to_wrapped_int!(i16, Int128);
primitive_to_wrapped_int!(i32, Int128);
primitive_to_wrapped_int!(i64, Int128);
primitive_to_wrapped_int!(i128, Int128);

// Int to int
wrapped_int_to_primitive!(Int128, i128);
Expand Down
72 changes: 13 additions & 59 deletions packages/std/src/math/int256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use crate::{
/// the implementation in the future.
use bnum::types::{I256, U256};

use super::conversion::{grow_be_int, try_from_int_to_int, try_from_uint_to_int};
use super::conversion::{
grow_be_int, primitive_to_wrapped_int, try_from_int_to_int, try_from_uint_to_int,
};
use super::impl_int_serde;
use super::num_consts::NumConsts;

Expand Down Expand Up @@ -370,35 +372,11 @@ impl From<Uint64> for Int256 {
}

// uint to Int
impl From<u128> for Int256 {
fn from(val: u128) -> Self {
Int256(val.into())
}
}

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

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

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

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

// Int to Int
try_from_int_to_int!(Int512, Int256);
Expand All @@ -416,35 +394,11 @@ impl From<Int64> for Int256 {
}

// int to Int
impl From<i128> for Int256 {
fn from(val: i128) -> Self {
Int256(val.into())
}
}

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

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

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

impl From<i8> for Int256 {
fn from(val: i8) -> Self {
Int256(val.into())
}
}
primitive_to_wrapped_int!(i8, Int256);
primitive_to_wrapped_int!(i16, Int256);
primitive_to_wrapped_int!(i32, Int256);
primitive_to_wrapped_int!(i64, Int256);
primitive_to_wrapped_int!(i128, Int256);

impl TryFrom<&str> for Int256 {
type Error = StdError;
Expand Down
70 changes: 11 additions & 59 deletions packages/std/src/math/int512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
/// the implementation in the future.
use bnum::types::{I512, U512};

use super::conversion::{grow_be_int, try_from_uint_to_int};
use super::conversion::{grow_be_int, primitive_to_wrapped_int, try_from_uint_to_int};
use super::impl_int_serde;
use super::num_consts::NumConsts;

Expand Down Expand Up @@ -365,66 +365,18 @@ impl From<Uint64> for Int512 {
}

// uint to Int
impl From<u128> for Int512 {
fn from(val: u128) -> Self {
Int512(val.into())
}
}

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

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

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

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

// int to Int
impl From<i128> for Int512 {
fn from(val: i128) -> Self {
Int512(val.into())
}
}

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

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

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

impl From<i8> for Int512 {
fn from(val: i8) -> Self {
Int512(val.into())
}
}
primitive_to_wrapped_int!(i8, Int512);
primitive_to_wrapped_int!(i16, Int512);
primitive_to_wrapped_int!(i32, Int512);
primitive_to_wrapped_int!(i64, Int512);
primitive_to_wrapped_int!(i128, Int512);

// Int to Int
impl From<Int64> for Int512 {
Expand Down
51 changes: 10 additions & 41 deletions packages/std/src/math/int64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use crate::{
__internal::forward_ref_partial_eq,
};

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

Expand Down Expand Up @@ -296,48 +298,15 @@ impl NumConsts for Int64 {
}

// uint to Int
impl From<u32> for Int64 {
fn from(val: u32) -> Self {
Int64(val.into())
}
}

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

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

// int to Int
impl From<i64> for Int64 {
fn from(val: i64) -> Self {
Int64(val)
}
}

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

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

impl From<i8> for Int64 {
fn from(val: i8) -> Self {
Int64(val.into())
}
}
primitive_to_wrapped_int!(i8, Int64);
primitive_to_wrapped_int!(i16, Int64);
primitive_to_wrapped_int!(i32, Int64);
primitive_to_wrapped_int!(i64, Int64);

// Int to int
wrapped_int_to_primitive!(Int64, i64);
Expand Down

0 comments on commit bd2c1ef

Please sign in to comment.