1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use diem_types::access_path::AccessPath;
use move_core_types::{
account_address::AccountAddress,
language_storage::{ModuleId, StructTag},
};
use std::collections::btree_map::{self, BTreeMap};
pub trait AccessPathCache {
fn get_module_path(&mut self, module_id: ModuleId) -> AccessPath;
fn get_resource_path(&mut self, address: AccountAddress, struct_tag: StructTag) -> AccessPath;
}
impl AccessPathCache for () {
fn get_module_path(&mut self, module_id: ModuleId) -> AccessPath {
AccessPath::from(&module_id)
}
fn get_resource_path(&mut self, address: AccountAddress, struct_tag: StructTag) -> AccessPath {
AccessPath::new(address, struct_tag.access_vector())
}
}
#[derive(Clone)]
pub struct BTreeAccessPathCache {
modules: BTreeMap<ModuleId, Vec<u8>>,
resources: BTreeMap<StructTag, Vec<u8>>,
}
impl AccessPathCache for BTreeAccessPathCache {
fn get_module_path(&mut self, module_id: ModuleId) -> AccessPath {
let addr = *module_id.address();
let access_vec = match self.modules.entry(module_id) {
btree_map::Entry::Vacant(entry) => {
let v = entry.key().access_vector();
entry.insert(v).clone()
}
btree_map::Entry::Occupied(entry) => entry.get().clone(),
};
AccessPath::new(addr, access_vec)
}
fn get_resource_path(&mut self, address: AccountAddress, struct_tag: StructTag) -> AccessPath {
let access_vec = match self.resources.entry(struct_tag) {
btree_map::Entry::Vacant(entry) => {
let v = entry.key().access_vector();
entry.insert(v).clone()
}
btree_map::Entry::Occupied(entry) => entry.get().clone(),
};
AccessPath::new(address, access_vec)
}
}
impl BTreeAccessPathCache {
#[allow(dead_code)]
pub fn new() -> Self {
Self {
modules: BTreeMap::new(),
resources: BTreeMap::new(),
}
}
}