Skip to content

Commit 42f7462

Browse files
committed
Extract Package::lib_target method
1 parent 2912a58 commit 42f7462

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/cargo/core/compiler/context/unit_dependencies.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fn compute_deps<'a, 'b, 'cfg>(
122122
true
123123
})
124124
}).filter_map(|(id, _)| match bcx.get_package(id) {
125-
Ok(pkg) => pkg.targets().iter().find(|t| t.is_lib()).map(|t| {
125+
Ok(pkg) => pkg.lib_target().map(|t| {
126126
let mode = check_or_build_mode(&unit.mode, t);
127127
let unit = new_unit(bcx, pkg, t, profile_for, unit.kind.for_target(t), mode);
128128
Ok((unit, profile_for))
@@ -222,7 +222,7 @@ fn compute_deps_doc<'a, 'cfg>(
222222
let mut ret = Vec::new();
223223
for dep in deps {
224224
let dep = dep?;
225-
let lib = match dep.targets().iter().find(|t| t.is_lib()) {
225+
let lib = match dep.lib_target() {
226226
Some(lib) => lib,
227227
None => continue,
228228
};

src/cargo/core/package.rs

+4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ impl Package {
129129
pub fn targets(&self) -> &[Target] {
130130
self.manifest.targets()
131131
}
132+
/// Get the library target for the package
133+
pub fn lib_target(&self) -> Option<&Target> {
134+
self.targets().iter().find(|t| t.is_lib())
135+
}
132136
/// Get the current package version
133137
pub fn version(&self) -> &Version {
134138
self.package_id().version()

src/cargo/ops/cargo_compile.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,11 @@ fn generate_targets<'a>(
546546
proposals.extend(default_units);
547547
if build_config.mode == CompileMode::Test {
548548
// Include the lib as it will be required for doctests.
549-
if let Some(t) = pkg.targets().iter().find(|t| t.is_lib() && t.doctested()) {
550-
proposals.push((new_unit(pkg, t, CompileMode::Build), false));
549+
match pkg.lib_target() {
550+
Some(t) if t.doctested() => {
551+
proposals.push((new_unit(pkg, t, CompileMode::Build), false));
552+
}
553+
_ => {}
551554
}
552555
}
553556
}
@@ -560,7 +563,7 @@ fn generate_targets<'a>(
560563
ref benches,
561564
} => {
562565
if lib {
563-
if let Some(target) = pkg.targets().iter().find(|t| t.is_lib()) {
566+
if let Some(target) = pkg.lib_target() {
564567
proposals.push((new_unit(pkg, target, build_config.mode), false));
565568
} else if !all_targets {
566569
bail!("no library targets found")

0 commit comments

Comments
 (0)