move Contetx ComponentMap into RefCell

feature/battle
brnrs 5 years ago
parent babd1084a6
commit 13015bbc04
  1. 4
      src/contexts/component_factory.rs
  2. 33
      src/contexts/model_i.rs

@ -73,7 +73,7 @@ mod tests {
Ok(from_context!(context; A, B))
}
let mut context = Context::new();
let context = Context::new();
context.set(A("Test"));
context.set(B(67));
@ -103,7 +103,7 @@ mod tests {
// Given
derive_ccf!(CFactory; A, B);
let c_factory = CFactory;
let mut context = Context::new();
let context = Context::new();
context.set(A("test"));
context.set(B(67));

@ -1,11 +1,11 @@
use std::any::Any;
use std::cell::RefCell;
use super::component::*;
use super::component_map::*;
#[allow(dead_code)]
pub struct Context<'t> {
map: ComponentMap,
map: RefCell<ComponentMap>,
base_context: Option<&'t Context<'t>>,
}
@ -13,13 +13,13 @@ pub struct Context<'t> {
impl<'t> Context<'t> {
pub fn new() -> Context<'t> {
Context {
map: ComponentMap::new(),
map: RefCell::new(ComponentMap::new()),
base_context: None,
}
}
pub fn get<T: Any + Sized + 'static>(&self) -> Option<Component<T>> {
match self.map.get() {
match self.map.borrow().get() {
component @ Some(_) => component,
_ => match self.base_context {
Some(context) => context.get(),
@ -28,8 +28,8 @@ impl<'t> Context<'t> {
}
}
pub fn set<T: Any + Sized + 'static>(&mut self, component: T) {
self.map.set(component)
pub fn set<T: Any + Sized + 'static>(&self, component: T) {
self.map.borrow_mut().set(component)
}
pub fn subcontext(&self) -> Context {
@ -58,11 +58,8 @@ mod tests {
// Given
#[derive(Debug, Eq, PartialEq)]
struct TestStruct(&'static str);
let context = {
let mut tmp = Context::new();
tmp.set(TestStruct("hello"));
tmp
};
let context = Context::new();
context.set(TestStruct("hello"));
// When
let test_struct = context.get::<TestStruct>().unwrap();
@ -76,11 +73,8 @@ mod tests {
// Given
#[derive(Debug, Eq, PartialEq)]
struct TestStruct(&'static str);
let context = {
let mut tmp = Context::new();
tmp.set(TestStruct("hello"));
tmp
};
let context = Context::new();
context.set(TestStruct("hello"));
let subcontext = context.subcontext();
// When
@ -96,11 +90,8 @@ mod tests {
#[derive(Debug, Eq, PartialEq)]
struct TestStruct(&'static str);
let context = Context::new();
let subcontext = {
let mut tmp = context.subcontext();
tmp.set(TestStruct("hello"));
tmp
};
let subcontext = context.subcontext();
subcontext.set(TestStruct("hello"));
// When
let test_struct = subcontext.get::<TestStruct>().unwrap();

Loading…
Cancel
Save