treefile: Minor cleanup of handle_repo_packages_overrides()

This isn't important at all, but I am just trying to up my
"Rust iterator and mapping" skills.  Here:

- In this case we always want to go from `Option<T>` to `Option<T>`,
  so instead of destructuring `Some()` back to another `Some()`
  we can use `as_mut()` to manipulate the inner vector directly.
- Now instead of destroying the inner `Vec<>` we need to use `drain()`
  instead
- And then we can just `extend()` with a reversed iterator from
  our explicit temporary
This commit is contained in:
Colin Walters 2021-04-30 09:18:14 -04:00
parent 7fe133b6d8
commit 73b9d2ff3b

View File

@ -1306,24 +1306,23 @@ impl TreeComposeConfig {
// we need to ensure that appended repo packages override earlier ones // we need to ensure that appended repo packages override earlier ones
fn handle_repo_packages_overrides(&mut self) { fn handle_repo_packages_overrides(&mut self) {
if let Some(repo_packages) = self.repo_packages.take() { self.repo_packages.as_mut().map(|repo_packages| {
let mut seen_pkgs: HashSet<String> = HashSet::new(); let mut seen_pkgs: HashSet<String> = HashSet::new();
self.repo_packages = Some({ // Create a temporary new filtered vec; see
let mut v: Vec<RepoPackage> = repo_packages // https://doc.rust-lang.org/std/iter/struct.Map.html#notes-about-side-effects for why
.into_iter() // the reverse and re-reverse due to the side effect during `map()`.
.rev() let v: Vec<_> = repo_packages
.map(|mut rp| { .drain(..)
rp.packages.retain(|p| seen_pkgs.insert(p.into())); .rev()
rp .map(|mut rp| {
}) rp.packages.retain(|p| seen_pkgs.insert(p.into()));
.filter(|rp| !rp.packages.is_empty()) rp
.collect(); })
// can't inline this in the iterator chain above: .filter(|rp| !rp.packages.is_empty())
// https://doc.rust-lang.org/std/iter/struct.Map.html#notes-about-side-effects .collect();
v.reverse(); // Now replace the original, re-reversing.
v repo_packages.extend(v.into_iter().rev());
}); });
}
} }
} }