Skip to content

Commit 1c03264

Browse files
committed
Auto merge of #6621 - dwijnand:clean-cargo_compile, r=ehuss
Extract & re-use filter_targets in cargo_compile Also drop part of the docs in `list_rule_targets`, as that info is now on `Proposal`.
2 parents d7426e5 + df9ca87 commit 1c03264

File tree

1 file changed

+43
-56
lines changed

1 file changed

+43
-56
lines changed

src/cargo/ops/cargo_compile.rs

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -619,24 +619,16 @@ fn generate_targets<'a>(
619619
} => {
620620
if lib {
621621
let mut libs = Vec::new();
622-
for pkg in packages {
623-
for target in pkg.targets().iter().filter(|t| t.is_lib()) {
624-
if build_config.mode == CompileMode::Doctest && !target.doctestable() {
625-
ws.config()
626-
.shell()
627-
.warn(format!(
628-
"doc tests are not supported for crate type(s) `{}` in package `{}`",
629-
target.rustc_crate_types().join(", "),
630-
pkg.name()
631-
))?;
632-
} else {
633-
libs.push(Proposal {
634-
pkg,
635-
target,
636-
requires_features: false,
637-
mode: build_config.mode,
638-
});
639-
}
622+
for proposal in filter_targets(packages, Target::is_lib, false, build_config.mode) {
623+
let Proposal { target, pkg, .. } = proposal;
624+
if build_config.mode == CompileMode::Doctest && !target.doctestable() {
625+
ws.config().shell().warn(format!(
626+
"doc tests are not supported for crate type(s) `{}` in package `{}`",
627+
target.rustc_crate_types().join(", "),
628+
pkg.name()
629+
))?;
630+
} else {
631+
libs.push(proposal)
640632
}
641633
}
642634
if !all_targets && libs.is_empty() {
@@ -655,7 +647,7 @@ fn generate_targets<'a>(
655647

656648
// If --tests was specified, add all targets that would be
657649
// generated by `cargo test`.
658-
let test_filter = match *tests {
650+
let test_filter = match tests {
659651
FilterRule::All => Target::tested,
660652
FilterRule::Just(_) => Target::is_test,
661653
};
@@ -666,7 +658,7 @@ fn generate_targets<'a>(
666658
};
667659
// If --benches was specified, add all targets that would be
668660
// generated by `cargo bench`.
669-
let bench_filter = match *benches {
661+
let bench_filter = match benches {
670662
FilterRule::All => Target::benched,
671663
FilterRule::Just(_) => Target::is_bench,
672664
};
@@ -795,36 +787,22 @@ fn filter_default_targets(targets: &[Target], mode: CompileMode) -> Vec<&Target>
795787
}
796788
}
797789

798-
/// Returns a list of targets based on command-line target selection flags.
799-
/// The return value is a list of `(Package, Target, bool, CompileMode)`
800-
/// tuples. The `bool` value indicates whether or not all required features
801-
/// *must* be present.
790+
/// Returns a list of proposed targets based on command-line target selection flags.
802791
fn list_rule_targets<'a>(
803792
packages: &[&'a Package],
804793
rule: &FilterRule,
805794
target_desc: &'static str,
806795
is_expected_kind: fn(&Target) -> bool,
807796
mode: CompileMode,
808797
) -> CargoResult<Vec<Proposal<'a>>> {
809-
let mut result = Vec::new();
810-
match *rule {
798+
let mut proposals = Vec::new();
799+
match rule {
811800
FilterRule::All => {
812-
for pkg in packages {
813-
for target in pkg.targets() {
814-
if is_expected_kind(target) {
815-
result.push(Proposal {
816-
pkg,
817-
target,
818-
requires_features: false,
819-
mode,
820-
});
821-
}
822-
}
823-
}
801+
proposals.extend(filter_targets(packages, is_expected_kind, false, mode))
824802
}
825-
FilterRule::Just(ref names) => {
803+
FilterRule::Just(names) => {
826804
for name in names {
827-
result.extend(find_named_targets(
805+
proposals.extend(find_named_targets(
828806
packages,
829807
name,
830808
target_desc,
@@ -834,7 +812,7 @@ fn list_rule_targets<'a>(
834812
}
835813
}
836814
}
837-
Ok(result)
815+
Ok(proposals)
838816
}
839817

840818
/// Find the targets for a specifically named target.
@@ -845,20 +823,9 @@ fn find_named_targets<'a>(
845823
is_expected_kind: fn(&Target) -> bool,
846824
mode: CompileMode,
847825
) -> CargoResult<Vec<Proposal<'a>>> {
848-
let mut result = Vec::new();
849-
for pkg in packages {
850-
for target in pkg.targets() {
851-
if target.name() == target_name && is_expected_kind(target) {
852-
result.push(Proposal {
853-
pkg,
854-
target,
855-
requires_features: true,
856-
mode,
857-
});
858-
}
859-
}
860-
}
861-
if result.is_empty() {
826+
let filter = |t: &Target| t.name() == target_name && is_expected_kind(t);
827+
let proposals = filter_targets(packages, filter, true, mode);
828+
if proposals.is_empty() {
862829
let suggestion = packages
863830
.iter()
864831
.flat_map(|pkg| {
@@ -880,5 +847,25 @@ fn find_named_targets<'a>(
880847
None => failure::bail!("no {} target named `{}`", target_desc, target_name),
881848
}
882849
}
883-
Ok(result)
850+
Ok(proposals)
851+
}
852+
853+
fn filter_targets<'a>(
854+
packages: &[&'a Package],
855+
predicate: impl Fn(&Target) -> bool,
856+
requires_features: bool,
857+
mode: CompileMode,
858+
) -> Vec<Proposal<'a>> {
859+
let mut proposals = Vec::new();
860+
for pkg in packages {
861+
for target in pkg.targets().iter().filter(|t| predicate(t)) {
862+
proposals.push(Proposal {
863+
pkg,
864+
target,
865+
requires_features,
866+
mode,
867+
});
868+
}
869+
}
870+
proposals
884871
}

0 commit comments

Comments
 (0)