fugue_db/
inter_ref.rs

1use crate::Id;
2use crate::Function;
3
4use crate::error::Error;
5use crate::schema;
6
7#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct InterRef<'db> {
9    address: u64,
10    source_id: Id<Function<'db>>,
11    target_id: Id<Function<'db>>,
12    call: bool,
13}
14
15impl<'db> InterRef<'db> {
16    pub fn address(&self) -> u64 {
17        self.address
18    }
19
20    pub fn source_id(&self) -> Id<Function<'db>> {
21        self.source_id.clone()
22    }
23
24    pub fn target_id(&self) -> Id<Function<'db>> {
25        self.target_id.clone()
26    }
27
28    pub fn is_call(&self) -> bool {
29        self.call
30    }
31
32    pub fn is_jump(&self) -> bool {
33        !self.call
34    }
35
36    pub(crate) fn from_reader(reader: schema::InterRef) -> Result<Self, Error> {
37        Ok(Self {
38            address: reader.address(),
39            source_id: reader.source().into(),
40            target_id: reader.target().into(),
41            call: reader.call(),
42        })
43    }
44
45    pub(crate) fn to_builder<'a: 'b, 'b>(
46        &self,
47        builder: &'b mut flatbuffers::FlatBufferBuilder<'a>
48    ) -> Result<flatbuffers::WIPOffset<schema::InterRef<'a>>, Error> {
49        let mut ibuilder = schema::InterRefBuilder::new(builder);
50
51        ibuilder.add_address(self.address());
52        ibuilder.add_source(self.source_id().index() as u32);
53        ibuilder.add_target(self.target_id().index() as u32);
54        ibuilder.add_call(self.is_call());
55
56        Ok(ibuilder.finish())
57    }
58}
59