-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass an iterator to add_incompatibility_from_dependencies
#226
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,7 +71,7 @@ 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}; | ||
use crate::type_aliases::{Map, SelectedDependencies}; | ||
use crate::version_set::VersionSet; | ||
use log::{debug, info}; | ||
|
||
|
@@ -158,22 +158,22 @@ pub fn resolve<DP: DependencyProvider>( | |
)); | ||
continue; | ||
} | ||
Dependencies::Available(x) if x.contains_key(p) => { | ||
Dependencies::Available(x) if x.clone().into_iter().any(|(d, _)| &d == p) => { | ||
return Err(PubGrubError::SelfDependency { | ||
package: p.clone(), | ||
version: v, | ||
version: v.clone(), | ||
}); | ||
} | ||
Dependencies::Available(x) => x, | ||
}; | ||
|
||
// Add that package and version if the dependencies are not problematic. | ||
let dep_incompats = | ||
state.add_incompatibility_from_dependencies(p.clone(), v.clone(), &dependencies); | ||
state.add_incompatibility_from_dependencies(p.clone(), v.clone(), dependencies); | ||
|
||
state.partial_solution.add_version( | ||
p.clone(), | ||
v, | ||
v.clone(), | ||
dep_incompats, | ||
&state.incompatibility_store, | ||
); | ||
|
@@ -189,11 +189,11 @@ pub fn resolve<DP: DependencyProvider>( | |
/// An enum used by [DependencyProvider] that holds information about package dependencies. | ||
/// For each [Package] there is a set of versions allowed as a dependency. | ||
#[derive(Clone)] | ||
pub enum Dependencies<P: Package, VS: VersionSet, M: Eq + Clone + Debug + Display> { | ||
pub enum Dependencies<T, M: Eq + Clone + Debug + Display> { | ||
/// Package dependencies are unavailable with the reason why they are missing. | ||
Unavailable(M), | ||
/// Container for all available package versions. | ||
Available(DependencyConstraints<P, VS>), | ||
Available(T), | ||
} | ||
|
||
/// Trait that allows the algorithm to retrieve available packages and their dependencies. | ||
|
@@ -280,7 +280,10 @@ pub trait DependencyProvider { | |
&self, | ||
package: &Self::P, | ||
version: &Self::V, | ||
) -> Result<Dependencies<Self::P, Self::VS, Self::M>, Self::Err>; | ||
) -> Result< | ||
Dependencies<impl IntoIterator<Item = (Self::P, Self::VS)> + Clone, Self::M>, | ||
Self::Err, | ||
>; | ||
|
||
/// This is called fairly regularly during the resolution, | ||
/// if it returns an Err then resolution will be terminated. | ||
|
@@ -304,7 +307,7 @@ pub trait DependencyProvider { | |
)] | ||
#[cfg_attr(feature = "serde", serde(transparent))] | ||
pub struct OfflineDependencyProvider<P: Package, VS: VersionSet> { | ||
dependencies: Map<P, BTreeMap<VS::V, DependencyConstraints<P, VS>>>, | ||
dependencies: Map<P, BTreeMap<VS::V, Map<P, VS>>>, | ||
} | ||
|
||
impl<P: Package, VS: VersionSet> OfflineDependencyProvider<P, VS> { | ||
|
@@ -354,8 +357,8 @@ impl<P: Package, VS: VersionSet> OfflineDependencyProvider<P, VS> { | |
|
||
/// Lists dependencies of a given package and version. | ||
/// Returns [None] if no information is available regarding that package and version pair. | ||
fn dependencies(&self, package: &P, version: &VS::V) -> Option<DependencyConstraints<P, VS>> { | ||
self.dependencies.get(package)?.get(version).cloned() | ||
fn dependencies(&self, package: &P, version: &VS::V) -> Option<&Map<P, VS>> { | ||
self.dependencies.get(package)?.get(version) | ||
} | ||
} | ||
|
||
|
@@ -393,12 +396,15 @@ impl<P: Package, VS: VersionSet> DependencyProvider for OfflineDependencyProvide | |
&self, | ||
package: &P, | ||
version: &VS::V, | ||
) -> Result<Dependencies<P, VS, Self::M>, Infallible> { | ||
) -> Result< | ||
Dependencies<impl IntoIterator<Item = (Self::P, Self::VS)> + Clone, Self::M>, | ||
Self::Err, | ||
> { | ||
Ok(match self.dependencies(package, version) { | ||
None => { | ||
Dependencies::Unavailable("its dependencies could not be determined".to_string()) | ||
} | ||
Some(dependencies) => Dependencies::Available(dependencies), | ||
Some(dependencies) => Dependencies::Available(dependencies.clone()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: The allocation in this clone can be removed by making it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thx i accidentally worsened this because i missed that this isn't |
||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The allocations in these collect calls make me sad. This cache hit case is exactly where I was hoping to avoid allocations by accepting an iterator. My most recent attempt got a little closer to something elegant, but with its own set of trade-offs.
Fundamentally were hitting https://smallcultfollowing.com/babysteps/blog/2018/09/02/rust-pattern-iterating-an-over-a-rc-vec-t/ I think the unstable
gen
blocks may be an ergonomic fix for this, but I have not succeeded at trying it in my own projects.