-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathmetadata.rs
More file actions
71 lines (63 loc) · 1.79 KB
/
metadata.rs
File metadata and controls
71 lines (63 loc) · 1.79 KB
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
66
67
68
69
70
71
use std::{
collections::HashMap,
fmt::{self, Debug, Formatter},
io::Cursor,
iter,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use crate::mir_loc::{DefPathHash, Func, MirLoc, MirLocId};
#[derive(Debug, Serialize, Deserialize)]
pub struct Metadata {
pub locs: Vec<MirLoc>,
pub functions: HashMap<DefPathHash, String>,
}
impl Metadata {
pub fn get(&self, index: MirLocId) -> &MirLoc {
&self.locs[index as usize]
}
pub fn read(bytes: &[u8]) -> bincode::Result<Self> {
bincode_deserialize_many(bytes)
}
}
fn bincode_deserialize_many<T, C>(bytes: &[u8]) -> bincode::Result<C>
where
T: DeserializeOwned,
C: FromIterator<T>,
{
let len = bytes.len();
let mut cursor = Cursor::new(bytes);
iter::from_fn(|| {
// No good alternatives: <https://github.com/rust-lang/rust/issues/86369>.
if cursor.position() == len.try_into().unwrap() {
return None;
}
Some(bincode::deserialize_from(&mut cursor))
})
.collect::<Result<_, _>>()
}
impl FromIterator<Metadata> for Metadata {
fn from_iter<I: IntoIterator<Item = Metadata>>(iter: I) -> Self {
let mut locs = Vec::new();
let mut functions = HashMap::new();
for metadata in iter {
locs.extend(metadata.locs);
functions.extend(metadata.functions);
}
Self { locs, functions }
}
}
impl Debug for MirLoc {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let MirLoc {
func:
Func {
def_path_hash: _,
name: fn_name,
},
basic_block_idx,
statement_idx,
metadata: _,
} = self;
write!(f, "{fn_name}:{basic_block_idx}:{statement_idx}")
}
}