@@ -14,7 +14,6 @@ use std::{
14
14
sync:: { Arc , Mutex } ,
15
15
} ;
16
16
17
- use enum_dispatch:: enum_dispatch;
18
17
#[ cfg( feature = "test" ) ]
19
18
use rand:: { thread_rng, Rng } ;
20
19
@@ -23,53 +22,19 @@ pub mod terminalsource;
23
22
24
23
use crate :: utils:: tty:: { stderr_isatty, stdout_isatty} ;
25
24
26
- /// An abstraction for the current process.
27
- ///
28
- /// This acts as a clonable proxy to the global state provided by some key OS
29
- /// interfaces - it is a zero cost abstraction. For the test variant it manages
30
- /// a mutex and takes out locks to ensure consistency.
31
- ///
32
- /// This provides replacements env::arg*, env::var*, and the standard files
33
- /// io::std* with traits that are customisable for tests. As a result any macros
34
- /// or code that have non-pluggable usage of those are incompatible with
35
- /// CurrentProcess and must not be used. That includes \[e\]println! as well as
36
- /// third party crates.
37
- ///
38
- /// CurrentProcess is used via an instance in a thread local variable; when
39
- /// making new threads, be sure to copy CurrentProcess::process() into the new
40
- /// thread before calling any code that may need to use a CurrentProcess
41
- /// function.
42
- ///
43
- /// Run some code using with: this will set the current instance, call your
44
- /// function, then finally reset the instance at the end before returning.
45
- ///
46
- /// Testing level interoperation with external code that depends on environment
47
- /// variables could be possible with a hypothetical `with_projected()` which
48
- /// would be a zero-cost operation in real processes, but in test processes will
49
- /// take a lock out to mutually exclude other code, then overwrite the current
50
- /// value of std::env::vars, restoring it at the end. However, the only use for
51
- /// that today is a test of cargo::home, which is now implemented in a separate
52
- /// crate, so we've just deleted the test.
53
- ///
54
- /// A thread local is used to permit the instance to be available to the entire
55
- /// rustup library without needing to explicitly wire this normally global state
56
- /// everywhere; and a trait object with dyn dispatch is likewise used to avoid
57
- /// needing to thread trait parameters across the entire code base: none of the
58
- /// methods are in performance critical loops (except perhaps progress bars -
59
- /// and even there we should be doing debouncing and managing update rates).
60
- #[ enum_dispatch]
61
- pub trait CurrentProcess : Debug { }
62
-
63
25
/// Allows concrete types for the currentprocess abstraction.
64
26
#[ derive( Clone , Debug ) ]
65
- #[ enum_dispatch( CurrentProcess ) ]
66
27
pub enum Process {
67
28
OSProcess ( OSProcess ) ,
68
29
#[ cfg( feature = "test" ) ]
69
30
TestProcess ( TestProcess ) ,
70
31
}
71
32
72
33
impl Process {
34
+ pub fn os ( ) -> Self {
35
+ Self :: OSProcess ( OSProcess :: new ( ) )
36
+ }
37
+
73
38
pub fn name ( & self ) -> Option < String > {
74
39
let arg0 = match self . var ( "RUSTUP_FORCE_ARG0" ) {
75
40
Ok ( v) => Some ( v) ,
@@ -186,6 +151,13 @@ impl home::env::Env for Process {
186
151
}
187
152
}
188
153
154
+ #[ cfg( feature = "test" ) ]
155
+ impl From < TestProcess > for Process {
156
+ fn from ( p : TestProcess ) -> Self {
157
+ Self :: TestProcess ( p)
158
+ }
159
+ }
160
+
189
161
/// Obtain the current instance of CurrentProcess
190
162
pub fn process ( ) -> Process {
191
163
home_process ( )
0 commit comments