Skip to content

Commit

Permalink
Implement ops::Index with PackageId for Resolve graph
Browse files Browse the repository at this point in the history
While iterating through dependency packages, I find myself needing to
look up dependency nodes recursively through the graph, in the same
way that I need `Package` information for its `PackageId` via the
already-existing `ops::Index` implementation on `Metadata`.  This is
simpler if the indexing operation is also available on a `Resolve`d
graph.
  • Loading branch information
MarijnS95 committed May 22, 2024
1 parent 6d527db commit 2f059b4
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl Metadata {
impl<'a> std::ops::Index<&'a PackageId> for Metadata {
type Output = Package;

fn index(&self, idx: &'a PackageId) -> &Package {
fn index(&self, idx: &'a PackageId) -> &Self::Output {
self.packages
.iter()
.find(|p| p.id == *idx)
Expand Down Expand Up @@ -272,6 +272,17 @@ pub struct Resolve {
pub root: Option<PackageId>,
}

impl<'a> std::ops::Index<&'a PackageId> for Resolve {
type Output = Node;

fn index(&self, idx: &'a PackageId) -> &Self::Output {
self.nodes
.iter()
.find(|p| p.id == *idx)
.unwrap_or_else(|| panic!("no Node with this id: {:?}", idx))
}
}

#[derive(Clone, Serialize, Deserialize, Debug)]
#[cfg_attr(feature = "builder", derive(Builder))]
#[non_exhaustive]
Expand Down

0 comments on commit 2f059b4

Please sign in to comment.