Skip to content

Commit e2b5cc2

Browse files
committed
Use primitive_to_wrapped_int for Uint64, Uint128, Uint256 and Uint512
1 parent bd2c1ef commit e2b5cc2

File tree

4 files changed

+31
-120
lines changed

4 files changed

+31
-120
lines changed

packages/std/src/math/uint128.rs

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
Uint256, Uint64,
1717
};
1818

19-
use super::conversion::{forward_try_from, wrapped_int_to_primitive};
19+
use super::conversion::{forward_try_from, primitive_to_wrapped_int, wrapped_int_to_primitive};
2020
use super::impl_int_serde;
2121
use super::num_consts::NumConsts;
2222

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

324324
// uint to Uint
325+
primitive_to_wrapped_int!(u8, Uint128);
326+
primitive_to_wrapped_int!(u16, Uint128);
327+
primitive_to_wrapped_int!(u32, Uint128);
328+
primitive_to_wrapped_int!(u64, Uint128);
329+
primitive_to_wrapped_int!(u128, Uint128);
330+
331+
// Uint to uint
332+
wrapped_int_to_primitive!(Uint128, u128);
333+
325334
impl From<Uint64> for Uint128 {
326335
fn from(val: Uint64) -> Self {
327336
val.u64().into()
328337
}
329338
}
330339

331-
impl From<u128> for Uint128 {
332-
fn from(val: u128) -> Self {
333-
Uint128(val)
334-
}
335-
}
336-
337-
impl From<u64> for Uint128 {
338-
fn from(val: u64) -> Self {
339-
Uint128(val.into())
340-
}
341-
}
342-
343-
impl From<u32> for Uint128 {
344-
fn from(val: u32) -> Self {
345-
Uint128(val.into())
346-
}
347-
}
348-
349-
impl From<u16> for Uint128 {
350-
fn from(val: u16) -> Self {
351-
Uint128(val.into())
352-
}
353-
}
354-
355-
impl From<u8> for Uint128 {
356-
fn from(val: u8) -> Self {
357-
Uint128(val.into())
358-
}
359-
}
360-
361-
// Uint to uint
362-
wrapped_int_to_primitive!(Uint128, u128);
363-
364340
forward_try_from!(Uint128, Uint64);
365341

366342
// Int to Uint

packages/std/src/math/uint256.rs

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
/// the implementation in the future.
2121
use bnum::types::U256;
2222

23-
use super::conversion::{forward_try_from, try_from_int_to_uint};
23+
use super::conversion::{forward_try_from, primitive_to_wrapped_int, try_from_int_to_uint};
2424
use super::impl_int_serde;
2525
use super::num_consts::NumConsts;
2626

@@ -385,6 +385,13 @@ impl NumConsts for Uint256 {
385385

386386
impl_mul_fraction!(Uint256);
387387

388+
// uint to Uint
389+
primitive_to_wrapped_int!(u8, Uint256);
390+
primitive_to_wrapped_int!(u16, Uint256);
391+
primitive_to_wrapped_int!(u32, Uint256);
392+
primitive_to_wrapped_int!(u64, Uint256);
393+
primitive_to_wrapped_int!(u128, Uint256);
394+
388395
impl From<Uint128> for Uint256 {
389396
fn from(val: Uint128) -> Self {
390397
val.u128().into()
@@ -397,36 +404,6 @@ impl From<Uint64> for Uint256 {
397404
}
398405
}
399406

400-
impl From<u128> for Uint256 {
401-
fn from(val: u128) -> Self {
402-
Uint256(val.into())
403-
}
404-
}
405-
406-
impl From<u64> for Uint256 {
407-
fn from(val: u64) -> Self {
408-
Uint256(val.into())
409-
}
410-
}
411-
412-
impl From<u32> for Uint256 {
413-
fn from(val: u32) -> Self {
414-
Uint256(val.into())
415-
}
416-
}
417-
418-
impl From<u16> for Uint256 {
419-
fn from(val: u16) -> Self {
420-
Uint256(val.into())
421-
}
422-
}
423-
424-
impl From<u8> for Uint256 {
425-
fn from(val: u8) -> Self {
426-
Uint256(val.into())
427-
}
428-
}
429-
430407
forward_try_from!(Uint256, Uint128);
431408
forward_try_from!(Uint256, Uint64);
432409

packages/std/src/math/uint512.rs

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
/// the implementation in the future.
1919
use bnum::types::U512;
2020

21-
use super::conversion::{forward_try_from, try_from_int_to_uint};
21+
use super::conversion::{forward_try_from, primitive_to_wrapped_int, try_from_int_to_uint};
2222
use super::impl_int_serde;
2323
use super::num_consts::NumConsts;
2424

@@ -348,6 +348,13 @@ impl NumConsts for Uint512 {
348348
const MIN: Self = Self::MIN;
349349
}
350350

351+
// uint to Uint
352+
primitive_to_wrapped_int!(u8, Uint512);
353+
primitive_to_wrapped_int!(u16, Uint512);
354+
primitive_to_wrapped_int!(u32, Uint512);
355+
primitive_to_wrapped_int!(u64, Uint512);
356+
primitive_to_wrapped_int!(u128, Uint512);
357+
351358
impl From<Uint256> for Uint512 {
352359
fn from(val: Uint256) -> Self {
353360
let mut bytes = [0u8; 64];
@@ -369,36 +376,6 @@ impl From<Uint64> for Uint512 {
369376
}
370377
}
371378

372-
impl From<u128> for Uint512 {
373-
fn from(val: u128) -> Self {
374-
Uint512(val.into())
375-
}
376-
}
377-
378-
impl From<u64> for Uint512 {
379-
fn from(val: u64) -> Self {
380-
Uint512(val.into())
381-
}
382-
}
383-
384-
impl From<u32> for Uint512 {
385-
fn from(val: u32) -> Self {
386-
Uint512(val.into())
387-
}
388-
}
389-
390-
impl From<u16> for Uint512 {
391-
fn from(val: u16) -> Self {
392-
Uint512(val.into())
393-
}
394-
}
395-
396-
impl From<u8> for Uint512 {
397-
fn from(val: u8) -> Self {
398-
Uint512(val.into())
399-
}
400-
}
401-
402379
impl TryFrom<Uint512> for Uint256 {
403380
type Error = ConversionOverflowError;
404381

packages/std/src/math/uint64.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
Uint128,
1616
};
1717

18-
use super::conversion::{forward_try_from, wrapped_int_to_primitive};
18+
use super::conversion::{forward_try_from, primitive_to_wrapped_int, wrapped_int_to_primitive};
1919
use super::impl_int_serde;
2020
use super::num_consts::NumConsts;
2121

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

320320
// uint to Uint
321-
impl From<u64> for Uint64 {
322-
fn from(val: u64) -> Self {
323-
Uint64(val)
324-
}
325-
}
326-
327-
impl From<u32> for Uint64 {
328-
fn from(val: u32) -> Self {
329-
Uint64(val.into())
330-
}
331-
}
332-
333-
impl From<u16> for Uint64 {
334-
fn from(val: u16) -> Self {
335-
Uint64(val.into())
336-
}
337-
}
338-
339-
impl From<u8> for Uint64 {
340-
fn from(val: u8) -> Self {
341-
Uint64(val.into())
342-
}
343-
}
321+
primitive_to_wrapped_int!(u8, Uint64);
322+
primitive_to_wrapped_int!(u16, Uint64);
323+
primitive_to_wrapped_int!(u32, Uint64);
324+
primitive_to_wrapped_int!(u64, Uint64);
344325

345326
// Uint to uint
346327
wrapped_int_to_primitive!(Uint64, u64);

0 commit comments

Comments
 (0)