Skip to content

Commit

Permalink
clippy: replace fold calls with try_fold
Browse files Browse the repository at this point in the history
  • Loading branch information
apoelstra committed Aug 8, 2024
1 parent 9815ca7 commit bb88c18
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
3 changes: 1 addition & 2 deletions src/descriptor/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,8 +874,7 @@ fn parse_xkey_deriv<K: InnerXKey>(
// step all the vectors of indexes contain a single element. If it did though, one of the
// vectors contains more than one element.
// Now transform this list of vectors of steps into distinct derivation paths.
.fold(Ok(Vec::new()), |paths, index_list| {
let mut paths = paths?;
.try_fold(Vec::new(), |mut paths, index_list| {
let mut index_list = index_list?.into_iter();
let first_index = index_list
.next()
Expand Down
31 changes: 13 additions & 18 deletions src/miniscript/types/extra_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl OpLimits {

/// Worst case opcode count when this element is satisfied
pub fn op_count(&self) -> Option<usize> {
opt_add(Some(self.count), self.sat)
self.sat.map(|sat| self.count + sat)
}
}

Expand Down Expand Up @@ -821,11 +821,11 @@ impl Property for ExtData {
.iter()
.rev()
.enumerate()
.fold(Some(0), |acc, (i, &(x, y))| {
.try_fold(0, |acc, (i, &(x, y))| {
if i <= k {
opt_add(acc, x)
x.map(|x| x + acc)
} else {
opt_add(acc, y)
y.map(|y| y + acc)
}
});

Expand All @@ -834,11 +834,11 @@ impl Property for ExtData {
.iter()
.rev()
.enumerate()
.fold(Some(0), |acc, (i, &(x, y))| {
.try_fold(0, |acc, (i, &(x, y))| {
if i <= k {
opt_max(acc, x)
x.map(|x| cmp::max(x, acc))
} else {
opt_max(acc, y)
y.map(|y| cmp::max(y, acc))
}
});

Expand All @@ -848,11 +848,11 @@ impl Property for ExtData {
max_sat_size_vec
.iter()
.enumerate()
.fold(Some((0, 0)), |acc, (i, &(x, y))| {
.try_fold((0, 0), |acc, (i, &(x, y))| {
if i <= k {
opt_tuple_add(acc, x)
x.map(|x| (acc.0 + x.0, acc.1 + x.1))
} else {
opt_tuple_add(acc, y)
y.map(|y| (acc.0 + y.0, acc.1 + y.1))
}
});

Expand All @@ -861,11 +861,11 @@ impl Property for ExtData {
ops_count_sat_vec
.iter()
.enumerate()
.fold(Some(0), |acc, (i, &(x, y))| {
.try_fold(0, |acc, (i, &(x, y))| {
if i <= k {
opt_add(acc, x)
x.map(|x| x + acc)
} else {
opt_add(acc, Some(y))
Some(y + acc)
}
});

Expand Down Expand Up @@ -1083,11 +1083,6 @@ fn opt_add(a: Option<usize>, b: Option<usize>) -> Option<usize> {
a.and_then(|x| b.map(|y| x + y))
}

/// Returns Some((x0+y0, x1+y1)) is both x and y are Some. Otherwise, returns `None`.
fn opt_tuple_add(a: Option<(usize, usize)>, b: Option<(usize, usize)>) -> Option<(usize, usize)> {
a.and_then(|x| b.map(|(w, s)| (w + x.0, s + x.1)))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit bb88c18

Please sign in to comment.