From f7c6d04b05cc5df192b5e70b7d65e709902ad1db Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Wed, 15 Jan 2020 17:22:25 -0500 Subject: [PATCH] Introduce newtype wrapping JobId --- src/cargo/core/compiler/job_queue.rs | 31 ++++++++++++++++++---------- src/cargo/core/compiler/timings.rs | 9 ++++---- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/cargo/core/compiler/job_queue.rs b/src/cargo/core/compiler/job_queue.rs index 9c8128241..bd9a049e9 100644 --- a/src/cargo/core/compiler/job_queue.rs +++ b/src/cargo/core/compiler/job_queue.rs @@ -87,7 +87,7 @@ pub struct JobQueue<'a, 'cfg> { queue: DependencyQueue, Artifact, Job>, tx: Sender, rx: Receiver, - active: HashMap>, + active: HashMap>, compiled: HashSet, documented: HashSet, counts: HashMap, @@ -96,13 +96,22 @@ pub struct JobQueue<'a, 'cfg> { timings: Timings<'a, 'cfg>, } +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub struct JobId(pub u32); + +impl std::fmt::Display for JobId { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + pub struct JobState<'a> { /// Channel back to the main thread to coordinate messages and such. tx: Sender, /// The job id that this state is associated with, used when sending /// messages back to the main thread. - id: u32, + id: JobId, /// Whether or not we're expected to have a call to `rmeta_produced`. Once /// that method is called this is dynamically set to `false` to prevent @@ -135,19 +144,19 @@ enum Artifact { } enum Message { - Run(u32, String), + Run(JobId, String), BuildPlanMsg(String, ProcessBuilder, Arc>), Stdout(String), Stderr(String), FixDiagnostic(diagnostic_server::Message), Token(io::Result), - Finish(u32, Artifact, CargoResult<()>), + Finish(JobId, Artifact, CargoResult<()>), // This client should get release_raw called on it with one of our tokens - NeedsToken(u32, Client), + NeedsToken(JobId, Client), // A token previously passed to a NeedsToken client is being released. - ReleaseToken(u32), + ReleaseToken(JobId), } impl<'a> JobState<'a> { @@ -343,7 +352,7 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> { ) -> CargoResult<()> { let mut tokens = Vec::new(); let mut rustc_tokens = Vec::new(); - let mut to_send_clients: Vec<(u32, Client)> = Vec::new(); + let mut to_send_clients: Vec<(JobId, Client)> = Vec::new(); let mut queue = Vec::new(); let mut print = DiagnosticPrinter::new(cx.bcx.config); trace!("queue: {:#?}", self.queue); @@ -626,8 +635,8 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> { cx: &Context<'a, '_>, scope: &Scope<'a>, ) -> CargoResult<()> { - let id = self.next_id; - self.next_id = id.checked_add(1).unwrap(); + let id = JobId(self.next_id); + self.next_id = self.next_id.checked_add(1).unwrap(); info!("start {}: {:?}", id, unit); @@ -676,7 +685,7 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> { // to make sure nothing hangs by accident. struct FinishOnDrop<'a> { tx: &'a Sender, - id: u32, + id: JobId, result: CargoResult<()>, } @@ -737,7 +746,7 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> { fn finish( &mut self, - id: u32, + id: JobId, unit: &Unit<'a>, artifact: Artifact, cx: &mut Context<'a, '_>, diff --git a/src/cargo/core/compiler/timings.rs b/src/cargo/core/compiler/timings.rs index 4cd151b54..ff24e77b1 100644 --- a/src/cargo/core/compiler/timings.rs +++ b/src/cargo/core/compiler/timings.rs @@ -3,6 +3,7 @@ //! This module implements some simple tracking information for timing of how //! long it takes for different units to compile. use super::{CompileMode, Unit}; +use crate::core::compiler::job_queue::JobId; use crate::core::compiler::BuildContext; use crate::core::PackageId; use crate::util::cpu::State; @@ -41,7 +42,7 @@ pub struct Timings<'a, 'cfg> { unit_times: Vec>, /// Units that are in the process of being built. /// When they finished, they are moved to `unit_times`. - active: HashMap>, + active: HashMap>, /// Concurrency-tracking information. This is periodically updated while /// compilation progresses. concurrency: Vec, @@ -144,7 +145,7 @@ impl<'a, 'cfg> Timings<'a, 'cfg> { } /// Mark that a unit has started running. - pub fn unit_start(&mut self, id: u32, unit: Unit<'a>) { + pub fn unit_start(&mut self, id: JobId, unit: Unit<'a>) { if !self.enabled { return; } @@ -178,7 +179,7 @@ impl<'a, 'cfg> Timings<'a, 'cfg> { } /// Mark that the `.rmeta` file as generated. - pub fn unit_rmeta_finished(&mut self, id: u32, unlocked: Vec<&Unit<'a>>) { + pub fn unit_rmeta_finished(&mut self, id: JobId, unlocked: Vec<&Unit<'a>>) { if !self.enabled { return; } @@ -196,7 +197,7 @@ impl<'a, 'cfg> Timings<'a, 'cfg> { } /// Mark that a unit has finished running. - pub fn unit_finished(&mut self, id: u32, unlocked: Vec<&Unit<'a>>) { + pub fn unit_finished(&mut self, id: JobId, unlocked: Vec<&Unit<'a>>) { if !self.enabled { return; }