petgraph/graph_impl/
frozen.rs

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