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