Skip to content

Commit 25913e8

Browse files
committed
Use the ? operator with Option
1 parent 7c48ca7 commit 25913e8

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

src/bigint.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,16 +2331,14 @@ impl ToPrimitive for BigInt {
23312331

23322332
#[inline]
23332333
fn to_f32(&self) -> Option<f32> {
2334-
self.data
2335-
.to_f32()
2336-
.map(|n| if self.sign == Minus { -n } else { n })
2334+
let n = self.data.to_f32()?;
2335+
Some(if self.sign == Minus { -n } else { n })
23372336
}
23382337

23392338
#[inline]
23402339
fn to_f64(&self) -> Option<f64> {
2341-
self.data
2342-
.to_f64()
2343-
.map(|n| if self.sign == Minus { -n } else { n })
2340+
let n = self.data.to_f64()?;
2341+
Some(if self.sign == Minus { -n } else { n })
23442342
}
23452343
}
23462344

@@ -2370,7 +2368,8 @@ impl FromPrimitive for BigInt {
23702368
if n >= 0.0 {
23712369
BigUint::from_f64(n).map(BigInt::from)
23722370
} else {
2373-
BigUint::from_f64(-n).map(|x| -BigInt::from(x))
2371+
let x = BigUint::from_f64(-n)?;
2372+
Some(-BigInt::from(x))
23742373
}
23752374
}
23762375
}
@@ -2727,9 +2726,8 @@ impl BigInt {
27272726
/// ```
27282727
#[inline]
27292728
pub fn parse_bytes(buf: &[u8], radix: u32) -> Option<BigInt> {
2730-
str::from_utf8(buf)
2731-
.ok()
2732-
.and_then(|s| BigInt::from_str_radix(s, radix).ok())
2729+
let s = str::from_utf8(buf).ok()?;
2730+
BigInt::from_str_radix(s, radix).ok()
27332731
}
27342732

27352733
/// Creates and initializes a `BigInt`. Each u8 of the input slice is
@@ -2749,7 +2747,8 @@ impl BigInt {
27492747
/// assert_eq!(a.to_radix_be(190), (Sign:: Minus, inbase190));
27502748
/// ```
27512749
pub fn from_radix_be(sign: Sign, buf: &[u8], radix: u32) -> Option<BigInt> {
2752-
BigUint::from_radix_be(buf, radix).map(|u| BigInt::from_biguint(sign, u))
2750+
let u = BigUint::from_radix_be(buf, radix)?;
2751+
Some(BigInt::from_biguint(sign, u))
27532752
}
27542753

27552754
/// Creates and initializes a `BigInt`. Each u8 of the input slice is
@@ -2769,7 +2768,8 @@ impl BigInt {
27692768
/// assert_eq!(a.to_radix_be(190), (Sign::Minus, inbase190));
27702769
/// ```
27712770
pub fn from_radix_le(sign: Sign, buf: &[u8], radix: u32) -> Option<BigInt> {
2772-
BigUint::from_radix_le(buf, radix).map(|u| BigInt::from_biguint(sign, u))
2771+
let u = BigUint::from_radix_le(buf, radix)?;
2772+
Some(BigInt::from_biguint(sign, u))
27732773
}
27742774

27752775
/// Returns the sign and the byte representation of the `BigInt` in big-endian byte order.

src/biguint.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,9 +2205,8 @@ impl BigUint {
22052205
/// ```
22062206
#[inline]
22072207
pub fn parse_bytes(buf: &[u8], radix: u32) -> Option<BigUint> {
2208-
str::from_utf8(buf)
2209-
.ok()
2210-
.and_then(|s| BigUint::from_str_radix(s, radix).ok())
2208+
let s = str::from_utf8(buf).ok()?;
2209+
BigUint::from_str_radix(s, radix).ok()
22112210
}
22122211

22132212
/// Creates and initializes a `BigUint`. Each u8 of the input slice is
@@ -2493,11 +2492,8 @@ impl BigUint {
24932492
/// Returns the number of least-significant bits that are zero,
24942493
/// or `None` if the entire number is zero.
24952494
pub fn trailing_zeros(&self) -> Option<usize> {
2496-
self.data
2497-
.iter()
2498-
.enumerate()
2499-
.find(|&(_, &digit)| digit != 0)
2500-
.map(|(i, digit)| i * big_digit::BITS + digit.trailing_zeros() as usize)
2495+
let i = self.data.iter().position(|&digit| digit != 0)?;
2496+
Some(i * big_digit::BITS + self.data[i].trailing_zeros() as usize)
25012497
}
25022498
}
25032499

0 commit comments

Comments
 (0)