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