Skip to main content

petgraph/graph_impl/
frozen.rs

1use std::ops::{Deref, Index, IndexMut};
2
3use super::Frozen;
4use crate::data::{DataMap, DataMapMut};
5use crate::graph::Graph;
6use crate::graph::{GraphIndex, IndexType};
7use crate::visit::{Data, GraphProp, IntoNeighborsDirected, IntoNodeIdentifiers, NodeIndexable};
8use crate::visit::{
9    GetAdjacencyMatrix, IntoEdges, IntoEdgesDirected, NodeCompactIndexable, NodeCount,
10};
11use crate::visit::{IntoEdgeReferences, IntoNeighbors, IntoNodeReferences, Visitable};
12use crate::{Direction, EdgeType};
13
14impl<'a, G> Frozen<'a, G> {
15    /// Create a new `Frozen` from a mutable reference to a graph.
16    pub fn new(gr: &'a mut G) -> Self {
17        Frozen(gr)
18    }
19}
20
21/// Deref allows transparent access to all shared reference (read-only)
22/// functionality in the underlying graph.
23impl<'a, G> Deref for Frozen<'a, G> {
24    type Target = G;
25    fn deref(&self) -> &G {
26        self.0
27    }
28}
29
30impl<'a, G, I> Index<I> for Frozen<'a, G>
31where
32    G: Index<I>,
33{
34    type Output = G::Output;
35    fn index(&self, i: I) -> &G::Output {
36        self.0.index(i)
37    }
38}
39
40impl<'a, G, I> IndexMut<I> for Frozen<'a, G>
41where
42    G: IndexMut<I>,
43{
44    fn index_mut(&mut self, i: I) -> &mut G::Output {
45        self.0.index_mut(i)
46    }
47}
48
49impl<'a, N, E, Ty, Ix> Frozen<'a, Graph<N, E, Ty, Ix>>
50where
51    Ty: EdgeType,
52    Ix: IndexType,
53{
54    /// Index the `Graph` by two indices, any combination of
55    /// node or edge indices is fine.
56    ///
57    /// **Panics** if the indices are equal or if they are out of bounds.
58    pub fn index_twice_mut<T, U>(
59        &mut self,
60        i: T,
61        j: U,
62    ) -> (
63        &mut <Graph<N, E, Ty, Ix> as Index<T>>::Output,
64        &mut <Graph<N, E, Ty, Ix> as Index<U>>::Output,
65    )
66    where
67        Graph<N, E, Ty, Ix>: IndexMut<T> + IndexMut<U>,
68        T: GraphIndex,
69        U: GraphIndex,
70    {
71        self.0.index_twice_mut(i, j)
72    }
73}
74
75macro_rules! access0 {
76    ($e:expr) => {
77        $e.0
78    };
79}
80
81Data! {delegate_impl [['a, G], G, Frozen<'a, G>, deref_twice]}
82DataMap! {delegate_impl [['a, G], G, Frozen<'a, G>, deref_twice]}
83DataMapMut! {delegate_impl [['a, G], G, Frozen<'a, G>, access0]}
84GetAdjacencyMatrix! {delegate_impl [['a, G], G, Frozen<'a, G>, deref_twice]}
85IntoEdgeReferences! {delegate_impl [['a, 'b, G], G, &'b Frozen<'a, G>, deref_twice]}
86IntoEdges! {delegate_impl [['a, 'b, G], G, &'b Frozen<'a, G>, deref_twice]}
87IntoEdgesDirected! {delegate_impl [['a, 'b, G], G, &'b Frozen<'a, G>, deref_twice]}
88IntoNeighbors! {delegate_impl [['a, 'b, G], G, &'b Frozen<'a, G>, deref_twice]}
89IntoNeighborsDirected! {delegate_impl [['a, 'b, G], G, &'b Frozen<'a, G>, deref_twice]}
90IntoNodeIdentifiers! {delegate_impl [['a, 'b, G], G, &'b Frozen<'a, G>, deref_twice]}
91IntoNodeReferences! {delegate_impl [['a, 'b, G], G, &'b Frozen<'a, G>, deref_twice]}
92NodeCompactIndexable! {delegate_impl [['a, G], G, Frozen<'a, G>, deref_twice]}
93NodeCount! {delegate_impl [['a, G], G, Frozen<'a, G>, deref_twice]}
94NodeIndexable! {delegate_impl [['a, G], G, Frozen<'a, G>, deref_twice]}
95GraphProp! {delegate_impl [['a, G], G, Frozen<'a, G>, deref_twice]}
96Visitable! {delegate_impl [['a, G], G, Frozen<'a, G>, deref_twice]}