Skip to content

Commit 1fa386e

Browse files
committed
refactor: move cached crates.io SourceID to config module
`SourceId` should know nothing about `config` caching.
1 parent b4ddf95 commit 1fa386e

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/cargo/core/source_id.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,7 @@ impl SourceId {
253253
/// This is the main cargo registry by default, but it can be overridden in
254254
/// a `.cargo/config.toml`.
255255
pub fn crates_io(config: &Config) -> CargoResult<SourceId> {
256-
config.crates_io_source_id(|| {
257-
config.check_registry_index_not_set()?;
258-
let url = CRATES_IO_INDEX.into_url().unwrap();
259-
SourceId::new(SourceKind::Registry, url, Some(CRATES_IO_REGISTRY))
260-
})
256+
config.crates_io_source_id()
261257
}
262258

263259
/// Returns the `SourceId` corresponding to the main repository, using the

src/cargo/util/config/mod.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ use crate::core::compiler::rustdoc::RustdocExternMap;
7070
use crate::core::shell::Verbosity;
7171
use crate::core::{features, CliUnstable, Shell, SourceId, Workspace, WorkspaceRootConfig};
7272
use crate::ops::RegistryCredentialConfig;
73+
use crate::sources::CRATES_IO_INDEX;
74+
use crate::sources::CRATES_IO_REGISTRY;
7375
use crate::util::errors::CargoResult;
7476
use crate::util::network::http::configure_http_handle;
7577
use crate::util::network::http::http_handle;
@@ -1831,11 +1833,17 @@ impl Config {
18311833
target::load_target_triple(self, target)
18321834
}
18331835

1834-
pub fn crates_io_source_id<F>(&self, f: F) -> CargoResult<SourceId>
1835-
where
1836-
F: FnMut() -> CargoResult<SourceId>,
1837-
{
1838-
Ok(*(self.crates_io_source_id.try_borrow_with(f)?))
1836+
/// Returns the cached [`SourceId`] corresponding to the main repository.
1837+
///
1838+
/// This is the main cargo registry by default, but it can be overridden in
1839+
/// a `.cargo/config.toml`.
1840+
pub fn crates_io_source_id(&self) -> CargoResult<SourceId> {
1841+
let source_id = self.crates_io_source_id.try_borrow_with(|| {
1842+
self.check_registry_index_not_set()?;
1843+
let url = CRATES_IO_INDEX.into_url().unwrap();
1844+
SourceId::for_alt_registry(&url, CRATES_IO_REGISTRY)
1845+
})?;
1846+
Ok(*source_id)
18391847
}
18401848

18411849
pub fn creation_time(&self) -> Instant {

0 commit comments

Comments
 (0)