From 6fe08fa833e4a8b0a035bd489cfc60e299dbffff Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Tue, 1 Jun 2021 16:45:10 +0200 Subject: [PATCH] Add a mean to mutably access the members of a workspace It is used by cargo-c to patch all the lib crates in a workspace. --- src/cargo/core/workspace.rs | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index dcaafd476..904c5e1bc 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -473,6 +473,26 @@ impl<'cfg> Workspace<'cfg> { } } + /// Returns a mutable iterator over all packages in this workspace + pub fn members_mut(&mut self) -> impl Iterator { + let packages = &mut self.packages.packages; + let members: HashSet<_> = self + .members + .iter() + .map(|path| path.parent().unwrap().to_owned()) + .collect(); + + packages.iter_mut().filter_map(move |(path, package)| { + if members.contains(path) { + if let MaybePackage::Package(ref mut p) = package { + return Some(p); + } + } + + None + }) + } + /// Returns an iterator over default packages in this workspace pub fn default_members<'a>(&'a self) -> Members<'a, 'cfg> { Members { @@ -481,6 +501,26 @@ impl<'cfg> Workspace<'cfg> { } } + /// Returns an iterator over default packages in this workspace + pub fn default_members_mut(&mut self) -> impl Iterator { + let packages = &mut self.packages.packages; + let members: HashSet<_> = self + .default_members + .iter() + .map(|path| path.parent().unwrap().to_owned()) + .collect(); + + packages.iter_mut().filter_map(move |(path, package)| { + if members.contains(path) { + if let MaybePackage::Package(ref mut p) = package { + return Some(p); + } + } + + None + }) + } + /// Returns true if the package is a member of the workspace. pub fn is_member(&self, pkg: &Package) -> bool { self.member_ids.contains(&pkg.package_id())