Skip to content

Commit

Permalink
feat: Extend error type to Send + Sync (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Oct 19, 2023
1 parent 879fd6b commit b14fa78
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
4 changes: 2 additions & 2 deletions examples/caching_dependency_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<P: Package, VS: VersionSet, DP: DependencyProvider<P, VS>> DependencyProvid
fn choose_package_version<T: std::borrow::Borrow<P>, U: std::borrow::Borrow<VS>>(
&self,
packages: impl Iterator<Item = (T, U)>,
) -> Result<(T, Option<VS::V>), Box<dyn Error>> {
) -> Result<(T, Option<VS::V>), Box<dyn Error + Send + Sync>> {
self.remote_dependencies.choose_package_version(packages)
}

Expand All @@ -44,7 +44,7 @@ impl<P: Package, VS: VersionSet, DP: DependencyProvider<P, VS>> DependencyProvid
&self,
package: &P,
version: &VS::V,
) -> Result<Dependencies<P, VS>, Box<dyn Error>> {
) -> Result<Dependencies<P, VS>, Box<dyn Error + Send + Sync>> {
let mut cache = self.cached_dependencies.borrow_mut();
match cache.get_dependencies(package, version) {
Ok(Dependencies::Unknown) => {
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum PubGrubError<P: Package, VS: VersionSet> {
version: VS::V,
/// Error raised by the implementer of
/// [DependencyProvider](crate::solver::DependencyProvider).
source: Box<dyn std::error::Error>,
source: Box<dyn std::error::Error + Send + Sync>,
},

/// Error arising when the implementer of
Expand Down Expand Up @@ -63,12 +63,12 @@ pub enum PubGrubError<P: Package, VS: VersionSet> {
/// returned an error in the method
/// [choose_package_version](crate::solver::DependencyProvider::choose_package_version).
#[error("Decision making failed")]
ErrorChoosingPackageVersion(Box<dyn std::error::Error>),
ErrorChoosingPackageVersion(Box<dyn std::error::Error + Send + Sync>),

/// Error arising when the implementer of [DependencyProvider](crate::solver::DependencyProvider)
/// returned an error in the method [should_cancel](crate::solver::DependencyProvider::should_cancel).
#[error("We should cancel")]
ErrorInShouldCancel(Box<dyn std::error::Error>),
ErrorInShouldCancel(Box<dyn std::error::Error + Send + Sync>),

/// Something unexpected happened.
#[error("{0}")]
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@
//! type SemVS = Range<SemanticVersion>;
//!
//! impl DependencyProvider<String, SemVS> for MyDependencyProvider {
//! fn choose_package_version<T: Borrow<String>, U: Borrow<SemVS>>(&self,packages: impl Iterator<Item=(T, U)>) -> Result<(T, Option<SemanticVersion>), Box<dyn Error>> {
//! fn choose_package_version<T: Borrow<String>, U: Borrow<SemVS>>(&self,packages: impl Iterator<Item=(T, U)>) -> Result<(T, Option<SemanticVersion>), Box<dyn Error + Send + Sync>> {
//! unimplemented!()
//! }
//!
//! fn get_dependencies(
//! &self,
//! package: &String,
//! version: &SemanticVersion,
//! ) -> Result<Dependencies<String, SemVS>, Box<dyn Error>> {
//! ) -> Result<Dependencies<String, SemVS>, Box<dyn Error + Send + Sync>> {
//! unimplemented!()
//! }
//! }
Expand Down
10 changes: 5 additions & 5 deletions src/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,22 +259,22 @@ pub trait DependencyProvider<P: Package, VS: VersionSet> {
fn choose_package_version<T: Borrow<P>, U: Borrow<VS>>(
&self,
potential_packages: impl Iterator<Item = (T, U)>,
) -> Result<(T, Option<VS::V>), Box<dyn Error>>;
) -> Result<(T, Option<VS::V>), Box<dyn Error + Send + Sync>>;

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

/// This is called fairly regularly during the resolution,
/// if it returns an Err then resolution will be terminated.
/// This is helpful if you want to add some form of early termination like a timeout,
/// or you want to add some form of user feedback if things are taking a while.
/// If not provided the resolver will run as long as needed.
fn should_cancel(&self) -> Result<(), Box<dyn Error>> {
fn should_cancel(&self) -> Result<(), Box<dyn Error + Send + Sync>> {
Ok(())
}
}
Expand Down Expand Up @@ -384,7 +384,7 @@ impl<P: Package, VS: VersionSet> DependencyProvider<P, VS> for OfflineDependency
fn choose_package_version<T: Borrow<P>, U: Borrow<VS>>(
&self,
potential_packages: impl Iterator<Item = (T, U)>,
) -> Result<(T, Option<VS::V>), Box<dyn Error>> {
) -> Result<(T, Option<VS::V>), Box<dyn Error + Send + Sync>> {
Ok(choose_package_with_fewest_versions(
|p| {
self.dependencies
Expand All @@ -402,7 +402,7 @@ impl<P: Package, VS: VersionSet> DependencyProvider<P, VS> for OfflineDependency
&self,
package: &P,
version: &VS::V,
) -> Result<Dependencies<P, VS>, Box<dyn Error>> {
) -> Result<Dependencies<P, VS>, Box<dyn Error + Send + Sync>> {
Ok(match self.dependencies(package, version) {
None => Dependencies::Unknown,
Some(dependencies) => Dependencies::Known(dependencies),
Expand Down
18 changes: 13 additions & 5 deletions tests/proptest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ impl<P: Package, VS: VersionSet> DependencyProvider<P, VS>
fn choose_package_version<T: std::borrow::Borrow<P>, U: std::borrow::Borrow<VS>>(
&self,
potential_packages: impl Iterator<Item = (T, U)>,
) -> Result<(T, Option<VS::V>), Box<dyn Error>> {
) -> Result<(T, Option<VS::V>), Box<dyn Error + Send + Sync>> {
Ok(choose_package_with_fewest_versions(
|p| self.0.versions(p).into_iter().flatten().cloned(),
potential_packages,
))
}

fn get_dependencies(&self, p: &P, v: &VS::V) -> Result<Dependencies<P, VS>, Box<dyn Error>> {
fn get_dependencies(
&self,
p: &P,
v: &VS::V,
) -> Result<Dependencies<P, VS>, Box<dyn Error + Send + Sync>> {
self.0.get_dependencies(p, v)
}
}
Expand Down Expand Up @@ -73,15 +77,19 @@ impl<P: Package, VS: VersionSet, DP: DependencyProvider<P, VS>> DependencyProvid
fn choose_package_version<T: std::borrow::Borrow<P>, U: std::borrow::Borrow<VS>>(
&self,
potential_packages: impl Iterator<Item = (T, U)>,
) -> Result<(T, Option<VS::V>), Box<dyn Error>> {
) -> Result<(T, Option<VS::V>), Box<dyn Error + Send + Sync>> {
self.dp.choose_package_version(potential_packages)
}

fn get_dependencies(&self, p: &P, v: &VS::V) -> Result<Dependencies<P, VS>, Box<dyn Error>> {
fn get_dependencies(
&self,
p: &P,
v: &VS::V,
) -> Result<Dependencies<P, VS>, Box<dyn Error + Send + Sync>> {
self.dp.get_dependencies(p, v)
}

fn should_cancel(&self) -> Result<(), Box<dyn Error>> {
fn should_cancel(&self) -> Result<(), Box<dyn Error + Send + Sync>> {
assert!(self.start_time.elapsed().as_secs() < 60);
let calls = self.call_count.get();
assert!(calls < self.max_calls);
Expand Down

0 comments on commit b14fa78

Please sign in to comment.