1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//! Capability traits that describe what operations a graph backend supports.
//!
//! These traits are **not intended for direct use by library users.** The
//! [`Graph`](super::Graph) facade re-exposes every operation with
//! index-checking assertions and ergonomic signatures. Prefer calling
//! methods on `Graph` (e.g. [`Graph::insert_node`](super::Graph::insert_node),
//! [`Graph::take_node`](super::Graph::take_node)) rather than invoking
//! these traits directly.
//!
//! Graph backends implement the capability traits that match their storage
//! guarantees; the `Graph` blanket impl then wires the safe convenience
//! layer on top.
use Borrow;
use ;
use GraphProperty;
/// Mutable access to node data and incident-edge traversal with mutable edge
/// references.
///
/// Use [`Graph::walks_from_mut`](super::Graph::walks_from_mut) /
/// [`Graph::walks_of_mut`](super::Graph::walks_of_mut) instead of calling
/// these methods directly.
/// Mutable access to edge data.
///
/// Use [`Graph::edge_mut`](super::Graph::edge_mut) (index-checked) or
/// [`Graph::edge_unchecked_mut`](super::Graph::edge_unchecked_mut) instead
/// of calling this trait directly.
/// Marker: node indices are stable across mutations.
///
/// A `NodeIx` returned by this graph always refers to the same node for
/// the lifetime of the graph. Removing a node invalidates only that index
/// (`contains_node_index` returns `false`); all other node indices remain
/// valid.
///
/// Map-backed graphs (e.g. `BTreeGraph`, `HashGraph`) and
/// [`Stabilized`](super::stabilized::Stabilized) implement this
/// automatically. Vec-backed graphs do not, because swap-remove can
/// relocate the last element. Use
/// [`Graph::scope`](super::Graph::scope) or
/// [`Graph::unsafe_assert_stable_node`](super::Graph::unsafe_assert_stable_node)
/// to satisfy `StableNode` bounds on non-stable graphs.
///
/// # Safety
/// Implementor must guarantee that a `NodeIx` always refers to the same
/// node for the lifetime of the graph. If the node has been removed,
/// `contains_node_index()` must return `false`.
pub unsafe
/// Marker: edge indices are stable across mutations.
///
/// An `EdgeIx` returned by this graph always refers to the same edge for
/// the lifetime of the graph. Removing an edge invalidates only that index
/// (`contains_edge_index` returns `false`); all other edge indices remain
/// valid.
///
/// See [`StableNode`] for the analogous node guarantee and the list of
/// graphs that implement it. Use
/// [`Graph::unsafe_assert_stable_edge`](super::Graph::unsafe_assert_stable_edge)
/// to satisfy this bound on non-stable graphs.
///
/// # Safety
/// Implementor must guarantee that an `EdgeIx` always refers to the same
/// edge for the lifetime of the graph. If the edge has been removed,
/// `contains_edge_index()` must return `false`.
pub unsafe
/// Ability to insert new nodes into the graph.
///
/// Use [`Graph::insert_node`](super::Graph::insert_node) (index-checked)
/// or [`Graph::push`](super::Graph::push) (discards the returned index)
/// instead of calling this trait directly.
///
/// Insertion returns `Err(node)` when the graph rejects the value (e.g.
/// [`UniqueNode`] graphs that already contain an equal node).
/// Ability to insert new edges into the graph.
///
/// Use [`Graph::insert_edge`](super::Graph::insert_edge) (index-checked)
/// or [`Graph::push_edge`](super::Graph::push_edge) (discards the returned
/// index) instead of calling this trait directly.
///
/// Insertion returns `Err(edge)` when the graph rejects the value (e.g.
/// [`UniqueEdge`] graphs that already contain an equal edge).
/// Node values are unique: at most one node holds any given value.
///
/// This enables value-to-index lookup. Use
/// [`Graph::get_or_insert_node`](super::Graph::get_or_insert_node) instead
/// of calling [`node_index`](UniqueNode::node_index) directly.
///
/// Requires [`StableNode`] because the returned index must remain valid
/// for the caller to use.
///
/// Implemented automatically by map-backed graphs (`BTreeGraph`,
/// `HashGraph`) where node values serve as map keys.
/// Edge values are unique: at most one edge holds any given value.
///
/// This enables value-to-index lookup. Use
/// [`Graph::get_or_insert_edge`](super::Graph::get_or_insert_edge) instead
/// of calling [`edge_index`](UniqueEdge::edge_index) directly.
///
/// Requires [`StableEdge`] because the returned index must remain valid
/// for the caller to use.
///
/// Implemented automatically by map-backed graphs (`BTreeGraph`,
/// `HashGraph`) where edge values serve as map keys.
// ---- Directed / Bigraph ----
/// A directed graph: edges have a source (tail) and target (head)
/// distinction.
///
/// Use the [`Graph`](super::Graph) convenience methods instead of calling
/// this trait directly:
/// [`walks_to`](super::Graph::walks_to),
/// [`edge_indices_to`](super::Graph::edge_indices_to),
/// [`edge_tail_indices`](super::Graph::edge_tail_indices),
/// [`edge_head_indices`](super::Graph::edge_head_indices).
/// A bigraph (binary graph): each edge connects exactly two nodes.
///
/// Combined with [`Directed`], enables single-node
/// [`Graph::edge_head`](super::Graph::edge_head) /
/// [`Graph::edge_tail`](super::Graph::edge_tail) accessors on the
/// [`Graph`](super::Graph) facade.
/// Ability to remove nodes (and their incident edges) from the graph.
///
/// Use [`Graph::take_node`](super::Graph::take_node) (index-checked,
/// returns data) or
/// [`Graph::remove_node`](super::Graph::remove_node) (index-checked,
/// discards data) instead of calling this trait directly.
///
/// Removing a node always removes all edges incident to it, so this trait
/// requires [`RemoveEdge`].
/// Ability to remove edges from the graph.
///
/// Use [`Graph::take_edge`](super::Graph::take_edge) (index-checked,
/// returns data) or
/// [`Graph::remove_edge`](super::Graph::remove_edge) (index-checked,
/// discards data) instead of calling this trait directly.