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::OnceMap;
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> = OnceMap<K, V, BuildHasherDefault<FxHasher>>;
29
30impl InMemoryIndex {
31    /// Returns a reference to the package metadata map.
32    pub(crate) fn implicit(&self) -> &FxOnceMap<PackageName, Arc<VersionsResponse>> {
33        &self.0.implicit
34    }
35
36    /// Returns a reference to the package metadata map.
37    pub(crate) fn explicit(&self) -> &FxOnceMap<(PackageName, IndexUrl), Arc<VersionsResponse>> {
38        &self.0.explicit
39    }
40
41    /// Returns a reference to the distribution metadata map.
42    pub fn distributions(&self) -> &DistributionMetadataIndex {
43        &self.0.distributions
44    }
45}