Skip to content

Commit 2be6002

Browse files
committed
Simplify process access to pid
1 parent 9ebfcce commit 2be6002

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
@@ -65,11 +65,11 @@ use crate::utils::tty::{stderr_isatty, stdout_isatty};
6565
/// methods are in performance critical loops (except perhaps progress bars -
6666
/// and even there we should be doing debouncing and managing update rates).
6767
#[enum_dispatch]
68-
pub trait CurrentProcess: home::Env + CurrentDirSource + VarSource + ProcessSource + Debug {}
68+
pub trait CurrentProcess: home::Env + CurrentDirSource + VarSource + Debug {}
6969

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

134143
/// Obtain the current instance of CurrentProcess
@@ -182,16 +191,6 @@ thread_local! {
182191
pub(crate) static PROCESS: RefCell<Option<Process>> = const { RefCell::new(None) };
183192
}
184193

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

197196
#[derive(Clone, Debug)]
@@ -215,12 +214,6 @@ impl Default for OSProcess {
215214
}
216215
}
217216

218-
impl ProcessSource for OSProcess {
219-
fn id(&self) -> u64 {
220-
std::process::id() as u64
221-
}
222-
}
223-
224217
// ------------ test process ----------------
225218
#[cfg(feature = "test")]
226219
#[derive(Clone, Debug, Default)]
@@ -276,21 +269,14 @@ impl TestProcess {
276269
}
277270
}
278271

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

291277
use rustup_macros::unit_test as test;
292278

293-
use super::{process, with, ProcessSource, TestProcess};
279+
use super::{process, with, TestProcess};
294280

295281
#[test]
296282
fn test_instance() {
@@ -301,7 +287,7 @@ mod tests {
301287
"",
302288
);
303289
with(proc.clone().into(), || {
304-
assert_eq!(proc.id(), process().id(), "{:?} != {:?}", proc, process())
290+
assert_eq!(proc.id, process().id(), "{:?} != {:?}", proc, process())
305291
});
306292
}
307293
}

0 commit comments

Comments
 (0)