From 6fe08fa833e4a8b0a035bd489cfc60e299dbffff Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Tue, 1 Jun 2021 16:45:10 +0200 Subject: [PATCH 1/2] 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 dcaafd476eb..904c5e1bc8e 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()) From b26ceda7b0cf85987f366eac1d5fcd76349f96ca Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Mon, 7 Jun 2021 20:52:13 +0200 Subject: [PATCH 2/2] Simplify Workspace::members()/default_members() --- src/cargo/core/mod.rs | 2 +- src/cargo/core/workspace.rs | 54 +++++++++++-------------------------- 2 files changed, 17 insertions(+), 39 deletions(-) diff --git a/src/cargo/core/mod.rs b/src/cargo/core/mod.rs index 43bbbf1031c..aec49b143bd 100644 --- a/src/cargo/core/mod.rs +++ b/src/cargo/core/mod.rs @@ -10,7 +10,7 @@ pub use self::resolver::{Resolve, ResolveVersion}; pub use self::shell::{Shell, Verbosity}; pub use self::source::{GitReference, Source, SourceId, SourceMap}; pub use self::summary::{FeatureMap, FeatureValue, Summary}; -pub use self::workspace::{MaybePackage, Members, Workspace, WorkspaceConfig, WorkspaceRootConfig}; +pub use self::workspace::{MaybePackage, Workspace, WorkspaceConfig, WorkspaceRootConfig}; pub mod compiler; pub mod dependency; diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 904c5e1bc8e..a748e0890c5 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -3,7 +3,6 @@ use std::collections::hash_map::{Entry, HashMap}; use std::collections::{BTreeMap, BTreeSet, HashSet}; use std::path::{Path, PathBuf}; use std::rc::Rc; -use std::slice; use anyhow::{bail, Context as _}; use glob::glob; @@ -136,13 +135,6 @@ pub struct WorkspaceRootConfig { custom_metadata: Option, } -/// An iterator over the member packages of a workspace, returned by -/// `Workspace::members` -pub struct Members<'a, 'cfg> { - ws: &'a Workspace<'cfg>, - iter: slice::Iter<'a, PathBuf>, -} - impl<'cfg> Workspace<'cfg> { /// Creates a new workspace given the target manifest pointed to by /// `manifest_path`. @@ -466,11 +458,14 @@ impl<'cfg> Workspace<'cfg> { } /// Returns an iterator over all packages in this workspace - pub fn members<'a>(&'a self) -> Members<'a, 'cfg> { - Members { - ws: self, - iter: self.members.iter(), - } + pub fn members(&self) -> impl Iterator { + let packages = &self.packages; + self.members + .iter() + .filter_map(move |path| match packages.get(path) { + &MaybePackage::Package(ref p) => Some(p), + _ => None, + }) } /// Returns a mutable iterator over all packages in this workspace @@ -494,11 +489,14 @@ impl<'cfg> Workspace<'cfg> { } /// Returns an iterator over default packages in this workspace - pub fn default_members<'a>(&'a self) -> Members<'a, 'cfg> { - Members { - ws: self, - iter: self.default_members.iter(), - } + pub fn default_members<'a>(&'a self) -> impl Iterator { + let packages = &self.packages; + self.default_members + .iter() + .filter_map(move |path| match packages.get(path) { + &MaybePackage::Package(ref p) => Some(p), + _ => None, + }) } /// Returns an iterator over default packages in this workspace @@ -1569,26 +1567,6 @@ impl<'cfg> Packages<'cfg> { } } -impl<'a, 'cfg> Iterator for Members<'a, 'cfg> { - type Item = &'a Package; - - fn next(&mut self) -> Option<&'a Package> { - loop { - let next = self.iter.next().map(|path| self.ws.packages.get(path)); - match next { - Some(&MaybePackage::Package(ref p)) => return Some(p), - Some(&MaybePackage::Virtual(_)) => {} - None => return None, - } - } - } - - fn size_hint(&self) -> (usize, Option) { - let (_, upper) = self.iter.size_hint(); - (0, upper) - } -} - impl MaybePackage { fn workspace_config(&self) -> &WorkspaceConfig { match *self {