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 23, 2024
1 parent 0fc19a6 commit e4b1243
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 e4b1243

Please sign in to comment.