Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a mean to mutably access the members of a workspace #9547

Merged
merged 2 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cargo/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
94 changes: 56 additions & 38 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -136,13 +135,6 @@ pub struct WorkspaceRootConfig {
custom_metadata: Option<toml::Value>,
}

/// 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`.
Expand Down Expand Up @@ -466,19 +458,65 @@ 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<Item = &Package> {
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
pub fn members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this use package.get_mut like above uses packages.get?

(same for default_members_mut below)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, the get_mut lifetime gets (rightfully) in the way.

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 {
ws: self,
iter: self.default_members.iter(),
}
pub fn default_members<'a>(&'a self) -> impl Iterator<Item = &Package> {
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
pub fn default_members_mut(&mut self) -> impl Iterator<Item = &mut Package> {
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.
Expand Down Expand Up @@ -1529,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<usize>) {
let (_, upper) = self.iter.size_hint();
(0, upper)
}
}

impl MaybePackage {
fn workspace_config(&self) -> &WorkspaceConfig {
match *self {
Expand Down