From fbf8ff45aece3b4fa4ab622e2fe02af2718c3b1f Mon Sep 17 00:00:00 2001 From: Matthieu Pizenberg Date: Fri, 13 Nov 2020 23:46:12 +0100 Subject: [PATCH] refactor: use min_by in decision making helper --- src/solver.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/solver.rs b/src/solver.rs index 7c9647eb..2b2f21e2 100644 --- a/src/solver.rs +++ b/src/solver.rs @@ -263,22 +263,17 @@ where I: Iterator, F: Fn(&P) -> I, { - let mut package: Option<(T, _)> = None; - let mut min_key = usize::MAX; - for (p, term) in potential_packages { - let key = list_available_versions(p.borrow()) - .filter(|v| term.borrow().contains(v.borrow())) - .count(); - - if key < min_key { - min_key = key; - package = Some((p, term)); - } - } - let (package, term) = package.expect("potential_packages gave us an empty iterator"); - let ver = - list_available_versions(package.borrow()).find(|v| term.borrow().contains(v.borrow())); - (package, ver) + let count_valid = |(p, range): &(T, U)| { + list_available_versions(p.borrow()) + .filter(|v| range.borrow().contains(v.borrow())) + .count() + }; + let (pkg, range) = potential_packages + .min_by_key(count_valid) + .expect("potential_packages gave us an empty iterator"); + let version = + list_available_versions(pkg.borrow()).find(|v| range.borrow().contains(v.borrow())); + (pkg, version) } /// A basic implementation of [DependencyProvider].