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

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

Loading…
Cancel
Save