Skip to content

Commit 05e5d6e

Browse files
committed
refactor(tree): Abstact edges for feature
1 parent 0fd5de0 commit 05e5d6e

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

src/cargo/ops/tree/graph.rs

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -456,43 +456,27 @@ fn add_pkg(
456456
requested_kind,
457457
opts,
458458
);
459+
let new_edge = Edge {
460+
kind: EdgeKind::Dep(dep.kind()),
461+
node: dep_index,
462+
};
459463
if opts.graph_features {
460464
// Add the dependency node with feature nodes in-between.
461465
dep_name_map
462466
.entry(dep.name_in_toml())
463467
.or_default()
464468
.insert((dep_index, dep.is_optional()));
465469
if dep.uses_default_features() {
466-
add_feature(
467-
graph,
468-
INTERNED_DEFAULT,
469-
Some(from_index),
470-
dep_index,
471-
EdgeKind::Dep(dep.kind()),
472-
);
470+
add_feature(graph, INTERNED_DEFAULT, Some(from_index), new_edge);
473471
}
474472
for feature in dep.features().iter() {
475-
add_feature(
476-
graph,
477-
*feature,
478-
Some(from_index),
479-
dep_index,
480-
EdgeKind::Dep(dep.kind()),
481-
);
473+
add_feature(graph, *feature, Some(from_index), new_edge);
482474
}
483475
if !dep.uses_default_features() && dep.features().is_empty() {
484476
// No features, use a direct connection.
485-
let new_edge = Edge {
486-
kind: EdgeKind::Dep(dep.kind()),
487-
node: dep_index,
488-
};
489477
graph.edges[from_index].add_edge(new_edge);
490478
}
491479
} else {
492-
let new_edge = Edge {
493-
kind: EdgeKind::Dep(dep.kind()),
494-
node: dep_index,
495-
};
496480
graph.edges[from_index].add_edge(new_edge);
497481
}
498482
}
@@ -522,13 +506,12 @@ fn add_feature(
522506
graph: &mut Graph<'_>,
523507
name: InternedString,
524508
from: Option<usize>,
525-
to: usize,
526-
kind: EdgeKind,
509+
to: Edge,
527510
) -> (bool, usize) {
528511
// `to` *must* point to a package node.
529-
assert!(matches! {graph.nodes[to], Node::Package{..}});
512+
assert!(matches! {graph.nodes[to.node()], Node::Package{..}});
530513
let node = Node::Feature {
531-
node_index: to,
514+
node_index: to.node(),
532515
name,
533516
};
534517
let (missing, node_index) = match graph.index.get(&node) {
@@ -537,14 +520,14 @@ fn add_feature(
537520
};
538521
if let Some(from) = from {
539522
let from_edge = Edge {
540-
kind,
523+
kind: to.kind(),
541524
node: node_index,
542525
};
543526
graph.edges[from].add_edge(from_edge);
544527
}
545528
let to_edge = Edge {
546529
kind: EdgeKind::Feature,
547-
node: to,
530+
node: to.node(),
548531
};
549532
graph.edges[node_index].add_edge(to_edge);
550533
(missing, node_index)
@@ -579,7 +562,11 @@ fn add_cli_features(
579562
for fv in to_add {
580563
match fv {
581564
FeatureValue::Feature(feature) => {
582-
let index = add_feature(graph, feature, None, package_index, EdgeKind::Feature).1;
565+
let feature_edge = Edge {
566+
kind: EdgeKind::Feature,
567+
node: package_index,
568+
};
569+
let index = add_feature(graph, feature, None, feature_edge).1;
583570
graph.cli_features.insert(index);
584571
}
585572
// This is enforced by CliFeatures.
@@ -609,12 +596,18 @@ fn add_cli_features(
609596
for (dep_index, is_optional) in dep_connections {
610597
if is_optional {
611598
// Activate the optional dep on self.
612-
let index =
613-
add_feature(graph, dep_name, None, package_index, EdgeKind::Feature).1;
599+
let feature_edge = Edge {
600+
kind: EdgeKind::Feature,
601+
node: package_index,
602+
};
603+
let index = add_feature(graph, dep_name, None, feature_edge).1;
614604
graph.cli_features.insert(index);
615605
}
616-
let index =
617-
add_feature(graph, dep_feature, None, dep_index, EdgeKind::Feature).1;
606+
let dep_edge = Edge {
607+
kind: EdgeKind::Feature,
608+
node: dep_index,
609+
};
610+
let index = add_feature(graph, dep_feature, None, dep_edge).1;
618611
graph.cli_features.insert(index);
619612
}
620613
}
@@ -670,13 +663,11 @@ fn add_feature_rec(
670663
for fv in fvs {
671664
match fv {
672665
FeatureValue::Feature(dep_name) => {
673-
let (missing, feat_index) = add_feature(
674-
graph,
675-
*dep_name,
676-
Some(from),
677-
package_index,
678-
EdgeKind::Feature,
679-
);
666+
let feature_edge = Edge {
667+
kind: EdgeKind::Feature,
668+
node: package_index,
669+
};
670+
let (missing, feat_index) = add_feature(graph, *dep_name, Some(from), feature_edge);
680671
// Don't recursive if the edge already exists to deal with cycles.
681672
if missing {
682673
add_feature_rec(
@@ -722,21 +713,18 @@ fn add_feature_rec(
722713
let dep_pkg_id = graph.package_id_for_index(dep_index);
723714
if is_optional && !weak {
724715
// Activate the optional dep on self.
725-
add_feature(
726-
graph,
727-
*dep_name,
728-
Some(from),
729-
package_index,
730-
EdgeKind::Feature,
731-
);
716+
let feature_edge = Edge {
717+
kind: EdgeKind::Feature,
718+
node: package_index,
719+
};
720+
add_feature(graph, *dep_name, Some(from), feature_edge);
732721
}
733-
let (missing, feat_index) = add_feature(
734-
graph,
735-
*dep_feature,
736-
Some(from),
737-
dep_index,
738-
EdgeKind::Feature,
739-
);
722+
let dep_edge = Edge {
723+
kind: EdgeKind::Feature,
724+
node: dep_index,
725+
};
726+
let (missing, feat_index) =
727+
add_feature(graph, *dep_feature, Some(from), dep_edge);
740728
if missing {
741729
add_feature_rec(
742730
graph,

0 commit comments

Comments
 (0)