graph_traits/
petgraph.rs

1use petgraph::{
2	data::{Build as PetgraphBuild, DataMap as PetgraphDataMap, DataMapMut as PetgraphDataMapMut},
3	visit::{Data as PetgraphData, GraphBase as PetgraphGraphBase},
4};
5
6use petgraph::{
7	graph::{Graph, IndexType},
8	visit::EdgeRef,
9	EdgeType,
10};
11
12use crate::{
13	GraphBase, GraphEdgeAddable, GraphEdgeEndpoints, GraphEdgeFrom, GraphEdgeIndexable,
14	GraphEdgeMutIndexable, GraphEdgeRemovable, GraphEdgeTo, GraphEdgesFrom, GraphNodeAddable,
15	GraphNodeIndexable, GraphNodeMutIndexable, GraphNodeRemovable,
16};
17
18impl<T> GraphBase for T
19where
20	T: PetgraphGraphBase,
21{
22	type NodeID = T::NodeId;
23	type EdgeID = T::EdgeId;
24}
25
26impl<N, T> GraphNodeAddable<N> for T
27where
28	T: PetgraphBuild + PetgraphData<NodeWeight = N>,
29{
30	fn add_node(&mut self, data: N) -> Self::NodeID { self.add_node(data) }
31}
32
33impl<E, T> GraphEdgeAddable<E> for T
34where
35	T: PetgraphBuild + PetgraphData<EdgeWeight = E>,
36{
37	fn add_edge(&mut self, a: Self::NodeID, b: Self::NodeID, data: E) -> Self::EdgeID {
38		self.add_edge(a, b, data).unwrap()
39	}
40}
41
42impl<N, T> GraphNodeIndexable<N> for T
43where
44	T: PetgraphDataMap + PetgraphData<NodeWeight = N>,
45{
46	fn node(&self, id: Self::NodeID) -> &N { self.node_weight(id).unwrap() }
47}
48
49impl<N, T> GraphNodeMutIndexable<N> for T
50where
51	T: PetgraphDataMapMut + PetgraphData<NodeWeight = N>,
52{
53	fn node_mut(&mut self, id: Self::NodeID) -> &mut N { self.node_weight_mut(id).unwrap() }
54}
55
56impl<E, T> GraphEdgeIndexable<E> for T
57where
58	T: PetgraphDataMap + PetgraphData<EdgeWeight = E>,
59{
60	fn edge(&self, id: Self::EdgeID) -> &E { self.edge_weight(id).unwrap() }
61}
62
63impl<E, T> GraphEdgeMutIndexable<E> for T
64where
65	T: PetgraphDataMapMut + PetgraphData<EdgeWeight = E>,
66{
67	fn edge_mut(&mut self, id: Self::EdgeID) -> &mut E { self.edge_weight_mut(id).unwrap() }
68}
69
70// Petgraph does not have some traits which align with some of the traits in this crate therefore we implement per-struct
71
72impl<N, E, Ty, Ix> GraphNodeRemovable<N> for Graph<N, E, Ty, Ix>
73where
74	Ty: EdgeType,
75	Ix: IndexType,
76{
77	fn remove_node(&mut self, id: Self::NodeID) -> N { self.remove_node(id).unwrap() }
78}
79
80impl<N, E, Ty, Ix> GraphEdgeRemovable<E> for Graph<N, E, Ty, Ix>
81where
82	Ty: EdgeType,
83	Ix: IndexType,
84{
85	fn remove_edge(&mut self, id: Self::EdgeID) -> E { self.remove_edge(id).unwrap() }
86}
87
88impl<N, E, Ty, Ix> GraphEdgeTo for Graph<N, E, Ty, Ix>
89where
90	Ty: EdgeType,
91	Ix: IndexType,
92{
93	fn edge_to(&self, id: Self::EdgeID) -> Self::NodeID { self.edge_endpoints(id).unwrap().1 }
94}
95
96impl<N, E, Ty, Ix> GraphEdgeFrom for Graph<N, E, Ty, Ix>
97where
98	Ty: EdgeType,
99	Ix: IndexType,
100{
101	fn edge_from(&self, id: Self::EdgeID) -> Self::NodeID { self.edge_endpoints(id).unwrap().0 }
102}
103
104impl<N, E, Ty, Ix> GraphEdgeEndpoints for Graph<N, E, Ty, Ix>
105where
106	Ty: EdgeType,
107	Ix: IndexType,
108{
109	fn edge_endpoints(&self, id: Self::EdgeID) -> (Self::NodeID, Self::NodeID) {
110		self.edge_endpoints(id).unwrap()
111	}
112}
113
114impl<N, E, Ty, Ix> GraphEdgesFrom for Graph<N, E, Ty, Ix>
115where
116	Ty: EdgeType,
117	Ix: IndexType,
118{
119	type EdgesFromOutput = Vec<Self::EdgeID>;
120
121	fn edges_from(&self, id: Self::NodeID) -> Self::EdgesFromOutput {
122		self.edges(id).map(|edge_ref| edge_ref.id()).collect()
123	}
124}