Skip to content

Commit 822135d

Browse files
committed
refactor(tree): Manually implement comparison traits
1 parent 2e007b5 commit 822135d

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/cargo/ops/tree/graph.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,43 @@ use crate::util::interning::{InternedString, INTERNED_DEFAULT};
1010
use crate::util::CargoResult;
1111
use std::collections::{HashMap, HashSet};
1212

13-
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
13+
#[derive(Debug, Copy, Clone)]
1414
pub struct NodeId {
1515
index: usize,
1616
}
1717

1818
impl NodeId {
19-
fn with_index(index: usize) -> Self {
19+
fn new(index: usize) -> Self {
2020
Self { index }
2121
}
2222
}
2323

24+
impl PartialEq for NodeId {
25+
fn eq(&self, other: &Self) -> bool {
26+
self.index == other.index
27+
}
28+
}
29+
30+
impl Eq for NodeId {}
31+
32+
impl PartialOrd for NodeId {
33+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
34+
Some(self.cmp(other))
35+
}
36+
}
37+
38+
impl Ord for NodeId {
39+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
40+
self.index.cmp(&other.index)
41+
}
42+
}
43+
44+
impl std::hash::Hash for NodeId {
45+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
46+
self.index.hash(state)
47+
}
48+
}
49+
2450
#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
2551
pub enum Node {
2652
Package {
@@ -134,7 +160,7 @@ impl<'a> Graph<'a> {
134160

135161
/// Adds a new node to the graph, returning its new index.
136162
fn add_node(&mut self, node: Node) -> NodeId {
137-
let from_index = NodeId::with_index(self.nodes.len());
163+
let from_index = NodeId::new(self.nodes.len());
138164
self.nodes.push(node);
139165
self.edges.push(Edges::new());
140166
self.index.insert(self.node(from_index).clone(), from_index);
@@ -178,7 +204,7 @@ impl<'a> Graph<'a> {
178204
Node::Package { package_id, .. } => package_ids.contains(package_id),
179205
_ => false,
180206
})
181-
.map(|(i, node)| (node, NodeId::with_index(i)))
207+
.map(|(i, node)| (node, NodeId::new(i)))
182208
.collect();
183209
// Sort for consistent output (the same command should always return
184210
// the same output). "unstable" since nodes should always be unique.
@@ -252,7 +278,7 @@ impl<'a> Graph<'a> {
252278
for edge in node_edges.all() {
253279
let new_edge = Edge {
254280
kind: edge.kind(),
255-
node: NodeId::with_index(from_idx),
281+
node: NodeId::new(from_idx),
256282
};
257283
new_edges[edge.node().index].add_edge(new_edge);
258284
}
@@ -273,7 +299,7 @@ impl<'a> Graph<'a> {
273299
packages
274300
.entry(package_id.name())
275301
.or_insert_with(Vec::new)
276-
.push((node, NodeId::with_index(i)));
302+
.push((node, NodeId::new(i)));
277303
}
278304
}
279305

@@ -645,7 +671,7 @@ fn add_internal_features(graph: &mut Graph<'_>, resolve: &Resolve) {
645671
Node::Package { .. } => None,
646672
Node::Feature { node_index, name } => {
647673
let package_id = graph.package_id_for_index(*node_index);
648-
Some((package_id, *node_index, NodeId::with_index(i), *name))
674+
Some((package_id, *node_index, NodeId::new(i), *name))
649675
}
650676
})
651677
.collect();

0 commit comments

Comments
 (0)