Skip to content

Commit 5ced6f2

Browse files
committed
Simplify process access to pid
1 parent b6de3f7 commit 5ced6f2

File tree

1 file changed

+13
-27
lines changed

1 file changed

+13
-27
lines changed

src/currentprocess.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ use varsource::*;
6464
/// methods are in performance critical loops (except perhaps progress bars -
6565
/// and even there we should be doing debouncing and managing update rates).
6666
#[enum_dispatch]
67-
pub trait CurrentProcess: home::Env + CurrentDirSource + VarSource + ProcessSource + Debug {}
67+
pub trait CurrentProcess: home::Env + CurrentDirSource + VarSource + Debug {}
6868

6969
/// Allows concrete types for the currentprocess abstraction.
7070
#[derive(Clone, Debug)]
71-
#[enum_dispatch(CurrentProcess, CurrentDirSource, VarSource, ProcessSource)]
71+
#[enum_dispatch(CurrentProcess, CurrentDirSource, VarSource)]
7272
pub enum Process {
7373
OSProcess(OSProcess),
7474
#[cfg(feature = "test")]
@@ -128,6 +128,15 @@ impl Process {
128128
Process::TestProcess(p) => Box::new(filesource::TestWriter(p.stderr.clone())),
129129
}
130130
}
131+
132+
#[cfg(test)]
133+
fn id(&self) -> u64 {
134+
match self {
135+
Process::OSProcess(_) => std::process::id() as u64,
136+
#[cfg(feature = "test")]
137+
Process::TestProcess(p) => p.id,
138+
}
139+
}
131140
}
132141

133142
/// Obtain the current instance of CurrentProcess
@@ -247,16 +256,6 @@ thread_local! {
247256
pub(crate) static PROCESS: RefCell<Option<Process>> = const { RefCell::new(None) };
248257
}
249258

250-
// PID related things
251-
#[enum_dispatch]
252-
pub trait ProcessSource {
253-
/// Returns a unique id for the process.
254-
///
255-
/// Real process ids are <= u32::MAX.
256-
/// Test process ids are > u32::MAX
257-
fn id(&self) -> u64;
258-
}
259-
260259
// ----------- real process -----------------
261260

262261
#[derive(Clone, Debug)]
@@ -280,12 +279,6 @@ impl Default for OSProcess {
280279
}
281280
}
282281

283-
impl ProcessSource for OSProcess {
284-
fn id(&self) -> u64 {
285-
std::process::id() as u64
286-
}
287-
}
288-
289282
// ------------ test process ----------------
290283
#[cfg(feature = "test")]
291284
#[derive(Clone, Debug, Default)]
@@ -342,21 +335,14 @@ impl TestProcess {
342335
}
343336
}
344337

345-
#[cfg(feature = "test")]
346-
impl ProcessSource for TestProcess {
347-
fn id(&self) -> u64 {
348-
self.id
349-
}
350-
}
351-
352338
#[cfg(test)]
353339
mod tests {
354340
use std::collections::HashMap;
355341
use std::env;
356342

357343
use rustup_macros::unit_test as test;
358344

359-
use super::{process, with, ProcessSource, TestProcess};
345+
use super::{process, with, TestProcess};
360346

361347
#[test]
362348
fn test_instance() {
@@ -367,7 +353,7 @@ mod tests {
367353
"",
368354
);
369355
with(proc.clone().into(), || {
370-
assert_eq!(proc.id(), process().id(), "{:?} != {:?}", proc, process())
356+
assert_eq!(proc.id, process().id(), "{:?} != {:?}", proc, process())
371357
});
372358
}
373359
}

0 commit comments

Comments
 (0)