Skip to content

Commit c22dd96

Browse files
committed
Expose internal interfaces used by uv
This wrapper avoids accessing the `incompatibility_store` directly in uv code. Before: ```rust let dep_incompats = self.pubgrub.add_version( package.clone(), version.clone(), dependencies, ); self.pubgrub.partial_solution.add_version( package.clone(), version.clone(), dep_incompats, &self.pubgrub.incompatibility_store, ); ``` After: ```rust self.pubgrub.add_incompatibility_from_dependencies(package.clone(), version.clone(), dependencies); ``` `add_incompatibility_from_dependencies` is one of the main methods for the custom interface to pubgrub.
1 parent 05b0934 commit c22dd96

File tree

7 files changed

+39
-31
lines changed

7 files changed

+39
-31
lines changed

src/internal/arena.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type FnvIndexSet<V> = indexmap::IndexSet<V, rustc_hash::FxBuildHasher>;
1212
/// that we actually don't need since it is phantom.
1313
///
1414
/// <https://github.com/rust-lang/rust/issues/26925>
15-
pub(crate) struct Id<T> {
15+
pub struct Id<T> {
1616
raw: u32,
1717
_ty: PhantomData<fn() -> T>,
1818
}
@@ -73,7 +73,7 @@ impl<T> Id<T> {
7373
/// to have references between those items.
7474
/// They are all dropped at once when the arena is dropped.
7575
#[derive(Clone, PartialEq, Eq)]
76-
pub(crate) struct Arena<T> {
76+
pub struct Arena<T> {
7777
data: Vec<T>,
7878
}
7979

src/internal/core.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ use crate::{DependencyProvider, DerivationTree, Map, NoSolutionError, VersionSet
1414

1515
/// Current state of the PubGrub algorithm.
1616
#[derive(Clone)]
17-
pub(crate) struct State<DP: DependencyProvider> {
17+
pub struct State<DP: DependencyProvider> {
18+
/// The root package and version.
1819
pub root_package: Id<DP::P>,
1920
root_version: DP::V,
2021

22+
/// All incompatibilities indexed by package.
2123
#[allow(clippy::type_complexity)]
22-
incompatibilities: Map<Id<DP::P>, Vec<IncompDpId<DP>>>,
24+
pub incompatibilities: Map<Id<DP::P>, Vec<IncompDpId<DP>>>,
2325

2426
/// As an optimization, store the ids of incompatibilities that are already contradicted.
2527
///
@@ -33,14 +35,13 @@ pub(crate) struct State<DP: DependencyProvider> {
3335
merged_dependencies: Map<(Id<DP::P>, Id<DP::P>), SmallVec<IncompDpId<DP>>>,
3436

3537
/// Partial solution.
36-
/// TODO: remove pub.
37-
pub(crate) partial_solution: PartialSolution<DP>,
38+
pub partial_solution: PartialSolution<DP>,
3839

3940
/// The store is the reference storage for all incompatibilities.
40-
pub(crate) incompatibility_store: Arena<Incompatibility<DP::P, DP::VS, DP::M>>,
41+
pub incompatibility_store: Arena<Incompatibility<DP::P, DP::VS, DP::M>>,
4142

4243
/// The store is the reference storage for all packages.
43-
pub(crate) package_store: HashArena<DP::P>,
44+
pub package_store: HashArena<DP::P>,
4445

4546
/// This is a stack of work to be done in `unit_propagation`.
4647
/// It can definitely be a local variable to that method, but
@@ -50,7 +51,7 @@ pub(crate) struct State<DP: DependencyProvider> {
5051

5152
impl<DP: DependencyProvider> State<DP> {
5253
/// Initialization of PubGrub state.
53-
pub(crate) fn init(root_package: DP::P, root_version: DP::V) -> Self {
54+
pub fn init(root_package: DP::P, root_version: DP::V) -> Self {
5455
let mut incompatibility_store = Arena::new();
5556
let mut package_store = HashArena::new();
5657
let root_package = package_store.alloc(root_package);
@@ -74,7 +75,7 @@ impl<DP: DependencyProvider> State<DP> {
7475
}
7576

7677
/// Add the dependencies for the current version of the current package as incompatibilities.
77-
pub(crate) fn add_package_version_dependencies(
78+
pub fn add_package_version_dependencies(
7879
&mut self,
7980
package: Id<DP::P>,
8081
version: DP::V,
@@ -91,7 +92,7 @@ impl<DP: DependencyProvider> State<DP> {
9192
}
9293

9394
/// Add an incompatibility to the state.
94-
pub(crate) fn add_incompatibility(&mut self, incompat: Incompatibility<DP::P, DP::VS, DP::M>) {
95+
pub fn add_incompatibility(&mut self, incompat: Incompatibility<DP::P, DP::VS, DP::M>) {
9596
let id = self.incompatibility_store.alloc(incompat);
9697
self.merge_incompatibility(id);
9798
}
@@ -129,7 +130,7 @@ impl<DP: DependencyProvider> State<DP> {
129130
/// incompatibility.
130131
#[cold]
131132
#[allow(clippy::type_complexity)] // Type definitions don't support impl trait.
132-
pub(crate) fn unit_propagation(
133+
pub fn unit_propagation(
133134
&mut self,
134135
package: Id<DP::P>,
135136
) -> Result<SmallVec<(Id<DP::P>, IncompDpId<DP>)>, NoSolutionError<DP>> {

src/internal/incompatibility.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ use crate::{
2828
/// during conflict resolution. More about all this in
2929
/// [PubGrub documentation](https://github.com/dart-lang/pub/blob/master/doc/solver.md#incompatibility).
3030
#[derive(Debug, Clone)]
31-
pub(crate) struct Incompatibility<P: Package, VS: VersionSet, M: Eq + Clone + Debug + Display> {
31+
pub struct Incompatibility<P: Package, VS: VersionSet, M: Eq + Clone + Debug + Display> {
3232
package_terms: SmallMap<Id<P>, Term<VS>>,
33-
kind: Kind<P, VS, M>,
33+
/// The reason for the incompatibility.
34+
pub kind: Kind<P, VS, M>,
3435
}
3536

3637
/// Type alias of unique identifiers for incompatibilities.
@@ -42,8 +43,9 @@ pub(crate) type IncompDpId<DP> = IncompId<
4243
<DP as DependencyProvider>::M,
4344
>;
4445

46+
/// The reason for the incompatibility.
4547
#[derive(Debug, Clone)]
46-
enum Kind<P: Package, VS: VersionSet, M: Eq + Clone + Debug + Display> {
48+
pub enum Kind<P: Package, VS: VersionSet, M: Eq + Clone + Debug + Display> {
4749
/// Initial incompatibility aiming at picking the root package for the first decision.
4850
///
4951
/// This incompatibility drives the resolution, it requires that we pick the (virtual) root
@@ -104,7 +106,7 @@ impl<P: Package, VS: VersionSet, M: Eq + Clone + Debug + Display> Incompatibilit
104106
}
105107

106108
/// Create an incompatibility to remember that a given set does not contain any version.
107-
pub(crate) fn no_versions(package: Id<P>, term: Term<VS>) -> Self {
109+
pub fn no_versions(package: Id<P>, term: Term<VS>) -> Self {
108110
let set = match &term {
109111
Term::Positive(r) => r.clone(),
110112
Term::Negative(_) => panic!("No version should have a positive term"),
@@ -117,7 +119,7 @@ impl<P: Package, VS: VersionSet, M: Eq + Clone + Debug + Display> Incompatibilit
117119

118120
/// Create an incompatibility for a reason outside pubgrub.
119121
#[allow(dead_code)] // Used by uv
120-
pub(crate) fn custom_term(package: Id<P>, term: Term<VS>, metadata: M) -> Self {
122+
pub fn custom_term(package: Id<P>, term: Term<VS>, metadata: M) -> Self {
121123
let set = match &term {
122124
Term::Positive(r) => r.clone(),
123125
Term::Negative(_) => panic!("No version should have a positive term"),
@@ -129,7 +131,7 @@ impl<P: Package, VS: VersionSet, M: Eq + Clone + Debug + Display> Incompatibilit
129131
}
130132

131133
/// Create an incompatibility for a reason outside pubgrub.
132-
pub(crate) fn custom_version(package: Id<P>, version: VS::V, metadata: M) -> Self {
134+
pub fn custom_version(package: Id<P>, version: VS::V, metadata: M) -> Self {
133135
let set = VS::singleton(version);
134136
let term = Term::Positive(set.clone());
135137
Self {
@@ -139,7 +141,7 @@ impl<P: Package, VS: VersionSet, M: Eq + Clone + Debug + Display> Incompatibilit
139141
}
140142

141143
/// Build an incompatibility from a given dependency.
142-
pub(crate) fn from_dependency(package: Id<P>, versions: VS, dep: (Id<P>, VS)) -> Self {
144+
pub fn from_dependency(package: Id<P>, versions: VS, dep: (Id<P>, VS)) -> Self {
143145
let (p2, set2) = dep;
144146
Self {
145147
package_terms: if set2 == VS::empty() {
@@ -345,6 +347,7 @@ impl<'a, P: Package, VS: VersionSet + 'a, M: Eq + Clone + Debug + Display + 'a>
345347
}
346348

347349
impl<P: Package, VS: VersionSet, M: Eq + Clone + Debug + Display> Incompatibility<P, VS, M> {
350+
/// Display the incompatibility.
348351
pub fn display<'a>(&'a self, package_store: &'a HashArena<P>) -> impl Display + 'a {
349352
match self.iter().collect::<Vec<_>>().as_slice() {
350353
[] => "version solving failed".into(),

src/internal/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ mod partial_solution;
99
mod small_map;
1010
mod small_vec;
1111

12-
pub(crate) use arena::{Arena, HashArena, Id};
13-
pub(crate) use core::State;
14-
pub(crate) use incompatibility::{IncompDpId, IncompId, Incompatibility, Relation};
12+
pub(crate) use arena::{Arena, HashArena};
13+
pub(crate) use incompatibility::{IncompDpId, IncompId, Relation};
1514
pub(crate) use partial_solution::{DecisionLevel, PartialSolution, SatisfierSearch};
1615
pub(crate) use small_map::SmallMap;
1716
pub(crate) use small_vec::SmallVec;
17+
18+
// uv-specific additions
19+
pub use arena::Id;
20+
pub use core::State;
21+
pub use incompatibility::{Incompatibility, Kind};

src/internal/partial_solution.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl DecisionLevel {
3030
/// The partial solution contains all package assignments,
3131
/// organized by package and historically ordered.
3232
#[derive(Clone, Debug)]
33-
pub(crate) struct PartialSolution<DP: DependencyProvider> {
33+
pub struct PartialSolution<DP: DependencyProvider> {
3434
next_global_index: u32,
3535
/// The number of decisions that have been made, equal to the number of packages with decisions.
3636
current_decision_level: DecisionLevel,
@@ -202,7 +202,7 @@ impl<DP: DependencyProvider> PartialSolution<DP> {
202202
}
203203

204204
/// Add a decision.
205-
pub(crate) fn add_decision(&mut self, package: Id<DP::P>, version: DP::V) {
205+
pub fn add_decision(&mut self, package: Id<DP::P>, version: DP::V) {
206206
// Check that add_decision is never used in the wrong context.
207207
if cfg!(debug_assertions) {
208208
match self.package_assignments.get_mut(&package) {
@@ -294,7 +294,7 @@ impl<DP: DependencyProvider> PartialSolution<DP> {
294294
}
295295

296296
#[cold]
297-
pub(crate) fn pick_highest_priority_pkg(
297+
pub fn pick_highest_priority_pkg(
298298
&mut self,
299299
mut prioritizer: impl FnMut(Id<DP::P>, &DP::VS) -> DP::Priority,
300300
) -> Option<(Id<DP::P>, &DP::VS)> {
@@ -323,7 +323,7 @@ impl<DP: DependencyProvider> PartialSolution<DP> {
323323
/// If a partial solution has, for every positive derivation,
324324
/// a corresponding decision that satisfies that assignment,
325325
/// it's a total solution and version solving has succeeded.
326-
pub(crate) fn extract_solution(&self) -> impl Iterator<Item = (Id<DP::P>, DP::V)> + '_ {
326+
pub fn extract_solution(&self) -> impl Iterator<Item = (Id<DP::P>, DP::V)> + '_ {
327327
self.package_assignments
328328
.iter()
329329
.take(self.current_decision_level.0 as usize)
@@ -465,10 +465,7 @@ impl<DP: DependencyProvider> PartialSolution<DP> {
465465
}
466466

467467
/// Retrieve intersection of terms related to package.
468-
pub(crate) fn term_intersection_for_package(
469-
&self,
470-
package: Id<DP::P>,
471-
) -> Option<&Term<DP::VS>> {
468+
pub fn term_intersection_for_package(&self, package: Id<DP::P>) -> Option<&Term<DP::VS>> {
472469
self.package_assignments
473470
.get(&package)
474471
.map(|pa| pa.assignments_intersection.term())

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,7 @@ pub use version_ranges::Ranges;
238238
pub use version_ranges::Ranges as Range;
239239
pub use version_set::VersionSet;
240240

241+
// uv-specific additions
242+
pub use internal::{Id, Incompatibility, Kind, State};
243+
241244
mod internal;

src/term.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<VS: VersionSet> Term<VS> {
7474

7575
/// Unwrap the set contained in a positive term.
7676
/// Will panic if used on a negative set.
77-
pub(crate) fn unwrap_positive(&self) -> &VS {
77+
pub fn unwrap_positive(&self) -> &VS {
7878
match self {
7979
Self::Positive(set) => set,
8080
_ => panic!("Negative term cannot unwrap positive set"),

0 commit comments

Comments
 (0)