Skip to content

Commit

Permalink
refactor: move cached crates.io SourceID to config module
Browse files Browse the repository at this point in the history
`SourceId` should know nothing about `config` caching.
  • Loading branch information
weihanglo committed Sep 20, 2023
1 parent b4ddf95 commit 1fa386e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
6 changes: 1 addition & 5 deletions src/cargo/core/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1fa386e

Please sign in to comment.