|
|
@ -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(); |
|
|
|