Skip to content

Commit 0ab9b1c

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

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

src/internal/incompatibility.rs

Lines changed: 28 additions & 25 deletions
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,37 +84,35 @@ 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

108107
/// Build an incompatibility from a given dependency.
109108
pub fn from_dependency(package: P, version: V, dep: (&P, &Range<V>)) -> Self {
110-
let range1 = Range::exact(version);
111109
let (p2, range2) = dep;
112110
Self {
113111
package_terms: SmallMap::Two([
114-
(package.clone(), Term::Positive(range1.clone())),
112+
(package.clone(), Term::Positive(Range::exact(version))),
115113
(p2.clone(), Term::Negative(range2.clone())),
116114
]),
117-
kind: Kind::FromDependencyOf(package, range1, p2.clone(), range2.clone()),
115+
kind: Kind::FromDependencyOf(package, p2.clone()),
118116
}
119117
}
120118

@@ -239,7 +237,8 @@ impl<P: Package, V: Version> Incompatibility<P, V> {
239237
shared_ids: &Set<Id<Self>>,
240238
store: &Arena<Self>,
241239
) -> DerivationTree<P, V> {
242-
match &store[self_id].kind {
240+
let val = &store[self_id];
241+
match &val.kind {
243242
Kind::DerivedFrom(id1, id2) => {
244243
let cause1 = Self::build_derivation_tree(*id1, shared_ids, store);
245244
let cause2 = Self::build_derivation_tree(*id2, shared_ids, store);
@@ -254,18 +253,22 @@ impl<P: Package, V: Version> Incompatibility<P, V> {
254253
Kind::NotRoot(package, version) => {
255254
DerivationTree::External(External::NotRoot(package.clone(), version.clone()))
256255
}
257-
Kind::NoVersions(package, range) => {
258-
DerivationTree::External(External::NoVersions(package.clone(), range.clone()))
256+
Kind::NoVersions(package) => DerivationTree::External(External::NoVersions(
257+
package.clone(),
258+
val.package_terms[package].as_range().clone(),
259+
)),
260+
Kind::UnavailableDependencies(package) => {
261+
DerivationTree::External(External::UnavailableDependencies(
262+
package.clone(),
263+
val.package_terms[package].as_range().clone(),
264+
))
259265
}
260-
Kind::UnavailableDependencies(package, range) => DerivationTree::External(
261-
External::UnavailableDependencies(package.clone(), range.clone()),
262-
),
263-
Kind::FromDependencyOf(package, range, dep_package, dep_range) => {
266+
Kind::FromDependencyOf(package, dep_package) => {
264267
DerivationTree::External(External::FromDependencyOf(
265268
package.clone(),
266-
range.clone(),
269+
val.package_terms[package].as_range().clone(),
267270
dep_package.clone(),
268-
dep_range.clone(),
271+
val.package_terms[dep_package].as_range().clone(),
269272
))
270273
}
271274
}
@@ -305,12 +308,12 @@ pub mod tests {
305308
let mut store = Arena::new();
306309
let i1 = store.alloc(Incompatibility {
307310
package_terms: SmallMap::Two([("p1", t1.clone()), ("p2", t2.negate())]),
308-
kind: Kind::UnavailableDependencies("0", Range::any())
311+
kind: Kind::UnavailableDependencies("0")
309312
});
310313

311314
let i2 = store.alloc(Incompatibility {
312315
package_terms: SmallMap::Two([("p2", t2), ("p3", t3.clone())]),
313-
kind: Kind::UnavailableDependencies("0", Range::any())
316+
kind: Kind::UnavailableDependencies("0")
314317
});
315318

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

src/internal/small_map.rs

Lines changed: 8 additions & 1 deletion
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

Lines changed: 8 additions & 0 deletions
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)