Skip to content

Commit f458966

Browse files
authored
Merge pull request #1297 from fbstj/main
following up on btoi removal
2 parents cebeedf + a0deb06 commit f458966

File tree

1 file changed

+32
-38
lines changed

1 file changed

+32
-38
lines changed

gix-utils/src/btoi.rs

+32-38
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub fn to_unsigned_with_radix<I: MinNumTraits>(bytes: &[u8], radix: u32) -> Resu
118118
return Err(ParseIntegerError { kind: ErrorKind::Empty });
119119
}
120120

121-
let mut result = I::zero();
121+
let mut result = I::ZERO;
122122

123123
for &digit in bytes {
124124
let x = match char::from(digit).to_digit(radix).and_then(I::from_u32) {
@@ -236,7 +236,7 @@ pub fn to_signed_with_radix<I: MinNumTraits>(bytes: &[u8], radix: u32) -> Result
236236
return Err(ParseIntegerError { kind: ErrorKind::Empty });
237237
}
238238

239-
let mut result = I::zero();
239+
let mut result = I::ZERO;
240240

241241
for &digit in digits {
242242
let x = match char::from(digit).to_digit(radix).and_then(I::from_u32) {
@@ -269,48 +269,42 @@ 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 {
273-
///
274-
fn from_u32(n: u32) -> Option<Self>;
275-
///
276-
fn zero() -> Self;
277-
///
278-
fn checked_mul(self, v: Self) -> Option<Self>;
279-
///
280-
fn checked_add(self, v: Self) -> Option<Self>;
281-
///
272+
pub trait MinNumTraits: Sized + Copy + TryFrom<u32> {
273+
/// the 0 value for this type
274+
const ZERO: Self;
275+
/// convert from a unsinged 32-bit word
276+
fn from_u32(n: u32) -> Option<Self> {
277+
Self::try_from(n).ok()
278+
}
279+
/// the checked multiplication operation for this type
280+
fn checked_mul(self, rhs: Self) -> Option<Self>;
281+
/// the chekced addition operation for this type
282+
fn checked_add(self, rhs: Self) -> Option<Self>;
283+
/// the checked subtraction operation for this type
282284
fn checked_sub(self, v: Self) -> Option<Self>;
283285
}
284286

287+
macro_rules! impl_checked {
288+
($f:ident) => {
289+
fn $f(self, rhs: Self) -> Option<Self> {
290+
Self::$f(self, rhs)
291+
}
292+
};
293+
}
294+
285295
macro_rules! min_num_traits {
286-
($t : ty, from_u32 => $from_u32 : expr) => {
296+
($t:ty) => {
287297
impl MinNumTraits for $t {
288-
fn from_u32(n: u32) -> Option<$t> {
289-
#[allow(clippy::redundant_closure_call)]
290-
$from_u32(n)
291-
}
292-
293-
fn zero() -> Self {
294-
0
295-
}
296-
297-
fn checked_mul(self, v: $t) -> Option<$t> {
298-
<$t>::checked_mul(self, v)
299-
}
300-
301-
fn checked_add(self, v: $t) -> Option<$t> {
302-
<$t>::checked_add(self, v)
303-
}
304-
305-
fn checked_sub(self, v: $t) -> Option<$t> {
306-
<$t>::checked_sub(self, v)
307-
}
298+
const ZERO: Self = 0;
299+
impl_checked!(checked_add);
300+
impl_checked!(checked_mul);
301+
impl_checked!(checked_sub);
308302
}
309303
};
310304
}
311305

312-
min_num_traits!(i32, from_u32 => |n: u32| n.try_into().ok());
313-
min_num_traits!(i64, from_u32 => |n: u32| Some(n.into()));
314-
min_num_traits!(u64, from_u32 => |n: u32| Some(n.into()));
315-
min_num_traits!(u8, from_u32 => |n: u32| n.try_into().ok());
316-
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)