Skip to content

Commit 6be0fbf

Browse files
refactor: add minor code clean-up (#83)
* refactor: add minor code clean-up * style: adjust code to project convention * style: unmerge std imports in SmallVec * style: rename option args to `maybe_*`
1 parent b7087ba commit 6be0fbf

File tree

6 files changed

+46
-30
lines changed

6 files changed

+46
-30
lines changed

src/internal/partial_solution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl<P: Package, V: Version> PartialSolution<P, V> {
179179
incompat: &Incompatibility<P, V>,
180180
store: &Arena<Incompatibility<P, V>>,
181181
) -> (&Assignment<P, V>, DecisionLevel, DecisionLevel) {
182-
let satisfier_map = Self::find_satisfier(incompat, self.history.as_slice(), store);
182+
let satisfier_map = Self::find_satisfier(incompat, &self.history, store);
183183
assert_eq!(
184184
satisfier_map.len(),
185185
incompat.len(),

src/internal/small_vec.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt;
2+
use std::ops::Deref;
23

34
#[derive(Clone)]
45
pub enum SmallVec<T> {
@@ -38,7 +39,7 @@ impl<T> SmallVec<T> {
3839
}
3940
}
4041

41-
pub fn iter(&self) -> impl Iterator<Item = &T> {
42+
pub fn iter(&self) -> std::slice::Iter<'_, T> {
4243
self.as_slice().iter()
4344
}
4445
}
@@ -49,10 +50,28 @@ impl<T> Default for SmallVec<T> {
4950
}
5051
}
5152

52-
impl<T: PartialEq> Eq for SmallVec<T> {}
53+
impl<T> Deref for SmallVec<T> {
54+
type Target = [T];
5355

54-
impl<T: PartialEq> PartialEq<SmallVec<T>> for SmallVec<T> {
55-
fn eq(&self, other: &SmallVec<T>) -> bool {
56+
fn deref(&self) -> &Self::Target {
57+
self.as_slice()
58+
}
59+
}
60+
61+
impl<'a, T> IntoIterator for &'a SmallVec<T> {
62+
type Item = &'a T;
63+
64+
type IntoIter = std::slice::Iter<'a, T>;
65+
66+
fn into_iter(self) -> Self::IntoIter {
67+
self.iter()
68+
}
69+
}
70+
71+
impl<T: Eq> Eq for SmallVec<T> {}
72+
73+
impl<T: PartialEq> PartialEq for SmallVec<T> {
74+
fn eq(&self, other: &Self) -> bool {
5675
self.as_slice() == other.as_slice()
5776
}
5877
}

src/range.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl<V: Version> Range<V> {
9393

9494
/// Compute the complement set of versions.
9595
pub fn negate(&self) -> Self {
96-
match self.segments.as_slice().first() {
96+
match self.segments.first() {
9797
None => Self::any(), // Complement of ∅ is *
9898

9999
// First high bound is +∞
@@ -110,9 +110,9 @@ impl<V: Version> Range<V> {
110110
// First high bound is not +∞
111111
Some((v1, Some(v2))) => {
112112
if v1 == &V::lowest() {
113-
Self::negate_segments(v2.clone(), &self.segments.as_slice()[1..])
113+
Self::negate_segments(v2.clone(), &self.segments[1..])
114114
} else {
115-
Self::negate_segments(V::lowest(), &self.segments.as_slice())
115+
Self::negate_segments(V::lowest(), &self.segments)
116116
}
117117
}
118118
}
@@ -125,11 +125,11 @@ impl<V: Version> Range<V> {
125125
fn negate_segments(start: V, segments: &[Interval<V>]) -> Range<V> {
126126
let mut complement_segments = SmallVec::empty();
127127
let mut start = Some(start);
128-
for (v1, some_v2) in segments.iter() {
128+
for (v1, maybe_v2) in segments {
129129
// start.unwrap() is fine because `segments` is not exposed,
130130
// and our usage guaranties that only the last segment may contain a None.
131131
complement_segments.push((start.unwrap(), Some(v1.to_owned())));
132-
start = some_v2.to_owned();
132+
start = maybe_v2.to_owned();
133133
}
134134
if let Some(last) = start {
135135
complement_segments.push((last, None));
@@ -144,7 +144,7 @@ impl<V: Version> Range<V> {
144144

145145
/// Compute the union of two sets of versions.
146146
pub fn union(&self, other: &Self) -> Self {
147-
(self.negate().intersection(&other.negate())).negate()
147+
self.negate().intersection(&other.negate()).negate()
148148
}
149149

150150
/// Compute the intersection of two sets of versions.
@@ -241,8 +241,8 @@ impl<V: Version> Range<V> {
241241
impl<V: Version> Range<V> {
242242
/// Check if a range contains a given version.
243243
pub fn contains(&self, version: &V) -> bool {
244-
for (v1, some_v2) in self.segments.iter() {
245-
match some_v2 {
244+
for (v1, maybe_v2) in &self.segments {
245+
match maybe_v2 {
246246
None => return v1 <= version,
247247
Some(v2) => {
248248
if version < v1 {
@@ -258,11 +258,7 @@ impl<V: Version> Range<V> {
258258

259259
/// Return the lowest version in the range (if there is one).
260260
pub fn lowest_version(&self) -> Option<V> {
261-
self.segments
262-
.as_slice()
263-
.first()
264-
.map(|(start, _)| start)
265-
.cloned()
261+
self.segments.first().map(|(start, _)| start).cloned()
266262
}
267263
}
268264

@@ -288,10 +284,10 @@ impl<V: Version> fmt::Display for Range<V> {
288284
}
289285
}
290286

291-
fn interval_to_string<V: Version>(interval: &Interval<V>) -> String {
292-
match interval {
293-
(start, Some(end)) => format!("[ {}, {} [", start, end),
294-
(start, None) => format!("[ {}, ∞ [", start),
287+
fn interval_to_string<V: Version>((start, maybe_end): &Interval<V>) -> String {
288+
match maybe_end {
289+
Some(end) => format!("[ {}, {} [", start, end),
290+
None => format!("[ {}, ∞ [", start),
295291
}
296292
}
297293

src/report.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! dependency solving failed.
55
66
use std::fmt;
7+
use std::ops::{Deref, DerefMut};
78

89
use crate::package::Package;
910
use crate::range::Range;
@@ -75,7 +76,7 @@ impl<P: Package, V: Version> DerivationTree<P, V> {
7576
match self {
7677
DerivationTree::External(_) => {}
7778
DerivationTree::Derived(derived) => {
78-
match (&mut *derived.cause1, &mut *derived.cause2) {
79+
match (derived.cause1.deref_mut(), derived.cause2.deref_mut()) {
7980
(DerivationTree::External(External::NoVersions(p, r)), ref mut cause2) => {
8081
cause2.collapse_no_versions();
8182
*self = cause2
@@ -208,7 +209,7 @@ impl DefaultStringReporter {
208209
}
209210

210211
fn build_recursive_helper<P: Package, V: Version>(&mut self, current: &Derived<P, V>) {
211-
match (&*current.cause1, &*current.cause2) {
212+
match (current.cause1.deref(), current.cause2.deref()) {
212213
(DerivationTree::External(external1), DerivationTree::External(external2)) => {
213214
// Simplest case, we just combine two external incompatibilities.
214215
self.lines.push(Self::explain_both_external(
@@ -309,7 +310,7 @@ impl DefaultStringReporter {
309310
external: &External<P, V>,
310311
current_terms: &Map<P, Term<V>>,
311312
) {
312-
match (&*derived.cause1, &*derived.cause2) {
313+
match (derived.cause1.deref(), derived.cause2.deref()) {
313314
// If the derived cause has itself one external prior cause,
314315
// we can chain the external explanations.
315316
(DerivationTree::Derived(prior_derived), DerivationTree::External(prior_external)) => {
@@ -478,7 +479,7 @@ impl<P: Package, V: Version> Reporter<P, V> for DefaultStringReporter {
478479
DerivationTree::Derived(derived) => {
479480
let mut reporter = Self::new();
480481
reporter.build_recursive(derived);
481-
reporter.lines[..].join("\n")
482+
reporter.lines.join("\n")
482483
}
483484
}
484485
}

src/term.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'a, V: 'a + Version> Term<V> {
149149
/// Check if a set of terms satisfies or contradicts a given term.
150150
/// Otherwise the relation is inconclusive.
151151
pub(crate) fn relation_with(&self, other_terms_intersection: &Term<V>) -> Relation {
152-
let full_intersection = self.intersection(other_terms_intersection.as_ref());
152+
let full_intersection = self.intersection(other_terms_intersection);
153153
if &full_intersection == other_terms_intersection {
154154
Relation::Satisfied
155155
} else if full_intersection == Self::empty() {

tests/sat_dependency_provider.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ fn sat_at_most_one(solver: &mut impl varisat::ExtendFormula, vars: &[varisat::Va
3434
// https://www.it.uu.se/research/group/astra/ModRef10/papers/Alan%20M.%20Frisch%20and%20Paul%20A.%20Giannoros.%20SAT%20Encodings%20of%20the%20At-Most-k%20Constraint%20-%20ModRef%202010.pdf
3535
let bits: Vec<varisat::Var> = solver.new_var_iter(log_bits(vars.len())).collect();
3636
for (i, p) in vars.iter().enumerate() {
37-
for b in 0..bits.len() {
38-
solver.add_clause(&[p.negative(), bits[b].lit(((1 << b) & i) > 0)]);
37+
for (j, &bit) in bits.iter().enumerate() {
38+
solver.add_clause(&[p.negative(), bit.lit(((1 << j) & i) > 0)]);
3939
}
4040
}
4141
}
@@ -79,7 +79,7 @@ impl<P: Package, V: Version> SatResolve<P, V> {
7979
Dependencies::Unknown => panic!(),
8080
Dependencies::Known(d) => d,
8181
};
82-
for (p1, range) in deps.iter() {
82+
for (p1, range) in &deps {
8383
let empty_vec = vec![];
8484
let mut matches: Vec<varisat::Lit> = all_versions_by_p
8585
.get(&p1)

0 commit comments

Comments
 (0)