Skip to content

Commit fdb60ca

Browse files
committed
Add usize and isize TryFrom implementations
1 parent 8d5dded commit fdb60ca

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

src/int.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ impl Int {
427427
}
428428

429429
fmt_impls!(Int);
430-
convert_impls!(Int, i8, i16, i32, i64, i128, u8, u16, u32);
430+
convert_impls!(Int, i8, i16, i32, i64, i128, isize, u8, u16, u32, usize);
431431

432432
impl From<u8> for Int {
433433
fn from(val: u8) -> Self {

src/macros.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,19 @@ macro_rules! fmt_impls {
3131
}
3232

3333
macro_rules! convert_impls {
34-
($type:ident, $t8:ident, $t16:ident, $t32:ident, $t64:ident, $t128:ident, $ot8:ident, $ot16:ident, $ot32:ident) => {
34+
(
35+
$type:ident,
36+
$t8:ident,
37+
$t16:ident,
38+
$t32:ident,
39+
$t64:ident,
40+
$t128:ident,
41+
$tsize:ident,
42+
$ot8:ident,
43+
$ot16:ident,
44+
$ot32:ident,
45+
$otsize:ident
46+
) => {
3547
impl ::core::convert::From<$t8> for $type {
3648
fn from(val: $t8) -> Self {
3749
Self($t64::from(val))
@@ -68,6 +80,26 @@ macro_rules! convert_impls {
6880
}
6981
}
7082

83+
impl ::core::convert::TryFrom<$tsize> for $type {
84+
type Error = crate::error::TryFromIntError;
85+
86+
fn try_from(val: $tsize) -> Result<Self, crate::error::TryFromIntError> {
87+
$t64::try_from(val)
88+
.map_err(|_| crate::error::TryFromIntError::new())
89+
.and_then($type::try_from)
90+
}
91+
}
92+
93+
impl ::core::convert::TryFrom<$otsize> for $type {
94+
type Error = crate::error::TryFromIntError;
95+
96+
fn try_from(val: $otsize) -> Result<Self, crate::error::TryFromIntError> {
97+
$t64::try_from(val)
98+
.map_err(|_| crate::error::TryFromIntError::new())
99+
.and_then($type::try_from)
100+
}
101+
}
102+
71103
impl ::core::convert::TryFrom<$type> for $t8 {
72104
type Error = ::core::num::TryFromIntError;
73105

@@ -128,6 +160,22 @@ macro_rules! convert_impls {
128160
}
129161
}
130162

163+
impl ::core::convert::TryFrom<$type> for $tsize {
164+
type Error = ::core::num::TryFromIntError;
165+
166+
fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> {
167+
Self::try_from(val.0)
168+
}
169+
}
170+
171+
impl ::core::convert::TryFrom<$type> for $otsize {
172+
type Error = ::core::num::TryFromIntError;
173+
174+
fn try_from(val: $type) -> Result<Self, ::core::num::TryFromIntError> {
175+
Self::try_from(val.0)
176+
}
177+
}
178+
131179
impl ::core::convert::From<$type> for f64 {
132180
fn from(val: $type) -> Self {
133181
val.0 as f64

src/uint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ impl UInt {
420420
}
421421

422422
fmt_impls!(UInt);
423-
convert_impls!(UInt, u8, u16, u32, u64, u128, i8, i16, i32);
423+
convert_impls!(UInt, u8, u16, u32, u64, u128, usize, i8, i16, i32, isize);
424424

425425
impl TryFrom<i8> for UInt {
426426
type Error = TryFromIntError;

0 commit comments

Comments
 (0)