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