5151
5252use crate :: util:: cache_lock:: { CacheLock , CacheLockMode , CacheLocker } ;
5353use std:: borrow:: Cow ;
54+ use std:: cell:: OnceCell ;
5455use std:: cell:: { RefCell , RefMut } ;
5556use std:: collections:: hash_map:: Entry :: { Occupied , Vacant } ;
5657use std:: collections:: { HashMap , HashSet } ;
@@ -74,6 +75,7 @@ use crate::core::{CliUnstable, Shell, SourceId, Workspace, WorkspaceRootConfig,
7475use crate :: ops:: RegistryCredentialConfig ;
7576use crate :: sources:: CRATES_IO_INDEX ;
7677use crate :: sources:: CRATES_IO_REGISTRY ;
78+ use crate :: util:: OnceExt as _;
7779use crate :: util:: errors:: CargoResult ;
7880use crate :: util:: network:: http:: configure_http_handle;
7981use crate :: util:: network:: http:: http_handle;
@@ -85,7 +87,6 @@ use cargo_util::paths;
8587use cargo_util_schemas:: manifest:: RegistryName ;
8688use curl:: easy:: Easy ;
8789use itertools:: Itertools ;
88- use lazycell:: LazyCell ;
8990use serde:: Deserialize ;
9091use serde:: de:: IntoDeserializer as _;
9192use serde_untagged:: UntaggedEnumVisitor ;
@@ -168,19 +169,19 @@ pub struct GlobalContext {
168169 /// Information about how to write messages to the shell
169170 shell : RefCell < Shell > ,
170171 /// A collection of configuration options
171- values : LazyCell < HashMap < String , ConfigValue > > ,
172+ values : OnceCell < HashMap < String , ConfigValue > > ,
172173 /// A collection of configuration options from the credentials file
173- credential_values : LazyCell < HashMap < String , ConfigValue > > ,
174+ credential_values : OnceCell < HashMap < String , ConfigValue > > ,
174175 /// CLI config values, passed in via `configure`.
175176 cli_config : Option < Vec < String > > ,
176177 /// The current working directory of cargo
177178 cwd : PathBuf ,
178179 /// Directory where config file searching should stop (inclusive).
179180 search_stop_path : Option < PathBuf > ,
180181 /// The location of the cargo executable (path to current process)
181- cargo_exe : LazyCell < PathBuf > ,
182+ cargo_exe : OnceCell < PathBuf > ,
182183 /// The location of the rustdoc executable
183- rustdoc : LazyCell < PathBuf > ,
184+ rustdoc : OnceCell < PathBuf > ,
184185 /// Whether we are printing extra verbose messages
185186 extra_verbose : bool ,
186187 /// `frozen` is the same as `locked`, but additionally will not access the
@@ -199,9 +200,9 @@ pub struct GlobalContext {
199200 /// Cli flags of the form "-Z something"
200201 unstable_flags_cli : Option < Vec < String > > ,
201202 /// A handle on curl easy mode for http calls
202- easy : LazyCell < RefCell < Easy > > ,
203+ easy : OnceCell < RefCell < Easy > > ,
203204 /// Cache of the `SourceId` for crates.io
204- crates_io_source_id : LazyCell < SourceId > ,
205+ crates_io_source_id : OnceCell < SourceId > ,
205206 /// If false, don't cache `rustc --version --verbose` invocations
206207 cache_rustc_info : bool ,
207208 /// Creation time of this config, used to output the total build time
@@ -211,23 +212,23 @@ pub struct GlobalContext {
211212 /// Environment variable snapshot.
212213 env : Env ,
213214 /// Tracks which sources have been updated to avoid multiple updates.
214- updated_sources : LazyCell < RefCell < HashSet < SourceId > > > ,
215+ updated_sources : OnceCell < RefCell < HashSet < SourceId > > > ,
215216 /// Cache of credentials from configuration or credential providers.
216217 /// Maps from url to credential value.
217- credential_cache : LazyCell < RefCell < HashMap < CanonicalUrl , CredentialCacheValue > > > ,
218+ credential_cache : OnceCell < RefCell < HashMap < CanonicalUrl , CredentialCacheValue > > > ,
218219 /// Cache of registry config from the `[registries]` table.
219- registry_config : LazyCell < RefCell < HashMap < SourceId , Option < RegistryConfig > > > > ,
220+ registry_config : OnceCell < RefCell < HashMap < SourceId , Option < RegistryConfig > > > > ,
220221 /// Locks on the package and index caches.
221222 package_cache_lock : CacheLocker ,
222223 /// Cached configuration parsed by Cargo
223- http_config : LazyCell < CargoHttpConfig > ,
224- future_incompat_config : LazyCell < CargoFutureIncompatConfig > ,
225- net_config : LazyCell < CargoNetConfig > ,
226- build_config : LazyCell < CargoBuildConfig > ,
227- target_cfgs : LazyCell < Vec < ( String , TargetCfgConfig ) > > ,
228- doc_extern_map : LazyCell < RustdocExternMap > ,
224+ http_config : OnceCell < CargoHttpConfig > ,
225+ future_incompat_config : OnceCell < CargoFutureIncompatConfig > ,
226+ net_config : OnceCell < CargoNetConfig > ,
227+ build_config : OnceCell < CargoBuildConfig > ,
228+ target_cfgs : OnceCell < Vec < ( String , TargetCfgConfig ) > > ,
229+ doc_extern_map : OnceCell < RustdocExternMap > ,
229230 progress_config : ProgressConfig ,
230- env_config : LazyCell < Arc < HashMap < String , OsString > > > ,
231+ env_config : OnceCell < Arc < HashMap < String , OsString > > > ,
231232 /// This should be false if:
232233 /// - this is an artifact of the rustc distribution process for "stable" or for "beta"
233234 /// - this is an `#[test]` that does not opt in with `enable_nightly_features`
@@ -247,10 +248,10 @@ pub struct GlobalContext {
247248 /// `WorkspaceRootConfigs` that have been found
248249 pub ws_roots : RefCell < HashMap < PathBuf , WorkspaceRootConfig > > ,
249250 /// The global cache tracker is a database used to track disk cache usage.
250- global_cache_tracker : LazyCell < RefCell < GlobalCacheTracker > > ,
251+ global_cache_tracker : OnceCell < RefCell < GlobalCacheTracker > > ,
251252 /// A cache of modifications to make to [`GlobalContext::global_cache_tracker`],
252253 /// saved to disk in a batch to improve performance.
253- deferred_global_last_use : LazyCell < RefCell < DeferredGlobalLastUse > > ,
254+ deferred_global_last_use : OnceCell < RefCell < DeferredGlobalLastUse > > ,
254255}
255256
256257impl GlobalContext {
@@ -286,11 +287,11 @@ impl GlobalContext {
286287 shell : RefCell :: new ( shell) ,
287288 cwd,
288289 search_stop_path : None ,
289- values : LazyCell :: new ( ) ,
290- credential_values : LazyCell :: new ( ) ,
290+ values : OnceCell :: new ( ) ,
291+ credential_values : OnceCell :: new ( ) ,
291292 cli_config : None ,
292- cargo_exe : LazyCell :: new ( ) ,
293- rustdoc : LazyCell :: new ( ) ,
293+ cargo_exe : OnceCell :: new ( ) ,
294+ rustdoc : OnceCell :: new ( ) ,
294295 extra_verbose : false ,
295296 frozen : false ,
296297 locked : false ,
@@ -304,28 +305,28 @@ impl GlobalContext {
304305 } ,
305306 unstable_flags : CliUnstable :: default ( ) ,
306307 unstable_flags_cli : None ,
307- easy : LazyCell :: new ( ) ,
308- crates_io_source_id : LazyCell :: new ( ) ,
308+ easy : OnceCell :: new ( ) ,
309+ crates_io_source_id : OnceCell :: new ( ) ,
309310 cache_rustc_info,
310311 creation_time : Instant :: now ( ) ,
311312 target_dir : None ,
312313 env,
313- updated_sources : LazyCell :: new ( ) ,
314- credential_cache : LazyCell :: new ( ) ,
315- registry_config : LazyCell :: new ( ) ,
314+ updated_sources : OnceCell :: new ( ) ,
315+ credential_cache : OnceCell :: new ( ) ,
316+ registry_config : OnceCell :: new ( ) ,
316317 package_cache_lock : CacheLocker :: new ( ) ,
317- http_config : LazyCell :: new ( ) ,
318- future_incompat_config : LazyCell :: new ( ) ,
319- net_config : LazyCell :: new ( ) ,
320- build_config : LazyCell :: new ( ) ,
321- target_cfgs : LazyCell :: new ( ) ,
322- doc_extern_map : LazyCell :: new ( ) ,
318+ http_config : OnceCell :: new ( ) ,
319+ future_incompat_config : OnceCell :: new ( ) ,
320+ net_config : OnceCell :: new ( ) ,
321+ build_config : OnceCell :: new ( ) ,
322+ target_cfgs : OnceCell :: new ( ) ,
323+ doc_extern_map : OnceCell :: new ( ) ,
323324 progress_config : ProgressConfig :: default ( ) ,
324- env_config : LazyCell :: new ( ) ,
325+ env_config : OnceCell :: new ( ) ,
325326 nightly_features_allowed : matches ! ( & * features:: channel( ) , "nightly" | "dev" ) ,
326327 ws_roots : RefCell :: new ( HashMap :: new ( ) ) ,
327- global_cache_tracker : LazyCell :: new ( ) ,
328- deferred_global_last_use : LazyCell :: new ( ) ,
328+ global_cache_tracker : OnceCell :: new ( ) ,
329+ deferred_global_last_use : OnceCell :: new ( ) ,
329330 }
330331 }
331332
@@ -526,21 +527,21 @@ impl GlobalContext {
526527 /// Which package sources have been updated, used to ensure it is only done once.
527528 pub fn updated_sources ( & self ) -> RefMut < ' _ , HashSet < SourceId > > {
528529 self . updated_sources
529- . borrow_with ( || RefCell :: new ( HashSet :: new ( ) ) )
530+ . get_or_init ( || RefCell :: new ( HashSet :: new ( ) ) )
530531 . borrow_mut ( )
531532 }
532533
533534 /// Cached credentials from credential providers or configuration.
534535 pub fn credential_cache ( & self ) -> RefMut < ' _ , HashMap < CanonicalUrl , CredentialCacheValue > > {
535536 self . credential_cache
536- . borrow_with ( || RefCell :: new ( HashMap :: new ( ) ) )
537+ . get_or_init ( || RefCell :: new ( HashMap :: new ( ) ) )
537538 . borrow_mut ( )
538539 }
539540
540541 /// Cache of already parsed registries from the `[registries]` table.
541542 pub ( crate ) fn registry_config ( & self ) -> RefMut < ' _ , HashMap < SourceId , Option < RegistryConfig > > > {
542543 self . registry_config
543- . borrow_with ( || RefCell :: new ( HashMap :: new ( ) ) )
544+ . get_or_init ( || RefCell :: new ( HashMap :: new ( ) ) )
544545 . borrow_mut ( )
545546 }
546547
@@ -561,18 +562,15 @@ impl GlobalContext {
561562 /// using this if possible.
562563 pub fn values_mut ( & mut self ) -> CargoResult < & mut HashMap < String , ConfigValue > > {
563564 let _ = self . values ( ) ?;
564- Ok ( self
565- . values
566- . borrow_mut ( )
567- . expect ( "already loaded config values" ) )
565+ Ok ( self . values . get_mut ( ) . expect ( "already loaded config values" ) )
568566 }
569567
570568 // Note: this is used by RLS, not Cargo.
571569 pub fn set_values ( & self , values : HashMap < String , ConfigValue > ) -> CargoResult < ( ) > {
572- if self . values . borrow ( ) . is_some ( ) {
570+ if self . values . get ( ) . is_some ( ) {
573571 bail ! ( "config values already found" )
574572 }
575- match self . values . fill ( values) {
573+ match self . values . set ( values) {
576574 Ok ( ( ) ) => Ok ( ( ) ) ,
577575 Err ( _) => bail ! ( "could not fill values" ) ,
578576 }
@@ -741,7 +739,7 @@ impl GlobalContext {
741739 /// This does NOT look at environment variables. See `get_cv_with_env` for
742740 /// a variant that supports environment variables.
743741 fn get_cv ( & self , key : & ConfigKey ) -> CargoResult < Option < ConfigValue > > {
744- if let Some ( vals) = self . credential_values . borrow ( ) {
742+ if let Some ( vals) = self . credential_values . get ( ) {
745743 let val = self . get_cv_helper ( key, vals) ?;
746744 if val. is_some ( ) {
747745 return Ok ( val) ;
@@ -1802,7 +1800,7 @@ impl GlobalContext {
18021800 }
18031801 }
18041802 self . credential_values
1805- . fill ( credential_values)
1803+ . set ( credential_values)
18061804 . expect ( "was not filled at beginning of the function" ) ;
18071805 Ok ( ( ) )
18081806 }
0 commit comments