]> git.lizzy.rs Git - rust.git/blob - src/librustc/dep_graph/prev.rs
17001bbb0c38a4b1362c64391e37af2134014e0b
[rust.git] / src / librustc / dep_graph / prev.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use ich::Fingerprint;
12 use rustc_data_structures::fx::FxHashMap;
13 use super::dep_node::DepNode;
14 use super::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
15
16 #[derive(Debug, RustcEncodable, RustcDecodable)]
17 pub struct PreviousDepGraph {
18     data: SerializedDepGraph,
19     index: FxHashMap<DepNode, SerializedDepNodeIndex>,
20 }
21
22 impl PreviousDepGraph {
23     pub fn new(data: SerializedDepGraph) -> PreviousDepGraph {
24         let index: FxHashMap<_, _> = data.nodes
25             .iter_enumerated()
26             .map(|(idx, &(dep_node, _))| (dep_node, idx))
27             .collect();
28         PreviousDepGraph { data, index }
29     }
30
31     #[inline]
32     pub fn edges_from(&self,
33                       dep_node: &DepNode)
34                       -> Option<(&[SerializedDepNodeIndex], SerializedDepNodeIndex)> {
35         self.index
36             .get(dep_node)
37             .map(|&node_index| {
38                 (self.data.edge_targets_from(node_index), node_index)
39             })
40     }
41
42     #[inline]
43     pub fn index_to_node(&self, dep_node_index: SerializedDepNodeIndex) -> DepNode {
44         self.data.nodes[dep_node_index].0
45     }
46
47     #[inline]
48     pub fn fingerprint_of(&self, dep_node: &DepNode) -> Option<Fingerprint> {
49         self.index
50             .get(dep_node)
51             .map(|&node_index| self.data.nodes[node_index].1)
52     }
53
54     #[inline]
55     pub fn fingerprint_by_index(&self,
56                                 dep_node_index: SerializedDepNodeIndex)
57                                 -> Fingerprint {
58         self.data.nodes[dep_node_index].1
59     }
60 }