Auto merge of #12711 - weihanglo:cached, r=epage

refactor: move cached crates.io SourceID to config module
This commit is contained in:
bors 2023-09-20 14:52:44 +00:00
commit 376b9a75bd
2 changed files with 14 additions and 10 deletions

View file

@ -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<SourceId> {
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

View file

@ -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<F>(&self, f: F) -> CargoResult<SourceId>
where
F: FnMut() -> CargoResult<SourceId>,
{
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<SourceId> {
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 {