From 1fa386e1c416181d04a6b658bb332c9b58c76022 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 15 Sep 2023 12:39:18 +0800 Subject: [PATCH] refactor: move cached crates.io SourceID to config module `SourceId` should know nothing about `config` caching. --- src/cargo/core/source_id.rs | 6 +----- src/cargo/util/config/mod.rs | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/cargo/core/source_id.rs b/src/cargo/core/source_id.rs index cec16f6e35d..152c3ad30fa 100644 --- a/src/cargo/core/source_id.rs +++ b/src/cargo/core/source_id.rs @@ -253,11 +253,7 @@ impl SourceId { /// This is the main cargo registry by default, but it can be overridden in /// a `.cargo/config.toml`. pub fn crates_io(config: &Config) -> CargoResult { - config.crates_io_source_id(|| { - config.check_registry_index_not_set()?; - let url = CRATES_IO_INDEX.into_url().unwrap(); - SourceId::new(SourceKind::Registry, url, Some(CRATES_IO_REGISTRY)) - }) + config.crates_io_source_id() } /// Returns the `SourceId` corresponding to the main repository, using the diff --git a/src/cargo/util/config/mod.rs b/src/cargo/util/config/mod.rs index d6fba5db9de..232ca27c0d2 100644 --- a/src/cargo/util/config/mod.rs +++ b/src/cargo/util/config/mod.rs @@ -70,6 +70,8 @@ use crate::core::compiler::rustdoc::RustdocExternMap; use crate::core::shell::Verbosity; use crate::core::{features, CliUnstable, Shell, SourceId, Workspace, WorkspaceRootConfig}; use crate::ops::RegistryCredentialConfig; +use crate::sources::CRATES_IO_INDEX; +use crate::sources::CRATES_IO_REGISTRY; use crate::util::errors::CargoResult; use crate::util::network::http::configure_http_handle; use crate::util::network::http::http_handle; @@ -1831,11 +1833,17 @@ impl Config { target::load_target_triple(self, target) } - pub fn crates_io_source_id(&self, f: F) -> CargoResult - where - F: FnMut() -> CargoResult, - { - Ok(*(self.crates_io_source_id.try_borrow_with(f)?)) + /// Returns the cached [`SourceId`] corresponding to the main repository. + /// + /// This is the main cargo registry by default, but it can be overridden in + /// a `.cargo/config.toml`. + pub fn crates_io_source_id(&self) -> CargoResult { + let source_id = self.crates_io_source_id.try_borrow_with(|| { + self.check_registry_index_not_set()?; + let url = CRATES_IO_INDEX.into_url().unwrap(); + SourceId::for_alt_registry(&url, CRATES_IO_REGISTRY) + })?; + Ok(*source_id) } pub fn creation_time(&self) -> Instant {