Extract Package::lib_target method
authorAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 2 May 2018 17:26:52 +0000 (20:26 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 5 May 2018 21:14:02 +0000 (00:14 +0300)
src/cargo/core/compiler/context/unit_dependencies.rs
src/cargo/core/package.rs
src/cargo/ops/cargo_compile.rs

index e53c3da374d9b538bb4658c41edc027a2583c354..ba742456a431d20c2e9178b6c7a14b64c11b3f62 100644 (file)
@@ -122,7 +122,7 @@ fn compute_deps<'a, 'b, 'cfg>(
             true
         })
     }).filter_map(|(id, _)| match bcx.get_package(id) {
-            Ok(pkg) => pkg.targets().iter().find(|t| t.is_lib()).map(|t| {
+            Ok(pkg) => pkg.lib_target().map(|t| {
                 let mode = check_or_build_mode(&unit.mode, t);
                 let unit = new_unit(bcx, pkg, t, profile_for, unit.kind.for_target(t), mode);
                 Ok((unit, profile_for))
@@ -222,7 +222,7 @@ fn compute_deps_doc<'a, 'cfg>(
     let mut ret = Vec::new();
     for dep in deps {
         let dep = dep?;
-        let lib = match dep.targets().iter().find(|t| t.is_lib()) {
+        let lib = match dep.lib_target() {
             Some(lib) => lib,
             None => continue,
         };
index 37ad259f10b625d042b1e4985d68f162c4e1a1cc..475130506c7bd6a682ce723828c19cd217e1a0c6 100644 (file)
@@ -129,6 +129,10 @@ impl Package {
     pub fn targets(&self) -> &[Target] {
         self.manifest.targets()
     }
+    /// Get the library target for the package
+    pub fn lib_target(&self) -> Option<&Target> {
+        self.targets().iter().find(|t| t.is_lib())
+    }
     /// Get the current package version
     pub fn version(&self) -> &Version {
         self.package_id().version()
index 36eac5b3b412a8c25080ecf5f95d13dc92454083..1d93885958fb1cdf988943bf504f84986a4f9e22 100644 (file)
@@ -546,8 +546,11 @@ fn generate_targets<'a>(
                 proposals.extend(default_units);
                 if build_config.mode == CompileMode::Test {
                     // Include the lib as it will be required for doctests.
-                    if let Some(t) = pkg.targets().iter().find(|t| t.is_lib() && t.doctested()) {
-                        proposals.push((new_unit(pkg, t, CompileMode::Build), false));
+                    match pkg.lib_target() {
+                        Some(t) if t.doctested() => {
+                            proposals.push((new_unit(pkg, t, CompileMode::Build), false));
+                        }
+                        _ => {}
                     }
                 }
             }
@@ -560,7 +563,7 @@ fn generate_targets<'a>(
                 ref benches,
             } => {
                 if lib {
-                    if let Some(target) = pkg.targets().iter().find(|t| t.is_lib()) {
+                    if let Some(target) = pkg.lib_target() {
                         proposals.push((new_unit(pkg, target, build_config.mode), false));
                     } else if !all_targets {
                         bail!("no library targets found")