51
51
52
52
use crate :: util:: cache_lock:: { CacheLock , CacheLockMode , CacheLocker } ;
53
53
use std:: borrow:: Cow ;
54
+ use std:: cell:: OnceCell ;
54
55
use std:: cell:: { RefCell , RefMut } ;
55
56
use std:: collections:: hash_map:: Entry :: { Occupied , Vacant } ;
56
57
use std:: collections:: { HashMap , HashSet } ;
@@ -74,6 +75,7 @@ use crate::core::{CliUnstable, Shell, SourceId, Workspace, WorkspaceRootConfig,
74
75
use crate :: ops:: RegistryCredentialConfig ;
75
76
use crate :: sources:: CRATES_IO_INDEX ;
76
77
use crate :: sources:: CRATES_IO_REGISTRY ;
78
+ use crate :: util:: OnceExt as _;
77
79
use crate :: util:: errors:: CargoResult ;
78
80
use crate :: util:: network:: http:: configure_http_handle;
79
81
use crate :: util:: network:: http:: http_handle;
@@ -85,7 +87,6 @@ use cargo_util::paths;
85
87
use cargo_util_schemas:: manifest:: RegistryName ;
86
88
use curl:: easy:: Easy ;
87
89
use itertools:: Itertools ;
88
- use lazycell:: LazyCell ;
89
90
use serde:: Deserialize ;
90
91
use serde:: de:: IntoDeserializer as _;
91
92
use serde_untagged:: UntaggedEnumVisitor ;
@@ -168,19 +169,19 @@ pub struct GlobalContext {
168
169
/// Information about how to write messages to the shell
169
170
shell : RefCell < Shell > ,
170
171
/// A collection of configuration options
171
- values : LazyCell < HashMap < String , ConfigValue > > ,
172
+ values : OnceCell < HashMap < String , ConfigValue > > ,
172
173
/// A collection of configuration options from the credentials file
173
- credential_values : LazyCell < HashMap < String , ConfigValue > > ,
174
+ credential_values : OnceCell < HashMap < String , ConfigValue > > ,
174
175
/// CLI config values, passed in via `configure`.
175
176
cli_config : Option < Vec < String > > ,
176
177
/// The current working directory of cargo
177
178
cwd : PathBuf ,
178
179
/// Directory where config file searching should stop (inclusive).
179
180
search_stop_path : Option < PathBuf > ,
180
181
/// The location of the cargo executable (path to current process)
181
- cargo_exe : LazyCell < PathBuf > ,
182
+ cargo_exe : OnceCell < PathBuf > ,
182
183
/// The location of the rustdoc executable
183
- rustdoc : LazyCell < PathBuf > ,
184
+ rustdoc : OnceCell < PathBuf > ,
184
185
/// Whether we are printing extra verbose messages
185
186
extra_verbose : bool ,
186
187
/// `frozen` is the same as `locked`, but additionally will not access the
@@ -199,9 +200,9 @@ pub struct GlobalContext {
199
200
/// Cli flags of the form "-Z something"
200
201
unstable_flags_cli : Option < Vec < String > > ,
201
202
/// A handle on curl easy mode for http calls
202
- easy : LazyCell < RefCell < Easy > > ,
203
+ easy : OnceCell < RefCell < Easy > > ,
203
204
/// Cache of the `SourceId` for crates.io
204
- crates_io_source_id : LazyCell < SourceId > ,
205
+ crates_io_source_id : OnceCell < SourceId > ,
205
206
/// If false, don't cache `rustc --version --verbose` invocations
206
207
cache_rustc_info : bool ,
207
208
/// Creation time of this config, used to output the total build time
@@ -211,23 +212,23 @@ pub struct GlobalContext {
211
212
/// Environment variable snapshot.
212
213
env : Env ,
213
214
/// Tracks which sources have been updated to avoid multiple updates.
214
- updated_sources : LazyCell < RefCell < HashSet < SourceId > > > ,
215
+ updated_sources : OnceCell < RefCell < HashSet < SourceId > > > ,
215
216
/// Cache of credentials from configuration or credential providers.
216
217
/// Maps from url to credential value.
217
- credential_cache : LazyCell < RefCell < HashMap < CanonicalUrl , CredentialCacheValue > > > ,
218
+ credential_cache : OnceCell < RefCell < HashMap < CanonicalUrl , CredentialCacheValue > > > ,
218
219
/// 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 > > > > ,
220
221
/// Locks on the package and index caches.
221
222
package_cache_lock : CacheLocker ,
222
223
/// 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 > ,
229
230
progress_config : ProgressConfig ,
230
- env_config : LazyCell < Arc < HashMap < String , OsString > > > ,
231
+ env_config : OnceCell < Arc < HashMap < String , OsString > > > ,
231
232
/// This should be false if:
232
233
/// - this is an artifact of the rustc distribution process for "stable" or for "beta"
233
234
/// - this is an `#[test]` that does not opt in with `enable_nightly_features`
@@ -247,10 +248,10 @@ pub struct GlobalContext {
247
248
/// `WorkspaceRootConfigs` that have been found
248
249
pub ws_roots : RefCell < HashMap < PathBuf , WorkspaceRootConfig > > ,
249
250
/// 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 > > ,
251
252
/// A cache of modifications to make to [`GlobalContext::global_cache_tracker`],
252
253
/// 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 > > ,
254
255
}
255
256
256
257
impl GlobalContext {
@@ -286,11 +287,11 @@ impl GlobalContext {
286
287
shell : RefCell :: new ( shell) ,
287
288
cwd,
288
289
search_stop_path : None ,
289
- values : LazyCell :: new ( ) ,
290
- credential_values : LazyCell :: new ( ) ,
290
+ values : OnceCell :: new ( ) ,
291
+ credential_values : OnceCell :: new ( ) ,
291
292
cli_config : None ,
292
- cargo_exe : LazyCell :: new ( ) ,
293
- rustdoc : LazyCell :: new ( ) ,
293
+ cargo_exe : OnceCell :: new ( ) ,
294
+ rustdoc : OnceCell :: new ( ) ,
294
295
extra_verbose : false ,
295
296
frozen : false ,
296
297
locked : false ,
@@ -304,28 +305,28 @@ impl GlobalContext {
304
305
} ,
305
306
unstable_flags : CliUnstable :: default ( ) ,
306
307
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 ( ) ,
309
310
cache_rustc_info,
310
311
creation_time : Instant :: now ( ) ,
311
312
target_dir : None ,
312
313
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 ( ) ,
316
317
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 ( ) ,
323
324
progress_config : ProgressConfig :: default ( ) ,
324
- env_config : LazyCell :: new ( ) ,
325
+ env_config : OnceCell :: new ( ) ,
325
326
nightly_features_allowed : matches ! ( & * features:: channel( ) , "nightly" | "dev" ) ,
326
327
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 ( ) ,
329
330
}
330
331
}
331
332
@@ -526,21 +527,21 @@ impl GlobalContext {
526
527
/// Which package sources have been updated, used to ensure it is only done once.
527
528
pub fn updated_sources ( & self ) -> RefMut < ' _ , HashSet < SourceId > > {
528
529
self . updated_sources
529
- . borrow_with ( || RefCell :: new ( HashSet :: new ( ) ) )
530
+ . get_or_init ( || RefCell :: new ( HashSet :: new ( ) ) )
530
531
. borrow_mut ( )
531
532
}
532
533
533
534
/// Cached credentials from credential providers or configuration.
534
535
pub fn credential_cache ( & self ) -> RefMut < ' _ , HashMap < CanonicalUrl , CredentialCacheValue > > {
535
536
self . credential_cache
536
- . borrow_with ( || RefCell :: new ( HashMap :: new ( ) ) )
537
+ . get_or_init ( || RefCell :: new ( HashMap :: new ( ) ) )
537
538
. borrow_mut ( )
538
539
}
539
540
540
541
/// Cache of already parsed registries from the `[registries]` table.
541
542
pub ( crate ) fn registry_config ( & self ) -> RefMut < ' _ , HashMap < SourceId , Option < RegistryConfig > > > {
542
543
self . registry_config
543
- . borrow_with ( || RefCell :: new ( HashMap :: new ( ) ) )
544
+ . get_or_init ( || RefCell :: new ( HashMap :: new ( ) ) )
544
545
. borrow_mut ( )
545
546
}
546
547
@@ -561,18 +562,15 @@ impl GlobalContext {
561
562
/// using this if possible.
562
563
pub fn values_mut ( & mut self ) -> CargoResult < & mut HashMap < String , ConfigValue > > {
563
564
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" ) )
568
566
}
569
567
570
568
// Note: this is used by RLS, not Cargo.
571
569
pub fn set_values ( & self , values : HashMap < String , ConfigValue > ) -> CargoResult < ( ) > {
572
- if self . values . borrow ( ) . is_some ( ) {
570
+ if self . values . get ( ) . is_some ( ) {
573
571
bail ! ( "config values already found" )
574
572
}
575
- match self . values . fill ( values) {
573
+ match self . values . set ( values) {
576
574
Ok ( ( ) ) => Ok ( ( ) ) ,
577
575
Err ( _) => bail ! ( "could not fill values" ) ,
578
576
}
@@ -741,7 +739,7 @@ impl GlobalContext {
741
739
/// This does NOT look at environment variables. See `get_cv_with_env` for
742
740
/// a variant that supports environment variables.
743
741
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 ( ) {
745
743
let val = self . get_cv_helper ( key, vals) ?;
746
744
if val. is_some ( ) {
747
745
return Ok ( val) ;
@@ -1802,7 +1800,7 @@ impl GlobalContext {
1802
1800
}
1803
1801
}
1804
1802
self . credential_values
1805
- . fill ( credential_values)
1803
+ . set ( credential_values)
1806
1804
. expect ( "was not filled at beginning of the function" ) ;
1807
1805
Ok ( ( ) )
1808
1806
}
0 commit comments