diff --git a/crates/pep508-rs/src/marker/tree.rs b/crates/pep508-rs/src/marker/tree.rs index b0518f880..510c547b2 100644 --- a/crates/pep508-rs/src/marker/tree.rs +++ b/crates/pep508-rs/src/marker/tree.rs @@ -1308,8 +1308,14 @@ impl ExtraMarkerTree<'_> { pub struct MarkerTreeContents(MarkerTree); impl From for MarkerTree { - fn from(value: MarkerTreeContents) -> Self { - value.0 + fn from(contents: MarkerTreeContents) -> Self { + contents.0 + } +} + +impl From> for MarkerTree { + fn from(marker: Option) -> Self { + marker.map(|contents| contents.0).unwrap_or_default() } } diff --git a/crates/uv-resolver/src/lock.rs b/crates/uv-resolver/src/lock.rs index 6e36e8c4e..c029afab0 100644 --- a/crates/uv-resolver/src/lock.rs +++ b/crates/uv-resolver/src/lock.rs @@ -141,7 +141,7 @@ impl Lock { continue; }; let marker = edge.weight().clone(); - locked_dist.add_dependency(dependency_dist, marker.unwrap_or_default()); + locked_dist.add_dependency(dependency_dist, marker); } let id = locked_dist.id.clone(); if let Some(locked_dist) = locked_dists.insert(id, locked_dist) { @@ -173,11 +173,7 @@ impl Lock { continue; }; let marker = edge.weight().clone(); - locked_dist.add_optional_dependency( - extra.clone(), - dependency_dist, - marker.unwrap_or_default(), - ); + locked_dist.add_optional_dependency(extra.clone(), dependency_dist, marker); } } if let Some(group) = dist.dev.as_ref() { @@ -195,11 +191,7 @@ impl Lock { continue; }; let marker = edge.weight().clone(); - locked_dist.add_dev_dependency( - group.clone(), - dependency_dist, - marker.unwrap_or_default(), - ); + locked_dist.add_dev_dependency(group.clone(), dependency_dist, marker); } } } diff --git a/crates/uv-resolver/src/resolution/display.rs b/crates/uv-resolver/src/resolution/display.rs index 656fb5bab..1436060d7 100644 --- a/crates/uv-resolver/src/resolution/display.rs +++ b/crates/uv-resolver/src/resolution/display.rs @@ -307,13 +307,13 @@ pub enum AnnotationStyle { } type ResolutionPetGraph = - petgraph::graph::Graph, petgraph::Directed>; + petgraph::graph::Graph; type IntermediatePetGraph = - petgraph::graph::Graph, petgraph::Directed>; + petgraph::graph::Graph; type RequirementsTxtGraph = - petgraph::graph::Graph, petgraph::Directed>; + petgraph::graph::Graph; /// Convert a [`petgraph::graph::Graph`] based on [`ResolutionGraphNode`] to a graph based on /// [`DisplayResolutionGraphNode`]. @@ -384,10 +384,10 @@ fn propagate_markers(mut graph: IntermediatePetGraph) -> IntermediatePetGraph { let mut edges = graph.edges_directed(index, Direction::Incoming); edges .next() - .and_then(|edge| graph.edge_weight(edge.id()).cloned().flatten()) + .and_then(|edge| graph.edge_weight(edge.id()).cloned()) .and_then(|initial| { edges.try_fold(initial, |mut acc, edge| { - acc.or(graph.edge_weight(edge.id())?.clone()?); + acc.or(graph.edge_weight(edge.id())?.clone()); Some(acc) }) }) @@ -400,11 +400,7 @@ fn propagate_markers(mut graph: IntermediatePetGraph) -> IntermediatePetGraph { .detach(); while let Some((outgoing, _)) = walker.next(&graph) { if let Some(weight) = graph.edge_weight_mut(outgoing) { - if let Some(weight) = weight { - weight.and(marker_tree.clone()); - } else { - *weight = Some(marker_tree.clone()); - } + weight.and(marker_tree.clone()); } } } @@ -417,7 +413,7 @@ fn propagate_markers(mut graph: IntermediatePetGraph) -> IntermediatePetGraph { // Re-add the removed edges. We no longer care about the edge _weights_, but we do want the // edges to be present, to power the `# via` annotations. for (source, target) in edges { - graph.add_edge(source, target, None); + graph.add_edge(source, target, MarkerTree::TRUE); } graph @@ -464,16 +460,16 @@ fn combine_extras(graph: &IntermediatePetGraph) -> RequirementsTxtGraph { let source = inverse[&source_node.version_id()]; let target = inverse[&target_node.version_id()]; - // If either the existing marker or new marker is `None`, then the dependency is - // included unconditionally, and so the combined marker should be `None`. + // If either the existing marker or new marker is `true`, then the dependency is + // included unconditionally, and so the combined marker should be `true`. if let Some(edge) = next .find_edge(source, target) .and_then(|edge| next.edge_weight_mut(edge)) { - if let (Some(marker), Some(ref version_marker)) = (edge.as_mut(), weight) { - marker.and(version_marker.clone()); + if edge.is_true() || weight.is_true() { + *edge = MarkerTree::TRUE; } else { - *edge = None; + edge.and(weight.clone()); } } else { next.update_edge(source, target, weight); diff --git a/crates/uv-resolver/src/resolution/graph.rs b/crates/uv-resolver/src/resolution/graph.rs index 77c18753d..58edc14f1 100644 --- a/crates/uv-resolver/src/resolution/graph.rs +++ b/crates/uv-resolver/src/resolution/graph.rs @@ -40,7 +40,7 @@ pub(crate) type MarkersForDistribution = #[derive(Debug)] pub struct ResolutionGraph { /// The underlying graph. - pub(crate) petgraph: Graph, Directed>, + pub(crate) petgraph: Graph, /// The range of supported Python versions. pub(crate) requires_python: Option, /// If the resolution had non-identical forks, store the forks in the lockfile so we can @@ -91,7 +91,7 @@ impl ResolutionGraph { options: Options, ) -> Result { let size_guess = resolutions[0].nodes.len(); - let mut petgraph: Graph, Directed> = + let mut petgraph: Graph = Graph::with_capacity(size_guess, size_guess); let mut inverse: FxHashMap> = FxHashMap::with_capacity_and_hasher(size_guess, FxBuildHasher); @@ -163,12 +163,10 @@ impl ResolutionGraph { // Normalize any markers. if let Some(ref requires_python) = requires_python { for edge in petgraph.edge_indices() { - if let Some(marker) = petgraph[edge].take() { - petgraph[edge] = Some(marker.simplify_python_versions( - Range::from(requires_python.bound_major_minor().clone()), - Range::from(requires_python.bound().clone()), - )); - } + petgraph[edge] = petgraph[edge].clone().simplify_python_versions( + Range::from(requires_python.bound_major_minor().clone()), + Range::from(requires_python.bound().clone()), + ); } } @@ -214,7 +212,7 @@ impl ResolutionGraph { } fn add_edge( - petgraph: &mut Graph>, + petgraph: &mut Graph, inverse: &mut FxHashMap, NodeIndex>, root_index: NodeIndex, edge: &ResolutionDependencyEdge, @@ -240,21 +238,16 @@ impl ResolutionGraph { .find_edge(from_index, to_index) .and_then(|edge| petgraph.edge_weight_mut(edge)) { - // If either the existing marker or new marker is `None`, then the dependency is - // included unconditionally, and so the combined marker should be `None`. - if let (Some(marker), Some(ref version_marker)) = (marker.as_mut(), edge.marker.clone()) - { - marker.or(version_marker.clone()); - } else { - *marker = None; - } + // If either the existing marker or new marker is `true`, then the dependency is + // included unconditionally, and so the combined marker is `true`. + marker.or(edge.marker.clone()); } else { petgraph.update_edge(from_index, to_index, edge.marker.clone()); } } fn add_version<'a>( - petgraph: &mut Graph>, + petgraph: &mut Graph, inverse: &mut FxHashMap, NodeIndex>, diagnostics: &mut Vec, preferences: &Preferences, @@ -660,7 +653,7 @@ impl From for distribution_types::Resolution { /// Find any packages that don't have any lower bound on them when in resolution-lowest mode. fn report_missing_lower_bounds( - petgraph: &Graph>, + petgraph: &Graph, diagnostics: &mut Vec, ) { for node_index in petgraph.node_indices() { @@ -687,7 +680,7 @@ fn report_missing_lower_bounds( fn has_lower_bound( node_index: NodeIndex, package_name: &PackageName, - petgraph: &Graph>, + petgraph: &Graph, ) -> bool { for neighbor_index in petgraph.neighbors_directed(node_index, Direction::Incoming) { let neighbor_dist = match petgraph.node_weight(neighbor_index).unwrap() { diff --git a/crates/uv-resolver/src/resolver/mod.rs b/crates/uv-resolver/src/resolver/mod.rs index ea899bbb0..dff08438f 100644 --- a/crates/uv-resolver/src/resolver/mod.rs +++ b/crates/uv-resolver/src/resolver/mod.rs @@ -670,7 +670,7 @@ impl ResolverState, pub(crate) to_extra: Option, pub(crate) to_dev: Option, - pub(crate) marker: Option, + pub(crate) marker: MarkerTree, } impl Resolution {