Skip to content

Commit 749abfa

Browse files
konstinEh2406zaniebcharliermarsh
authored
Pass an iterator to add_incompatibility_from_dependencies (#226)
* Relax dependencies to iterator instead of a hashmap to avoid clones Previously, `Dependencies::Available` was hardcoded to a hash map. By relaxing this to `impl IntoIterator<_> + Clone`, we avoid converting from uv's ordered `Vec<(_, _)>` to a `HashMap<_, _>` and avoid cloning. ## Design considerations We implement this using the return type `Dependencies<impl IntoIterator<Item = (DP::P, DP::VS)> + Clone, DP::M>`. This allows using `get_dependencies` without knowing the actual (which a generic bound would require) or adding another associated type to the dependency provider. The `impl` bound also captures all input lifetimes, keeping `p` and `v` borrowed. We could bind the iterator to only `&self` instead, but this would only move two clones (package and version) to the implementer. Co-authored-by: Jacob Finkelman <[email protected]> Co-authored-by: Zanie <[email protected]> Co-authored-by: Charlie Marsh <[email protected]> * Avoid clone * Don't change `DependencyProvider` * Avoid changing the public API for the dependencies internal iterator --------- Co-authored-by: Jacob Finkelman <[email protected]> Co-authored-by: Zanie <[email protected]> Co-authored-by: Charlie Marsh <[email protected]>
1 parent f041854 commit 749abfa

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

src/internal/core.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::internal::partial_solution::{DecisionLevel, PartialSolution};
1616
use crate::internal::small_vec::SmallVec;
1717
use crate::report::DerivationTree;
1818
use crate::solver::DependencyProvider;
19-
use crate::type_aliases::{DependencyConstraints, IncompDpId, Map};
19+
use crate::type_aliases::{IncompDpId, Map};
2020
use crate::version_set::VersionSet;
2121

2222
/// Current state of the PubGrub algorithm.
@@ -84,12 +84,12 @@ impl<DP: DependencyProvider> State<DP> {
8484
&mut self,
8585
package: DP::P,
8686
version: DP::V,
87-
deps: &DependencyConstraints<DP::P, DP::VS>,
87+
deps: impl IntoIterator<Item = (DP::P, DP::VS)>,
8888
) -> std::ops::Range<IncompDpId<DP>> {
8989
// Create incompatibilities and allocate them in the store.
9090
let new_incompats_id_range =
9191
self.incompatibility_store
92-
.alloc_iter(deps.iter().map(|dep| {
92+
.alloc_iter(deps.into_iter().map(|dep| {
9393
Incompatibility::from_dependency(
9494
package.clone(),
9595
<DP::VS as VersionSet>::singleton(version.clone()),

src/internal/incompatibility.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,18 @@ impl<P: Package, VS: VersionSet, M: Eq + Clone + Debug + Display> Incompatibilit
137137
}
138138

139139
/// Build an incompatibility from a given dependency.
140-
pub fn from_dependency(package: P, versions: VS, dep: (&P, &VS)) -> Self {
140+
pub fn from_dependency(package: P, versions: VS, dep: (P, VS)) -> Self {
141141
let (p2, set2) = dep;
142142
Self {
143-
package_terms: if set2 == &VS::empty() {
143+
package_terms: if set2 == VS::empty() {
144144
SmallMap::One([(package.clone(), Term::Positive(versions.clone()))])
145145
} else {
146146
SmallMap::Two([
147147
(package.clone(), Term::Positive(versions.clone())),
148148
(p2.clone(), Term::Negative(set2.clone())),
149149
])
150150
},
151-
kind: Kind::FromDependencyOf(package, versions, p2.clone(), set2.clone()),
151+
kind: Kind::FromDependencyOf(package, versions, p2, set2),
152152
}
153153
}
154154

@@ -190,7 +190,10 @@ impl<P: Package, VS: VersionSet, M: Eq + Clone + Debug + Display> Incompatibilit
190190
.unwrap()
191191
.unwrap_positive()
192192
.union(other.get(p1).unwrap().unwrap_positive()), // It is safe to `simplify` here
193-
(p2, dep_term.map_or(&VS::empty(), |v| v.unwrap_negative())),
193+
(
194+
p2.clone(),
195+
dep_term.map_or(VS::empty(), |v| v.unwrap_negative().clone()),
196+
),
194197
));
195198
}
196199

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
//! # use pubgrub::solver::{DependencyProvider, Dependencies};
7474
//! # use pubgrub::version::SemanticVersion;
7575
//! # use pubgrub::range::Range;
76-
//! # use pubgrub::type_aliases::Map;
76+
//! # use pubgrub::type_aliases::{DependencyConstraints, Map};
7777
//! # use std::error::Error;
7878
//! # use std::borrow::Borrow;
7979
//! # use std::convert::Infallible;
@@ -97,7 +97,7 @@
9797
//! package: &String,
9898
//! version: &SemanticVersion,
9999
//! ) -> Result<Dependencies<String, SemVS, Self::M>, Infallible> {
100-
//! unimplemented!()
100+
//! Ok(Dependencies::Available(DependencyConstraints::default()))
101101
//! }
102102
//!
103103
//! type Err = Infallible;

src/solver.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ pub fn resolve<DP: DependencyProvider>(
169169

170170
// Add that package and version if the dependencies are not problematic.
171171
let dep_incompats =
172-
state.add_incompatibility_from_dependencies(p.clone(), v.clone(), &dependencies);
172+
state.add_incompatibility_from_dependencies(p.clone(), v.clone(), dependencies);
173173

174174
state.partial_solution.add_version(
175175
p.clone(),
176-
v,
176+
v.clone(),
177177
dep_incompats,
178178
&state.incompatibility_store,
179179
);

0 commit comments

Comments
 (0)