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