Trait dep_obj::DepType [−][src]
pub trait DepType: Sized { type Id: ComponentId; }
Expand description
A dependency type.
Use the dep_type
or the dep_type_with_builder
macro
to create a type implementing this trait.
Examples
use components_arena::{Arena, Component, ComponentClassToken, ComponentId, Id}; use dep_obj::{dep_obj, dep_type}; use dep_obj::flow::{Flows, FlowsToken, Just}; use dyn_context::{State, StateExt}; use macro_attr_2018::macro_attr; use std::any::{Any, TypeId}; dep_type! { #[derive(Debug)] pub struct MyDepType in MyDepTypeId { prop_1: bool = false, prop_2: i32 = 10, } } macro_attr! { #[derive(Component!, Debug)] struct MyDepTypePrivateData { dep_data: MyDepType, } } macro_attr! { #[derive(ComponentId!, Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)] pub struct MyDepTypeId(Id<MyDepTypePrivateData>); } pub struct MyApp { flows: Flows, my_dep_types: Arena<MyDepTypePrivateData>, res: i32, } impl State for MyApp { fn get_raw(&self, ty: TypeId) -> Option<&dyn Any> { if ty == TypeId::of::<Flows>() { Some(&self.flows) } else if ty == TypeId::of::<MyApp>() { Some(self) } else { None } } fn get_mut_raw(&mut self, ty: TypeId) -> Option<&mut dyn Any> { if ty == TypeId::of::<Flows>() { Some(&mut self.flows) } else if ty == TypeId::of::<MyApp>() { Some(self) } else { None } } } impl MyDepTypeId { pub fn new(app: &mut MyApp) -> MyDepTypeId { app.my_dep_types.insert(|id| (MyDepTypePrivateData { dep_data: MyDepType::new_priv() }, MyDepTypeId(id))) } dep_obj! { pub fn obj(self as this, app: MyApp) -> MyDepType { if mut { &mut app.my_dep_types[this.0].dep_data } else { &app.my_dep_types[this.0].dep_data } } } } fn main() { let mut my_dep_types_token = ComponentClassToken::new().unwrap(); let mut flows_token = FlowsToken::new().unwrap(); let mut app = MyApp { flows: Flows::new(&mut flows_token), my_dep_types: Arena::new(&mut my_dep_types_token), res: 0, }; let id = MyDepTypeId::new(&mut app); id.obj(&mut app).prop(MyDepType::PROP_2).values().handle(&mut app, (), |state, _, Just(value)| { let app: &mut MyApp = state.get_mut(); app.res = value; }); assert_eq!(app.res, 10); id.obj(&mut app).prop(MyDepType::PROP_2).set_distinct(5); assert_eq!(app.res, 5); }
Associated Types
type Id: ComponentId