Skip to main content

e2rcore/interface/
i_ele.rs

1use std::any::Any;
2
3// use interface::i_renderobj;
4use interface::i_component;
5
6/// # all available functionalities for an object
7pub trait IObjImpl: IObjImplClone {
8    fn as_any( & self ) -> & Any;
9    fn update_components( & mut self, components: & mut Vec< Box< i_component::IComponent > > ) -> Result< (), & 'static str >;
10}
11
12pub trait IObjImplClone {
13    fn clone_box( & self ) -> Box< IObjImpl >;
14}
15
16impl< T > IObjImplClone for T where T: 'static + IObjImpl + Clone {
17    fn clone_box( & self ) -> Box< IObjImpl > {
18        Box::new( self.clone() )
19    }
20}
21
22impl Clone for Box< IObjImpl > {
23    fn clone( & self ) -> Box< IObjImpl > {
24        self.clone_box()
25    }
26}
27
28/// # generic encapsulation for a create object
29#[derive(Clone)]
30pub struct Ele {
31    pub _impl: Box< IObjImpl >,
32    pub _components: Vec< Box< i_component::IComponent > >,
33}
34
35impl Ele {
36    pub fn init< T >( c: T ) -> Ele where T : IObjImpl + 'static {
37        Ele {
38            _impl: Box::new(c),
39            _components: vec![],
40        }
41    }
42    pub fn update_components_from_impl( & mut self ) -> Result< (), & 'static str > {
43        self._components.clear();
44        self._impl.update_components( & mut self._components )
45    }
46    // pub fn apply_component< T >( & mut self, comp: T ) -> Result< (), & 'static str >
47    //     where T: i_component::IComponent
48    // {
49    //     self._components.push( Box::new( comp ) );
50    //     Ok( () )
51    // }
52    // pub fn get_components( & mut self ) -> Result< &[ Box< i_component::IComponent > ], & 'static str > {
53    //     Ok( &self._components[..] )
54    // }
55
56    // pub fn get_impl( & mut self ) -> &IObjImpl {
57    //     self._impl.deref_mut()
58    // }
59}
60
61// impl Deref for Ele {
62//     type Target = Ele;
63//     fn deref( & self ) -> & Self::Target {
64//         & self
65//     }
66// }
67
68// impl DerefMut for Ele {
69//     fn deref_mut( & mut self ) -> & mut Ele {
70//         & mut self
71//     }
72// }