use std::ops::{Index, IndexMut};
use graph::Graph;
#[derive(Copy, Clone, Debug)]
pub struct FunctionRef(pub(crate) usize);
#[derive(Debug, Default)]
pub struct Module {
pub main: Graph,
pub(crate) functions: Vec<Graph>,
}
impl Module {
pub fn add_function(&mut self) -> FunctionRef {
let index = self.functions.len();
self.functions.push(Graph::default());
FunctionRef(index)
}
pub fn function<'a>(&'a mut self, index: FunctionRef) -> Option<&'a mut Graph> {
self.functions.get_mut(index.0)
}
}
impl Index<FunctionRef> for Module {
type Output = Graph;
fn index(&self, index: FunctionRef) -> &Graph {
&self.functions[index.0]
}
}
impl IndexMut<FunctionRef> for Module {
fn index_mut(&mut self, index: FunctionRef) -> &mut Graph {
&mut self.functions[index.0]
}
}