@@ -15,60 +15,25 @@ use std::{
1515 sync:: { Arc , Mutex } ,
1616} ;
1717
18- use enum_dispatch:: enum_dispatch;
1918#[ cfg( feature = "test" ) ]
2019use rand:: { thread_rng, Rng } ;
2120
2221pub mod filesource;
2322pub mod terminalsource;
2423
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-
6224/// Allows concrete types for the currentprocess abstraction.
6325#[ derive( Clone , Debug ) ]
64- #[ enum_dispatch( CurrentProcess ) ]
6526pub enum Process {
6627 OSProcess ( OSProcess ) ,
6728 #[ cfg( feature = "test" ) ]
6829 TestProcess ( TestProcess ) ,
6930}
7031
7132impl Process {
33+ pub fn os ( ) -> Self {
34+ Self :: OSProcess ( OSProcess :: new ( ) )
35+ }
36+
7237 pub fn name ( & self ) -> Option < String > {
7338 let arg0 = match self . var ( "RUSTUP_FORCE_ARG0" ) {
7439 Ok ( v) => Some ( v) ,
@@ -185,6 +150,13 @@ impl home::env::Env for Process {
185150 }
186151}
187152
153+ #[ cfg( feature = "test" ) ]
154+ impl From < TestProcess > for Process {
155+ fn from ( p : TestProcess ) -> Self {
156+ Self :: TestProcess ( p)
157+ }
158+ }
159+
188160/// Obtain the current instance of CurrentProcess
189161pub fn process ( ) -> Process {
190162 home_process ( )
0 commit comments