Skip to content

Commit 5fb0b34

Browse files
committed
fix: move ActivationsKey into core
1 parent a3e89ee commit 5fb0b34

File tree

4 files changed

+42
-40
lines changed

4 files changed

+42
-40
lines changed

src/cargo/core/activation_key.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use std::num::NonZeroU64;
2+
3+
use crate::core::SourceId;
4+
use crate::util::interning::InternedString;
5+
6+
/// Find the activated version of a crate based on the name, source, and semver compatibility.
7+
/// By storing this in a hash map we ensure that there is only one
8+
/// semver compatible version of each crate.
9+
/// This all so stores the `ContextAge`.
10+
pub type ActivationsKey = (InternedString, SourceId, SemverCompatibility);
11+
12+
/// A type that represents when cargo treats two Versions as compatible.
13+
/// Versions `a` and `b` are compatible if their left-most nonzero digit is the
14+
/// same.
15+
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug, PartialOrd, Ord)]
16+
pub enum SemverCompatibility {
17+
Major(NonZeroU64),
18+
Minor(NonZeroU64),
19+
Patch(u64),
20+
}
21+
22+
impl From<&semver::Version> for SemverCompatibility {
23+
fn from(ver: &semver::Version) -> Self {
24+
if let Some(m) = NonZeroU64::new(ver.major) {
25+
return SemverCompatibility::Major(m);
26+
}
27+
if let Some(m) = NonZeroU64::new(ver.minor) {
28+
return SemverCompatibility::Minor(m);
29+
}
30+
SemverCompatibility::Patch(ver.patch)
31+
}
32+
}

src/cargo/core/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub use self::activation_key::ActivationsKey;
12
pub use self::dependency::{Dependency, SerializedDependency};
23
pub use self::features::{CliUnstable, Edition, Feature, Features};
34
pub use self::manifest::{EitherManifest, VirtualManifest};
@@ -16,6 +17,7 @@ pub use self::workspace::{
1617
};
1718
pub use cargo_util_schemas::core::{GitReference, PackageIdSpec, SourceKind};
1819

20+
pub mod activation_key;
1921
pub mod compiler;
2022
pub mod dependency;
2123
pub mod features;

src/cargo/core/package_id.rs

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::sync::OnceLock;
1010
use serde::de;
1111
use serde::ser;
1212

13+
use crate::core::ActivationsKey;
1314
use crate::core::PackageIdSpec;
1415
use crate::core::SourceId;
1516
use crate::util::interning::InternedString;
@@ -160,6 +161,9 @@ impl PackageId {
160161
pub fn source_id(self) -> SourceId {
161162
self.inner.source_id
162163
}
164+
pub fn as_activations_key(self) -> ActivationsKey {
165+
(self.name(), self.source_id(), self.version().into())
166+
}
163167

164168
pub fn with_source_id(self, source: SourceId) -> PackageId {
165169
PackageId::new(self.inner.name, self.inner.version.clone(), source)

src/cargo/core/resolver/context.rs

+4-40
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ use super::dep_cache::RegistryQueryer;
22
use super::errors::ActivateResult;
33
use super::types::{ConflictMap, ConflictReason, FeaturesSet, ResolveOpts};
44
use super::RequestedFeatures;
5-
use crate::core::{Dependency, PackageId, SourceId, Summary};
5+
use crate::core::{ActivationsKey, Dependency, PackageId, Summary};
66
use crate::util::interning::InternedString;
77
use crate::util::Graph;
88
use anyhow::format_err;
99
use std::collections::HashMap;
10-
use std::num::NonZeroU64;
1110
use tracing::debug;
1211

1312
// A `Context` is basically a bunch of local resolution information which is
@@ -22,56 +21,21 @@ pub struct ResolverContext {
2221
pub resolve_features: im_rc::HashMap<PackageId, FeaturesSet, rustc_hash::FxBuildHasher>,
2322
/// get the package that will be linking to a native library by its links attribute
2423
pub links: im_rc::HashMap<InternedString, PackageId, rustc_hash::FxBuildHasher>,
25-
2624
/// a way to look up for a package in activations what packages required it
2725
/// and all of the exact deps that it fulfilled.
2826
pub parents: Graph<PackageId, im_rc::HashSet<Dependency, rustc_hash::FxBuildHasher>>,
2927
}
3028

29+
pub type Activations =
30+
im_rc::HashMap<ActivationsKey, (Summary, ContextAge), rustc_hash::FxBuildHasher>;
31+
3132
/// When backtracking it can be useful to know how far back to go.
3233
/// The `ContextAge` of a `Context` is a monotonically increasing counter of the number
3334
/// of decisions made to get to this state.
3435
/// Several structures store the `ContextAge` when it was added,
3536
/// to be used in `find_candidate` for backtracking.
3637
pub type ContextAge = usize;
3738

38-
/// Find the activated version of a crate based on the name, source, and semver compatibility.
39-
/// By storing this in a hash map we ensure that there is only one
40-
/// semver compatible version of each crate.
41-
/// This all so stores the `ContextAge`.
42-
pub type ActivationsKey = (InternedString, SourceId, SemverCompatibility);
43-
44-
pub type Activations =
45-
im_rc::HashMap<ActivationsKey, (Summary, ContextAge), rustc_hash::FxBuildHasher>;
46-
47-
/// A type that represents when cargo treats two Versions as compatible.
48-
/// Versions `a` and `b` are compatible if their left-most nonzero digit is the
49-
/// same.
50-
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug, PartialOrd, Ord)]
51-
pub enum SemverCompatibility {
52-
Major(NonZeroU64),
53-
Minor(NonZeroU64),
54-
Patch(u64),
55-
}
56-
57-
impl From<&semver::Version> for SemverCompatibility {
58-
fn from(ver: &semver::Version) -> Self {
59-
if let Some(m) = NonZeroU64::new(ver.major) {
60-
return SemverCompatibility::Major(m);
61-
}
62-
if let Some(m) = NonZeroU64::new(ver.minor) {
63-
return SemverCompatibility::Minor(m);
64-
}
65-
SemverCompatibility::Patch(ver.patch)
66-
}
67-
}
68-
69-
impl PackageId {
70-
pub fn as_activations_key(self) -> ActivationsKey {
71-
(self.name(), self.source_id(), self.version().into())
72-
}
73-
}
74-
7539
impl ResolverContext {
7640
pub fn new() -> ResolverContext {
7741
ResolverContext {

0 commit comments

Comments
 (0)