Add data support to import macro

This commit is contained in:
Lachlan Sneff
2019-03-28 11:41:14 -07:00
parent be08154670
commit 0787d001e3
2 changed files with 36 additions and 0 deletions

View File

@ -4,6 +4,7 @@ use std::collections::VecDeque;
use std::{
cell::{Ref, RefCell},
rc::Rc,
ffi::c_void,
};
pub trait LikeNamespace {
@ -45,6 +46,7 @@ impl IsExport for Export {
/// ```
pub struct ImportObject {
map: Rc<RefCell<HashMap<String, Box<dyn LikeNamespace>>>>,
state_creator: Option<Rc<Fn() -> (*mut c_void, fn(*mut c_void))>>,
}
impl ImportObject {
@ -52,6 +54,17 @@ impl ImportObject {
pub fn new() -> Self {
Self {
map: Rc::new(RefCell::new(HashMap::new())),
state_creator: None,
}
}
pub fn new_with_data<F>(state_creator: F) -> Self
where
F: Fn() -> (*mut c_void, fn(*mut c_void)) + 'static,
{
Self {
map: Rc::new(RefCell::new(HashMap::new())),
state_creator: Some(Rc::new(state_creator)),
}
}
@ -98,6 +111,7 @@ impl ImportObject {
pub fn clone_ref(&self) -> Self {
Self {
map: Rc::clone(&self.map),
state_creator: self.state_creator.clone(),
}
}