Skip to content

Commit a0deb06

Browse files
committed
replace manual impl with TryFrom bounds & default method
1 parent 91b1855 commit a0deb06

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

gix-utils/src/btoi.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,13 @@ pub fn to_signed_with_radix<I: MinNumTraits>(bytes: &[u8], radix: u32) -> Result
269269
}
270270

271271
/// minimal subset of traits used by [`to_signed_with_radix`] and [`to_unsigned_with_radix`]
272-
pub trait MinNumTraits: Sized + Copy {
272+
pub trait MinNumTraits: Sized + Copy + TryFrom<u32> {
273273
/// the 0 value for this type
274274
const ZERO: Self;
275-
///
276-
fn from_u32(n: u32) -> Option<Self>;
275+
/// convert from a unsinged 32-bit word
276+
fn from_u32(n: u32) -> Option<Self> {
277+
Self::try_from(n).ok()
278+
}
277279
/// the checked multiplication operation for this type
278280
fn checked_mul(self, rhs: Self) -> Option<Self>;
279281
/// the chekced addition operation for this type
@@ -291,24 +293,18 @@ macro_rules! impl_checked {
291293
}
292294

293295
macro_rules! min_num_traits {
294-
($t : ty, from_u32 => $from_u32 : expr) => {
296+
($t:ty) => {
295297
impl MinNumTraits for $t {
296298
const ZERO: Self = 0;
297-
298-
fn from_u32(n: u32) -> Option<$t> {
299-
#[allow(clippy::redundant_closure_call)]
300-
$from_u32(n)
301-
}
302-
303299
impl_checked!(checked_add);
304300
impl_checked!(checked_mul);
305301
impl_checked!(checked_sub);
306302
}
307303
};
308304
}
309305

310-
min_num_traits!(i32, from_u32 => |n: u32| n.try_into().ok());
311-
min_num_traits!(i64, from_u32 => |n: u32| Some(n.into()));
312-
min_num_traits!(u64, from_u32 => |n: u32| Some(n.into()));
313-
min_num_traits!(u8, from_u32 => |n: u32| n.try_into().ok());
314-
min_num_traits!(usize, from_u32 => |n: u32| n.try_into().ok());
306+
min_num_traits!(i32);
307+
min_num_traits!(i64);
308+
min_num_traits!(u64);
309+
min_num_traits!(u8);
310+
min_num_traits!(usize);

0 commit comments

Comments
 (0)