Skip to main content

uv_resolver/resolver/
index.rs

1use std::hash::BuildHasherDefault;
2use std::sync::Arc;
3
4use rustc_hash::FxHasher;
5use uv_distribution_types::IndexUrl;
6use uv_normalize::PackageName;
7use uv_once_map::{RegisteredEntry, RegisteredOnceMap};
8use uv_resolver_types::DistributionMetadataIndex;
9
10use crate::resolver::provider::VersionsResponse;
11
12/// In-memory index of package metadata.
13#[derive(Default, Clone)]
14pub struct InMemoryIndex(Arc<SharedInMemoryIndex>);
15
16#[derive(Default)]
17struct SharedInMemoryIndex {
18    /// A map from package name to the metadata for that package and the index where the metadata
19    /// came from.
20    implicit: FxOnceMap<PackageName, Arc<VersionsResponse>>,
21
22    explicit: FxOnceMap<(PackageName, IndexUrl), Arc<VersionsResponse>>,
23
24    /// A map from a concrete distribution to its metadata.
25    distributions: DistributionMetadataIndex,
26}
27
28pub(crate) type FxOnceMap<K, V> = RegisteredOnceMap<K, V, BuildHasherDefault<FxHasher>>;
29pub(crate) type FxRegisteredEntry<'a, K, V> =
30    RegisteredEntry<'a, K, V, BuildHasherDefault<FxHasher>>;
31
32impl InMemoryIndex {
33    /// Returns a reference to the package metadata map.
34    pub(crate) fn implicit(&self) -> &FxOnceMap<PackageName, Arc<VersionsResponse>> {
35        &self.0.implicit
36    }
37
38    /// Returns a reference to the package metadata map.
39    pub(crate) fn explicit(&self) -> &FxOnceMap<(PackageName, IndexUrl), Arc<VersionsResponse>> {
40        &self.0.explicit
41    }
42
43    /// Returns a reference to the distribution metadata map.
44    pub fn distributions(&self) -> &DistributionMetadataIndex {
45        &self.0.distributions
46    }
47
48    /// Return exclusive access to distribution metadata when no cloned index can use it.
49    pub fn distributions_mut(&mut self) -> Option<&mut DistributionMetadataIndex> {
50        Arc::get_mut(&mut self.0).map(|index| &mut index.distributions)
51    }
52}