Skip to content

Commit f7ab7cc

Browse files
committed
refactor: make Kind smaller
1 parent 01008c6 commit f7ab7cc

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

src/internal/incompatibility.rs

+27-23
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ enum Kind<P: Package, V: Version> {
4343
/// Initial incompatibility aiming at picking the root package for the first decision.
4444
NotRoot(P, V),
4545
/// There are no versions in the given range for this package.
46-
NoVersions(P, Range<V>),
46+
NoVersions(P),
4747
/// Dependencies of the package are unavailable for versions in that range.
48-
UnavailableDependencies(P, Range<V>),
48+
UnavailableDependencies(P),
4949
/// Incompatibility coming from the dependencies of a given package.
50-
FromDependencyOf(P, Range<V>, P, Range<V>),
50+
FromDependencyOf(P, P),
5151
/// Derived from two causes. Stores cause ids.
5252
DerivedFrom(IncompId<P, V>, IncompId<P, V>),
5353
}
@@ -84,24 +84,23 @@ impl<P: Package, V: Version> Incompatibility<P, V> {
8484
/// Create an incompatibility to remember
8585
/// that a given range does not contain any version.
8686
pub fn no_versions(package: P, term: Term<V>) -> Self {
87-
let range = match &term {
88-
Term::Positive(r) => r.clone(),
89-
Term::Negative(_) => panic!("No version should have a positive term"),
90-
};
87+
assert!(term.is_positive(), "No version should have a positive term");
9188
Self {
9289
package_terms: SmallMap::One([(package.clone(), term)]),
93-
kind: Kind::NoVersions(package, range),
90+
kind: Kind::NoVersions(package),
9491
}
9592
}
9693

9794
/// Create an incompatibility to remember
9895
/// that a package version is not selectable
9996
/// because its list of dependencies is unavailable.
10097
pub fn unavailable_dependencies(package: P, version: V) -> Self {
101-
let range = Range::exact(version);
10298
Self {
103-
package_terms: SmallMap::One([(package.clone(), Term::Positive(range.clone()))]),
104-
kind: Kind::UnavailableDependencies(package, range),
99+
package_terms: SmallMap::One([(
100+
package.clone(),
101+
Term::Positive(Range::exact(version)),
102+
)]),
103+
kind: Kind::UnavailableDependencies(package),
105104
}
106105
}
107106

@@ -114,7 +113,7 @@ impl<P: Package, V: Version> Incompatibility<P, V> {
114113
(package.clone(), Term::Positive(range1.clone())),
115114
(p2.clone(), Term::Negative(range2.clone())),
116115
]),
117-
kind: Kind::FromDependencyOf(package, range1, p2.clone(), range2.clone()),
116+
kind: Kind::FromDependencyOf(package, p2.clone()),
118117
}
119118
}
120119

@@ -239,7 +238,8 @@ impl<P: Package, V: Version> Incompatibility<P, V> {
239238
shared_ids: &Set<Id<Self>>,
240239
store: &Arena<Self>,
241240
) -> DerivationTree<P, V> {
242-
match &store[self_id].kind {
241+
let val = &store[self_id];
242+
match &val.kind {
243243
Kind::DerivedFrom(id1, id2) => {
244244
let cause1 = Self::build_derivation_tree(*id1, shared_ids, store);
245245
let cause2 = Self::build_derivation_tree(*id2, shared_ids, store);
@@ -254,18 +254,22 @@ impl<P: Package, V: Version> Incompatibility<P, V> {
254254
Kind::NotRoot(package, version) => {
255255
DerivationTree::External(External::NotRoot(package.clone(), version.clone()))
256256
}
257-
Kind::NoVersions(package, range) => {
258-
DerivationTree::External(External::NoVersions(package.clone(), range.clone()))
257+
Kind::NoVersions(package) => DerivationTree::External(External::NoVersions(
258+
package.clone(),
259+
val.package_terms[package].as_range().clone(),
260+
)),
261+
Kind::UnavailableDependencies(package) => {
262+
DerivationTree::External(External::UnavailableDependencies(
263+
package.clone(),
264+
val.package_terms[package].as_range().clone(),
265+
))
259266
}
260-
Kind::UnavailableDependencies(package, range) => DerivationTree::External(
261-
External::UnavailableDependencies(package.clone(), range.clone()),
262-
),
263-
Kind::FromDependencyOf(package, range, dep_package, dep_range) => {
267+
Kind::FromDependencyOf(package, dep_package) => {
264268
DerivationTree::External(External::FromDependencyOf(
265269
package.clone(),
266-
range.clone(),
270+
val.package_terms[package].as_range().clone(),
267271
dep_package.clone(),
268-
dep_range.clone(),
272+
val.package_terms[dep_package].as_range().clone(),
269273
))
270274
}
271275
}
@@ -305,12 +309,12 @@ pub mod tests {
305309
let mut store = Arena::new();
306310
let i1 = store.alloc(Incompatibility {
307311
package_terms: SmallMap::Two([("p1", t1.clone()), ("p2", t2.negate())]),
308-
kind: Kind::UnavailableDependencies("0", Range::any())
312+
kind: Kind::UnavailableDependencies("0")
309313
});
310314

311315
let i2 = store.alloc(Incompatibility {
312316
package_terms: SmallMap::Two([("p2", t2), ("p3", t3.clone())]),
313-
kind: Kind::UnavailableDependencies("0", Range::any())
317+
kind: Kind::UnavailableDependencies("0")
314318
});
315319

316320
let mut i3 = Map::default();

src/internal/small_map.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::type_aliases::Map;
2-
use std::hash::Hash;
2+
use std::{hash::Hash, ops::Index};
33

44
#[derive(Debug, Clone)]
55
pub enum SmallMap<K, V> {
@@ -146,6 +146,13 @@ impl<K, V> SmallMap<K, V> {
146146
}
147147
}
148148

149+
impl<K: PartialEq + Eq + Hash, V> Index<&K> for SmallMap<K, V> {
150+
type Output = V;
151+
fn index(&self, key: &K) -> &V {
152+
&self.get(key).unwrap()
153+
}
154+
}
155+
149156
impl<K: Eq + Hash + Clone, V: Clone> SmallMap<K, V> {
150157
pub fn as_map(&self) -> Map<K, V> {
151158
match self {

src/term.rs

+8
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ impl<V: Version> Term<V> {
7171
_ => panic!("Negative term cannot unwrap positive range"),
7272
}
7373
}
74+
75+
/// Unwrap the range contains in the term.
76+
pub(crate) fn as_range(&self) -> &Range<V> {
77+
match self {
78+
Self::Positive(range) => range,
79+
Self::Negative(range) => range,
80+
}
81+
}
7482
}
7583

7684
/// Set operations with terms.

0 commit comments

Comments
 (0)