small refactorings
[hiphop-php.git] / hphp / hack / src / rupro / hackrs_test_utils / registrar.rs
blobdc25d5144aeda95c1884363bb3001f22b45cf4ab
1 // Copyright (c) Facebook, Inc. and its affiliates.
2 //
3 // This source code is licensed under the MIT license found in the
4 // LICENSE file in the "hack" directory of this source tree.
6 use dashmap::{mapref::entry::Entry, DashMap};
7 use depgraph_api::{DeclName, DepGraphWriter, DependencyName, Result};
8 use std::collections::HashSet;
10 pub type DeclNameSet = HashSet<DeclName>;
12 #[derive(Debug, Clone)]
13 pub struct DependencyGraph {
14     // We store the dependency edges in "reverse dependency" fashion (rdeps)
15     // e.g.
16     // ```
17     //   class A {}
18     //   class B extends A {}
19     // ```
20     // would result in
21     //   Extends(A) : {Type(B)}
22     pub rdeps: DashMap<DependencyName, DeclNameSet>,
25 impl DependencyGraph {
26     pub fn new() -> Self {
27         Self {
28             rdeps: DashMap::new(),
29         }
30     }
33 impl DepGraphWriter for DependencyGraph {
34     fn add_dependency(&self, dependent: DeclName, dependency: DependencyName) -> Result<()> {
35         match self.rdeps.entry(dependency) {
36             Entry::Vacant(e) => {
37                 let mut dependents: DeclNameSet = DeclNameSet::new();
38                 dependents.insert(dependent);
39                 e.insert(dependents);
40             }
41             Entry::Occupied(mut e) => {
42                 e.get_mut().insert(dependent);
43             }
44         }
45         Ok(())
46     }