diff --git a/examples/caching_dependency_provider.rs b/examples/caching_dependency_provider.rs index 9332a21b..198aace7 100644 --- a/examples/caching_dependency_provider.rs +++ b/examples/caching_dependency_provider.rs @@ -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; @@ -30,7 +29,7 @@ impl DependencyProvider for CachingDependencyProvider, + version: &DP::V, ) -> Result, DP::Err> { let mut cache = self.cached_dependencies.borrow_mut(); match cache.get_dependencies(package, version) { @@ -54,7 +53,7 @@ impl DependencyProvider for CachingDependencyProvider Result>, DP::Err> { + fn choose_version(&self, package: &DP::P, range: &DP::VS) -> Result, DP::Err> { self.remote_dependencies.choose_version(package, range) } @@ -67,7 +66,7 @@ impl DependencyProvider for CachingDependencyProvider, + version: DP::V, /// Error raised by the implementer of /// [DependencyProvider]. source: DP::Err, @@ -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, + version: DP::V, }, /// Error arising when the implementer of diff --git a/src/internal/core.rs b/src/internal/core.rs index c12bd0cb..476a9c6c 100644 --- a/src/internal/core.rs +++ b/src/internal/core.rs @@ -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 { root_package: DP::P, - root_version: V, + root_version: DP::V, #[allow(clippy::type_complexity)] incompatibilities: Map>>, @@ -53,7 +53,7 @@ pub struct State { impl State { /// Initialization of PubGrub state. - pub fn init(root_package: DP::P, root_version: V) -> 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(), @@ -83,7 +83,7 @@ impl State { pub fn add_incompatibility_from_dependencies( &mut self, package: DP::P, - version: V, + version: DP::V, deps: &DependencyConstraints, ) -> std::ops::Range> { // Create incompatibilities and allocate them in the store. diff --git a/src/internal/partial_solution.rs b/src/internal/partial_solution.rs index 77beade7..f8c10fc5 100644 --- a/src/internal/partial_solution.rs +++ b/src/internal/partial_solution.rs @@ -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; @@ -158,7 +158,7 @@ impl PartialSolution { } /// Add a decision. - pub fn add_decision(&mut self, package: DP::P, version: V) { + 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) { @@ -350,8 +350,8 @@ impl PartialSolution { pub fn add_version( &mut self, package: DP::P, - version: V, - new_incompatibilities: std::ops::Range>, + version: DP::V, + new_incompatibilities: std::ops::Range>, store: &Arena>, ) { let exact = Term::exact(version.clone()); diff --git a/src/lib.rs b/src/lib.rs index bdd28429..72b0d7d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,6 +109,7 @@ //! //! type Err = Infallible; //! type P = String; +//! type V = SemanticVersion; //! type VS = SemVS; //! } //! ``` diff --git a/src/solver.rs b/src/solver.rs index 7197c052..4e469d07 100644 --- a/src/solver.rs +++ b/src/solver.rs @@ -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), @@ -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}; @@ -87,10 +88,10 @@ use log::{debug, info}; pub fn resolve( dependency_provider: &DP, package: DP::P, - version: impl Into>, + version: impl Into, ) -> Result, PubGrubError> { let mut state: State = State::init(package.clone(), version.into()); - let mut added_dependencies: Map>> = Map::default(); + let mut added_dependencies: Map> = Map::default(); let mut next = package; loop { dependency_provider @@ -210,9 +211,13 @@ pub enum Dependencies { 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; /// [Decision making](https://github.com/dart-lang/pub/blob/master/doc/solver.md#decision-making) /// is the process of choosing the next package @@ -260,14 +265,14 @@ pub trait DependencyProvider { &self, package: &Self::P, range: &Self::VS, - ) -> Result>, Self::Err>; + ) -> Result, Self::Err>; /// Retrieves the package dependencies. /// Return [Dependencies::Unknown] if its dependencies are unknown. fn get_dependencies( &self, package: &Self::P, - version: &V, + version: &Self::V, ) -> Result, Self::Err>; /// This is called fairly regularly during the resolution, @@ -354,7 +359,7 @@ impl OfflineDependencyProvider { /// Versions are picked with the newest versions first. impl DependencyProvider for OfflineDependencyProvider { type P = P; - + type V = VS::V; type VS = VS; type Err = Infallible; diff --git a/src/type_aliases.rs b/src/type_aliases.rs index 86846178..1bee1a89 100644 --- a/src/type_aliases.rs +++ b/src/type_aliases.rs @@ -2,9 +2,7 @@ //! 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 = rustc_hash::FxHashMap; @@ -12,12 +10,10 @@ pub type Map = rustc_hash::FxHashMap; /// Set implementation used by the library. pub type Set = rustc_hash::FxHashSet; -/// The version type associated with the version set associated with the dependency provider. -pub type V = <::VS as VersionSet>::V; - /// Concrete dependencies picked by the library during [resolve](crate::solver::resolve) /// from [DependencyConstraints]. -pub type SelectedDependencies = Map<::P, V>; +pub type SelectedDependencies = + Map<::P, ::V>; /// Holds information about all possible versions a given package can accept. /// There is a difference in semantics between an empty map diff --git a/tests/proptest.rs b/tests/proptest.rs index 3043e23c..0d00a3aa 100644 --- a/tests/proptest.rs +++ b/tests/proptest.rs @@ -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}; @@ -56,7 +54,7 @@ impl DependencyProvider for OldestVersionsDependency type Err = Infallible; type P = P; - + type V = VS::V; type VS = VS; } @@ -84,7 +82,7 @@ impl DependencyProvider for TimeoutDependencyProvider, + v: &DP::V, ) -> Result, DP::Err> { self.dp.get_dependencies(p, v) } @@ -97,7 +95,7 @@ impl DependencyProvider for TimeoutDependencyProvider Result>, DP::Err> { + fn choose_version(&self, package: &DP::P, range: &DP::VS) -> Result, DP::Err> { self.dp.choose_version(package, range) } @@ -110,14 +108,14 @@ impl DependencyProvider for TimeoutDependencyProvider::V; type VS = DP::VS; } fn timeout_resolve( dependency_provider: DP, name: DP::P, - version: impl Into>, + version: impl Into, ) -> Result< SelectedDependencies>, PubGrubError>, diff --git a/tests/sat_dependency_provider.rs b/tests/sat_dependency_provider.rs index 1b76c852..f74a2eab 100644 --- a/tests/sat_dependency_provider.rs +++ b/tests/sat_dependency_provider.rs @@ -116,9 +116,9 @@ impl SatResolve { } } - pub fn is_valid_solution( + pub fn is_valid_solution>( &mut self, - pids: &SelectedDependencies>, + pids: &SelectedDependencies, ) -> bool { let mut assumption = vec![]; @@ -136,7 +136,7 @@ impl SatResolve { .expect("docs say it can't error in default config") } - pub fn check_resolve>( + pub fn check_resolve>( &mut self, res: &Result, PubGrubError>, p: &P, @@ -144,7 +144,7 @@ impl SatResolve { ) { match res { Ok(s) => { - assert!(self.is_valid_solution(s)); + assert!(self.is_valid_solution::(s)); } Err(_) => { assert!(!self.resolve(p, v));