Skip to content

Commit bd2c1ef

Browse files
committed
CReate and use primitive_to_wrapped_int to improve readability
1 parent 3b8071b commit bd2c1ef

File tree

5 files changed

+60
-212
lines changed

5 files changed

+60
-212
lines changed

packages/std/src/math/conversion.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ macro_rules! wrapped_int_to_primitive {
9696
}
9797
pub(crate) use wrapped_int_to_primitive;
9898

99+
/// Helper macro to implement `From` for a type that is just a wrapper around another type.
100+
/// This can be used for all our integer conversions where `bnum` implements `From`.
101+
macro_rules! primitive_to_wrapped_int {
102+
($input: ty, $output: ty) => {
103+
impl From<$input> for $output {
104+
fn from(value: $input) -> Self {
105+
// By convention all our Uint*/Int* types store the value in .0
106+
Self(value.into())
107+
}
108+
}
109+
};
110+
}
111+
pub(crate) use primitive_to_wrapped_int;
112+
99113
/// Helper macro to implement `TryFrom` for a conversion from a bigger signed int to a smaller one.
100114
/// This is needed because `bnum` does not implement `TryFrom` for those conversions
101115
/// because of limitations of const generics.

packages/std/src/math/int128.rs

Lines changed: 12 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use crate::{
1313
__internal::forward_ref_partial_eq,
1414
};
1515

16-
use super::conversion::{forward_try_from, try_from_int_to_int, wrapped_int_to_primitive};
16+
use super::conversion::{
17+
forward_try_from, primitive_to_wrapped_int, try_from_int_to_int, wrapped_int_to_primitive,
18+
};
1719
use super::impl_int_serde;
1820
use super::num_consts::NumConsts;
1921

@@ -306,29 +308,10 @@ forward_try_from!(Uint256, Int128);
306308
forward_try_from!(Uint512, Int128);
307309

308310
// uint to Int
309-
impl From<u64> for Int128 {
310-
fn from(val: u64) -> Self {
311-
Int128(val.into())
312-
}
313-
}
314-
315-
impl From<u32> for Int128 {
316-
fn from(val: u32) -> Self {
317-
Int128(val.into())
318-
}
319-
}
320-
321-
impl From<u16> for Int128 {
322-
fn from(val: u16) -> Self {
323-
Int128(val.into())
324-
}
325-
}
326-
327-
impl From<u8> for Int128 {
328-
fn from(val: u8) -> Self {
329-
Int128(val.into())
330-
}
331-
}
311+
primitive_to_wrapped_int!(u8, Int128);
312+
primitive_to_wrapped_int!(u16, Int128);
313+
primitive_to_wrapped_int!(u32, Int128);
314+
primitive_to_wrapped_int!(u64, Int128);
332315

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

343326
// int to Int
344-
impl From<i128> for Int128 {
345-
fn from(val: i128) -> Self {
346-
Int128(val)
347-
}
348-
}
349-
350-
impl From<i64> for Int128 {
351-
fn from(val: i64) -> Self {
352-
Int128(val.into())
353-
}
354-
}
355-
356-
impl From<i32> for Int128 {
357-
fn from(val: i32) -> Self {
358-
Int128(val.into())
359-
}
360-
}
361-
362-
impl From<i16> for Int128 {
363-
fn from(val: i16) -> Self {
364-
Int128(val.into())
365-
}
366-
}
367-
368-
impl From<i8> for Int128 {
369-
fn from(val: i8) -> Self {
370-
Int128(val.into())
371-
}
372-
}
327+
primitive_to_wrapped_int!(i8, Int128);
328+
primitive_to_wrapped_int!(i16, Int128);
329+
primitive_to_wrapped_int!(i32, Int128);
330+
primitive_to_wrapped_int!(i64, Int128);
331+
primitive_to_wrapped_int!(i128, Int128);
373332

374333
// Int to int
375334
wrapped_int_to_primitive!(Int128, i128);

packages/std/src/math/int256.rs

Lines changed: 13 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ use crate::{
1717
/// the implementation in the future.
1818
use bnum::types::{I256, U256};
1919

20-
use super::conversion::{grow_be_int, try_from_int_to_int, try_from_uint_to_int};
20+
use super::conversion::{
21+
grow_be_int, primitive_to_wrapped_int, try_from_int_to_int, try_from_uint_to_int,
22+
};
2123
use super::impl_int_serde;
2224
use super::num_consts::NumConsts;
2325

@@ -370,35 +372,11 @@ impl From<Uint64> for Int256 {
370372
}
371373

372374
// uint to Int
373-
impl From<u128> for Int256 {
374-
fn from(val: u128) -> Self {
375-
Int256(val.into())
376-
}
377-
}
378-
379-
impl From<u64> for Int256 {
380-
fn from(val: u64) -> Self {
381-
Int256(val.into())
382-
}
383-
}
384-
385-
impl From<u32> for Int256 {
386-
fn from(val: u32) -> Self {
387-
Int256(val.into())
388-
}
389-
}
390-
391-
impl From<u16> for Int256 {
392-
fn from(val: u16) -> Self {
393-
Int256(val.into())
394-
}
395-
}
396-
397-
impl From<u8> for Int256 {
398-
fn from(val: u8) -> Self {
399-
Int256(val.into())
400-
}
401-
}
375+
primitive_to_wrapped_int!(u8, Int256);
376+
primitive_to_wrapped_int!(u16, Int256);
377+
primitive_to_wrapped_int!(u32, Int256);
378+
primitive_to_wrapped_int!(u64, Int256);
379+
primitive_to_wrapped_int!(u128, Int256);
402380

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

418396
// int to Int
419-
impl From<i128> for Int256 {
420-
fn from(val: i128) -> Self {
421-
Int256(val.into())
422-
}
423-
}
424-
425-
impl From<i64> for Int256 {
426-
fn from(val: i64) -> Self {
427-
Int256(val.into())
428-
}
429-
}
430-
431-
impl From<i32> for Int256 {
432-
fn from(val: i32) -> Self {
433-
Int256(val.into())
434-
}
435-
}
436-
437-
impl From<i16> for Int256 {
438-
fn from(val: i16) -> Self {
439-
Int256(val.into())
440-
}
441-
}
442-
443-
impl From<i8> for Int256 {
444-
fn from(val: i8) -> Self {
445-
Int256(val.into())
446-
}
447-
}
397+
primitive_to_wrapped_int!(i8, Int256);
398+
primitive_to_wrapped_int!(i16, Int256);
399+
primitive_to_wrapped_int!(i32, Int256);
400+
primitive_to_wrapped_int!(i64, Int256);
401+
primitive_to_wrapped_int!(i128, Int256);
448402

449403
impl TryFrom<&str> for Int256 {
450404
type Error = StdError;

packages/std/src/math/int512.rs

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
/// the implementation in the future.
1717
use bnum::types::{I512, U512};
1818

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

@@ -365,66 +365,18 @@ impl From<Uint64> for Int512 {
365365
}
366366

367367
// uint to Int
368-
impl From<u128> for Int512 {
369-
fn from(val: u128) -> Self {
370-
Int512(val.into())
371-
}
372-
}
373-
374-
impl From<u64> for Int512 {
375-
fn from(val: u64) -> Self {
376-
Int512(val.into())
377-
}
378-
}
379-
380-
impl From<u32> for Int512 {
381-
fn from(val: u32) -> Self {
382-
Int512(val.into())
383-
}
384-
}
385-
386-
impl From<u16> for Int512 {
387-
fn from(val: u16) -> Self {
388-
Int512(val.into())
389-
}
390-
}
391-
392-
impl From<u8> for Int512 {
393-
fn from(val: u8) -> Self {
394-
Int512(val.into())
395-
}
396-
}
368+
primitive_to_wrapped_int!(u8, Int512);
369+
primitive_to_wrapped_int!(u16, Int512);
370+
primitive_to_wrapped_int!(u32, Int512);
371+
primitive_to_wrapped_int!(u64, Int512);
372+
primitive_to_wrapped_int!(u128, Int512);
397373

398374
// int to Int
399-
impl From<i128> for Int512 {
400-
fn from(val: i128) -> Self {
401-
Int512(val.into())
402-
}
403-
}
404-
405-
impl From<i64> for Int512 {
406-
fn from(val: i64) -> Self {
407-
Int512(val.into())
408-
}
409-
}
410-
411-
impl From<i32> for Int512 {
412-
fn from(val: i32) -> Self {
413-
Int512(val.into())
414-
}
415-
}
416-
417-
impl From<i16> for Int512 {
418-
fn from(val: i16) -> Self {
419-
Int512(val.into())
420-
}
421-
}
422-
423-
impl From<i8> for Int512 {
424-
fn from(val: i8) -> Self {
425-
Int512(val.into())
426-
}
427-
}
375+
primitive_to_wrapped_int!(i8, Int512);
376+
primitive_to_wrapped_int!(i16, Int512);
377+
primitive_to_wrapped_int!(i32, Int512);
378+
primitive_to_wrapped_int!(i64, Int512);
379+
primitive_to_wrapped_int!(i128, Int512);
428380

429381
// Int to Int
430382
impl From<Int64> for Int512 {

packages/std/src/math/int64.rs

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use crate::{
1313
__internal::forward_ref_partial_eq,
1414
};
1515

16-
use super::conversion::{forward_try_from, try_from_int_to_int, wrapped_int_to_primitive};
16+
use super::conversion::{
17+
forward_try_from, primitive_to_wrapped_int, try_from_int_to_int, wrapped_int_to_primitive,
18+
};
1719
use super::impl_int_serde;
1820
use super::num_consts::NumConsts;
1921

@@ -296,48 +298,15 @@ impl NumConsts for Int64 {
296298
}
297299

298300
// uint to Int
299-
impl From<u32> for Int64 {
300-
fn from(val: u32) -> Self {
301-
Int64(val.into())
302-
}
303-
}
304-
305-
impl From<u16> for Int64 {
306-
fn from(val: u16) -> Self {
307-
Int64(val.into())
308-
}
309-
}
310-
311-
impl From<u8> for Int64 {
312-
fn from(val: u8) -> Self {
313-
Int64(val.into())
314-
}
315-
}
301+
primitive_to_wrapped_int!(u8, Int64);
302+
primitive_to_wrapped_int!(u16, Int64);
303+
primitive_to_wrapped_int!(u32, Int64);
316304

317305
// int to Int
318-
impl From<i64> for Int64 {
319-
fn from(val: i64) -> Self {
320-
Int64(val)
321-
}
322-
}
323-
324-
impl From<i32> for Int64 {
325-
fn from(val: i32) -> Self {
326-
Int64(val.into())
327-
}
328-
}
329-
330-
impl From<i16> for Int64 {
331-
fn from(val: i16) -> Self {
332-
Int64(val.into())
333-
}
334-
}
335-
336-
impl From<i8> for Int64 {
337-
fn from(val: i8) -> Self {
338-
Int64(val.into())
339-
}
340-
}
306+
primitive_to_wrapped_int!(i8, Int64);
307+
primitive_to_wrapped_int!(i16, Int64);
308+
primitive_to_wrapped_int!(i32, Int64);
309+
primitive_to_wrapped_int!(i64, Int64);
341310

342311
// Int to int
343312
wrapped_int_to_primitive!(Int64, i64);

0 commit comments

Comments
 (0)