Skip to content

Commit

Permalink
mabe V as well?
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Mar 14, 2024
1 parent ee226c3 commit 58f786b
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 44 deletions.
7 changes: 3 additions & 4 deletions examples/caching_dependency_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::cell::RefCell;

use pubgrub::range::Range;
use pubgrub::solver::{resolve, Dependencies, DependencyProvider, OfflineDependencyProvider};
use pubgrub::type_aliases::V;
use pubgrub::version::NumberVersion;

type NumVS = Range<NumberVersion>;
Expand All @@ -30,7 +29,7 @@ impl<DP: DependencyProvider> DependencyProvider for CachingDependencyProvider<DP
fn get_dependencies(
&self,
package: &DP::P,
version: &V<DP>,
version: &DP::V,
) -> Result<Dependencies<DP::P, DP::VS>, DP::Err> {
let mut cache = self.cached_dependencies.borrow_mut();
match cache.get_dependencies(package, version) {
Expand All @@ -54,7 +53,7 @@ impl<DP: DependencyProvider> DependencyProvider for CachingDependencyProvider<DP
}
}

fn choose_version(&self, package: &DP::P, range: &DP::VS) -> Result<Option<V<DP>>, DP::Err> {
fn choose_version(&self, package: &DP::P, range: &DP::VS) -> Result<Option<DP::V>, DP::Err> {
self.remote_dependencies.choose_version(package, range)
}

Expand All @@ -67,7 +66,7 @@ impl<DP: DependencyProvider> DependencyProvider for CachingDependencyProvider<DP
type Err = DP::Err;

type P = DP::P;

type V = DP::V;
type VS = DP::VS;
}

Expand Down
5 changes: 2 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use thiserror::Error;

use crate::report::DerivationTree;
use crate::solver::DependencyProvider;
use crate::type_aliases::V;

/// Errors that may occur while solving dependencies.
#[derive(Error)]
Expand All @@ -27,7 +26,7 @@ where
/// Package whose dependencies we want.
package: DP::P,
/// Version of the package for which we want the dependencies.
version: V<DP>,
version: DP::V,
/// Error raised by the implementer of
/// [DependencyProvider].
source: DP::Err,
Expand All @@ -43,7 +42,7 @@ where
/// Package whose dependencies we want.
package: DP::P,
/// Version of the package for which we want the dependencies.
version: V<DP>,
version: DP::V,
},

/// Error arising when the implementer of
Expand Down
8 changes: 4 additions & 4 deletions src/internal/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ use crate::internal::partial_solution::{DecisionLevel, PartialSolution};
use crate::internal::small_vec::SmallVec;
use crate::report::DerivationTree;
use crate::solver::DependencyProvider;
use crate::type_aliases::{DependencyConstraints, IncompDpId, Map, V};
use crate::type_aliases::{DependencyConstraints, IncompDpId, Map};
use crate::version_set::VersionSet;

/// Current state of the PubGrub algorithm.
#[derive(Clone)]
pub struct State<DP: DependencyProvider> {
root_package: DP::P,
root_version: V<DP>,
root_version: DP::V,

#[allow(clippy::type_complexity)]
incompatibilities: Map<DP::P, Vec<IncompDpId<DP>>>,
Expand Down Expand Up @@ -53,7 +53,7 @@ pub struct State<DP: DependencyProvider> {

impl<DP: DependencyProvider> State<DP> {
/// Initialization of PubGrub state.
pub fn init(root_package: DP::P, root_version: V<DP>) -> Self {
pub fn init(root_package: DP::P, root_version: DP::V) -> Self {
let mut incompatibility_store = Arena::new();
let not_root_id = incompatibility_store.alloc(Incompatibility::not_root(
root_package.clone(),
Expand Down Expand Up @@ -83,7 +83,7 @@ impl<DP: DependencyProvider> State<DP> {
pub fn add_incompatibility_from_dependencies(
&mut self,
package: DP::P,
version: V<DP>,
version: DP::V,
deps: &DependencyConstraints<DP::P, DP::VS>,
) -> std::ops::Range<IncompDpId<DP>> {
// Create incompatibilities and allocate them in the store.
Expand Down
8 changes: 4 additions & 4 deletions src/internal/partial_solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::internal::small_map::SmallMap;
use crate::package::Package;
use crate::solver::DependencyProvider;
use crate::term::Term;
use crate::type_aliases::{IncompDpId, SelectedDependencies, V};
use crate::type_aliases::{IncompDpId, SelectedDependencies};
use crate::version_set::VersionSet;

use super::small_vec::SmallVec;
Expand Down Expand Up @@ -158,7 +158,7 @@ impl<DP: DependencyProvider> PartialSolution<DP> {
}

/// Add a decision.
pub fn add_decision(&mut self, package: DP::P, version: V<DP>) {
pub fn add_decision(&mut self, package: DP::P, version: DP::V) {
// Check that add_decision is never used in the wrong context.
if cfg!(debug_assertions) {
match self.package_assignments.get_mut(&package) {
Expand Down Expand Up @@ -350,8 +350,8 @@ impl<DP: DependencyProvider> PartialSolution<DP> {
pub fn add_version(
&mut self,
package: DP::P,
version: V<DP>,
new_incompatibilities: std::ops::Range<IncompDpId<DP>>,
version: DP::V,
new_incompatibilities: std::ops::Range<IncompId<DP::P, DP::VS>>,
store: &Arena<Incompatibility<DP::P, DP::VS>>,
) {
let exact = Term::exact(version.clone());
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
//!
//! type Err = Infallible;
//! type P = String;
//! type V = SemanticVersion;
//! type VS = SemVS;
//! }
//! ```
Expand Down
23 changes: 14 additions & 9 deletions src/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//! as long as packages (P) and versions (V) implement
//! the [Package] and [Version](crate::version::Version) traits.
//! [Package] is strictly equivalent and automatically generated
//! for any type that implement [Clone] + [Eq] + [Hash] + [Debug] + [Display](std::fmt::Display).
//! for any type that implement [Clone] + [Eq] + [Hash] + [Debug] + [Display].
//! [Version](crate::version::Version) simply states that versions are ordered,
//! that there should be
//! a minimal [lowest](crate::version::Version::lowest) version (like 0.0.0 in semantic versions),
Expand Down Expand Up @@ -73,12 +73,13 @@ use std::cmp::Reverse;
use std::collections::{BTreeMap, BTreeSet as Set};
use std::convert::Infallible;
use std::error::Error;
use std::fmt::{Debug, Display};

use crate::error::PubGrubError;
use crate::internal::core::State;
use crate::internal::incompatibility::Incompatibility;
use crate::package::Package;
use crate::type_aliases::{DependencyConstraints, Map, SelectedDependencies, V};
use crate::type_aliases::{DependencyConstraints, Map, SelectedDependencies};
use crate::version_set::VersionSet;
use log::{debug, info};

Expand All @@ -87,10 +88,10 @@ use log::{debug, info};
pub fn resolve<DP: DependencyProvider>(
dependency_provider: &DP,
package: DP::P,
version: impl Into<V<DP>>,
version: impl Into<DP::V>,
) -> Result<SelectedDependencies<DP>, PubGrubError<DP>> {
let mut state: State<DP> = State::init(package.clone(), version.into());
let mut added_dependencies: Map<DP::P, Set<V<DP>>> = Map::default();
let mut added_dependencies: Map<DP::P, Set<DP::V>> = Map::default();
let mut next = package;
loop {
dependency_provider
Expand Down Expand Up @@ -210,9 +211,13 @@ pub enum Dependencies<P: Package, VS: VersionSet> {
pub trait DependencyProvider {
/// How this provider stores the name of the packages.
type P: Package;

/// How this provider stores the vertions of the packages.
type V: Debug + Display + Clone + Ord;

/// How this provider stores the version requirements for the packages.
/// This also specifies the representation of a version.
type VS: VersionSet;
/// The requirements must be able to process the same kind of version as this dependency provider.
type VS: VersionSet<V = Self::V>;

/// [Decision making](https://github.com/dart-lang/pub/blob/master/doc/solver.md#decision-making)
/// is the process of choosing the next package
Expand Down Expand Up @@ -260,14 +265,14 @@ pub trait DependencyProvider {
&self,
package: &Self::P,
range: &Self::VS,
) -> Result<Option<V<Self>>, Self::Err>;
) -> Result<Option<Self::V>, Self::Err>;

/// Retrieves the package dependencies.
/// Return [Dependencies::Unknown] if its dependencies are unknown.
fn get_dependencies(
&self,
package: &Self::P,
version: &V<Self>,
version: &Self::V,
) -> Result<Dependencies<Self::P, Self::VS>, Self::Err>;

/// This is called fairly regularly during the resolution,
Expand Down Expand Up @@ -354,7 +359,7 @@ impl<P: Package, VS: VersionSet> OfflineDependencyProvider<P, VS> {
/// Versions are picked with the newest versions first.
impl<P: Package, VS: VersionSet> DependencyProvider for OfflineDependencyProvider<P, VS> {
type P = P;

type V = VS::V;
type VS = VS;

type Err = Infallible;
Expand Down
10 changes: 3 additions & 7 deletions src/type_aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@

//! Publicly exported type aliases.
use crate::{
internal::incompatibility::IncompId, solver::DependencyProvider, version_set::VersionSet,
};
use crate::{internal::incompatibility::IncompId, solver::DependencyProvider};

/// Map implementation used by the library.
pub type Map<K, V> = rustc_hash::FxHashMap<K, V>;

/// Set implementation used by the library.
pub type Set<V> = rustc_hash::FxHashSet<V>;

/// The version type associated with the version set associated with the dependency provider.
pub type V<DP> = <<DP as DependencyProvider>::VS as VersionSet>::V;

/// Concrete dependencies picked by the library during [resolve](crate::solver::resolve)
/// from [DependencyConstraints].
pub type SelectedDependencies<DP> = Map<<DP as DependencyProvider>::P, V<DP>>;
pub type SelectedDependencies<DP> =
Map<<DP as DependencyProvider>::P, <DP as DependencyProvider>::V>;

/// Holds information about all possible versions a given package can accept.
/// There is a difference in semantics between an empty map
Expand Down
16 changes: 7 additions & 9 deletions tests/proptest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ use pubgrub::package::Package;
use pubgrub::range::Range;
use pubgrub::report::{DefaultStringReporter, DerivationTree, External, Reporter};
use pubgrub::solver::{resolve, Dependencies, DependencyProvider, OfflineDependencyProvider};
use pubgrub::type_aliases::{SelectedDependencies, V};
use pubgrub::version::NumberVersion;
#[cfg(feature = "serde")]
use pubgrub::version::SemanticVersion;
use pubgrub::type_aliases::SelectedDependencies;
use pubgrub::version::{NumberVersion, SemanticVersion};
use pubgrub::version_set::VersionSet;

use proptest::collection::{btree_map, btree_set, vec};
Expand Down Expand Up @@ -56,7 +54,7 @@ impl<P: Package, VS: VersionSet> DependencyProvider for OldestVersionsDependency
type Err = Infallible;

type P = P;

type V = VS::V;
type VS = VS;
}

Expand Down Expand Up @@ -84,7 +82,7 @@ impl<DP: DependencyProvider> DependencyProvider for TimeoutDependencyProvider<DP
fn get_dependencies(
&self,
p: &DP::P,
v: &V<Self>,
v: &DP::V,
) -> Result<Dependencies<DP::P, DP::VS>, DP::Err> {
self.dp.get_dependencies(p, v)
}
Expand All @@ -97,7 +95,7 @@ impl<DP: DependencyProvider> DependencyProvider for TimeoutDependencyProvider<DP
Ok(())
}

fn choose_version(&self, package: &DP::P, range: &DP::VS) -> Result<Option<V<Self>>, DP::Err> {
fn choose_version(&self, package: &DP::P, range: &DP::VS) -> Result<Option<DP::V>, DP::Err> {
self.dp.choose_version(package, range)
}

Expand All @@ -110,14 +108,14 @@ impl<DP: DependencyProvider> DependencyProvider for TimeoutDependencyProvider<DP
type Err = DP::Err;

type P = DP::P;

type V = <DP::VS as VersionSet>::V;
type VS = DP::VS;
}

fn timeout_resolve<DP: DependencyProvider>(
dependency_provider: DP,
name: DP::P,
version: impl Into<V<DP>>,
version: impl Into<DP::V>,
) -> Result<
SelectedDependencies<TimeoutDependencyProvider<DP>>,
PubGrubError<TimeoutDependencyProvider<DP>>,
Expand Down
8 changes: 4 additions & 4 deletions tests/sat_dependency_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ impl<P: Package, VS: VersionSet> SatResolve<P, VS> {
}
}

pub fn is_valid_solution(
pub fn is_valid_solution<DP: DependencyProvider<P = P, VS = VS, V = VS::V>>(
&mut self,
pids: &SelectedDependencies<OfflineDependencyProvider<P, VS>>,
pids: &SelectedDependencies<DP>,
) -> bool {
let mut assumption = vec![];

Expand All @@ -136,15 +136,15 @@ impl<P: Package, VS: VersionSet> SatResolve<P, VS> {
.expect("docs say it can't error in default config")
}

pub fn check_resolve<DP: DependencyProvider<P = P, VS = VS>>(
pub fn check_resolve<DP: DependencyProvider<P = P, VS = VS, V = VS::V>>(
&mut self,
res: &Result<SelectedDependencies<DP>, PubGrubError<DP>>,
p: &P,
v: &VS::V,
) {
match res {
Ok(s) => {
assert!(self.is_valid_solution(s));
assert!(self.is_valid_solution::<DP>(s));
}
Err(_) => {
assert!(!self.resolve(p, v));
Expand Down

0 comments on commit 58f786b

Please sign in to comment.