Skip to content

Commit e61c237

Browse files
committed
Simplify process access to pid
1 parent cd60454 commit e61c237

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
@@ -63,11 +63,11 @@ use varsource::*;
6363
/// methods are in performance critical loops (except perhaps progress bars -
6464
/// and even there we should be doing debouncing and managing update rates).
6565
#[enum_dispatch]
66-
pub trait CurrentProcess: home::Env + CurrentDirSource + VarSource + ProcessSource + Debug {}
66+
pub trait CurrentProcess: home::Env + CurrentDirSource + VarSource + Debug {}
6767

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

132141
/// Obtain the current instance of CurrentProcess
@@ -180,16 +189,6 @@ thread_local! {
180189
pub(crate) static PROCESS: RefCell<Option<Process>> = const { RefCell::new(None) };
181190
}
182191

183-
// PID related things
184-
#[enum_dispatch]
185-
pub trait ProcessSource {
186-
/// Returns a unique id for the process.
187-
///
188-
/// Real process ids are <= u32::MAX.
189-
/// Test process ids are > u32::MAX
190-
fn id(&self) -> u64;
191-
}
192-
193192
// ----------- real process -----------------
194193

195194
#[derive(Clone, Debug)]
@@ -213,12 +212,6 @@ impl Default for OSProcess {
213212
}
214213
}
215214

216-
impl ProcessSource for OSProcess {
217-
fn id(&self) -> u64 {
218-
std::process::id() as u64
219-
}
220-
}
221-
222215
// ------------ test process ----------------
223216
#[cfg(feature = "test")]
224217
#[derive(Clone, Debug, Default)]
@@ -274,21 +267,14 @@ impl TestProcess {
274267
}
275268
}
276269

277-
#[cfg(feature = "test")]
278-
impl ProcessSource for TestProcess {
279-
fn id(&self) -> u64 {
280-
self.id
281-
}
282-
}
283-
284270
#[cfg(test)]
285271
mod tests {
286272
use std::collections::HashMap;
287273
use std::env;
288274

289275
use rustup_macros::unit_test as test;
290276

291-
use super::{process, with, ProcessSource, TestProcess};
277+
use super::{process, with, TestProcess};
292278

293279
#[test]
294280
fn test_instance() {
@@ -299,7 +285,7 @@ mod tests {
299285
"",
300286
);
301287
with(proc.clone().into(), || {
302-
assert_eq!(proc.id(), process().id(), "{:?} != {:?}", proc, process())
288+
assert_eq!(proc.id, process().id(), "{:?} != {:?}", proc, process())
303289
});
304290
}
305291
}

0 commit comments

Comments
 (0)