Skip to content

Commit 4cae665

Browse files
committed
Two small improvements
In `librustc_apfloat/ieee.rs`, use the iterator.[r]find methods to simplify the code. In `libserialize/json.rs`, make use of the fact that `Vec.last` on an empty `Vec` returns `None` to simplify the code to a single match.
1 parent 81cfaad commit 4cae665

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

src/librustc_apfloat/ieee.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -2306,24 +2306,14 @@ mod sig {
23062306

23072307
/// One, not zero, based LSB. That is, returns 0 for a zeroed significand.
23082308
pub(super) fn olsb(limbs: &[Limb]) -> usize {
2309-
for (i, &limb) in limbs.iter().enumerate() {
2310-
if limb != 0 {
2311-
return i * LIMB_BITS + limb.trailing_zeros() as usize + 1;
2312-
}
2313-
}
2314-
2315-
0
2309+
limbs.iter().enumerate().find(|(_, &limb)| limb != 0).map_or(0,
2310+
|(i, limb)| i * LIMB_BITS + limb.trailing_zeros() as usize + 1)
23162311
}
23172312

23182313
/// One, not zero, based MSB. That is, returns 0 for a zeroed significand.
23192314
pub(super) fn omsb(limbs: &[Limb]) -> usize {
2320-
for (i, &limb) in limbs.iter().enumerate().rev() {
2321-
if limb != 0 {
2322-
return (i + 1) * LIMB_BITS - limb.leading_zeros() as usize;
2323-
}
2324-
}
2325-
2326-
0
2315+
limbs.iter().enumerate().rfind(|(_, &limb)| limb != 0).map_or(0,
2316+
|(i, limb)| (i + 1) * LIMB_BITS - limb.leading_zeros() as usize)
23272317
}
23282318

23292319
/// Comparison (unsigned) of two significands.

src/libserialize/json.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1387,10 +1387,9 @@ impl Stack {
13871387

13881388
// Used by Parser to test whether the top-most element is an index.
13891389
fn last_is_index(&self) -> bool {
1390-
if let Some(InternalIndex(_)) = self.stack.last() {
1391-
true
1392-
} else {
1393-
false
1390+
match self.stack.last() {
1391+
Some(InternalIndex(_)) => true,
1392+
_ => false,
13941393
}
13951394
}
13961395

0 commit comments

Comments
 (0)