[−]Struct oxygengine_core::ecs::shred::MetaTable
The MetaTable
which allows to store object-safe trait implementations for resources.
For example, you have a trait Foo
that is implemented by several resources.
You can register all the implementors using MetaTable::register
. Later on, you
can iterate over all resources that implement Foo
without knowing their specific type.
Examples
use shred::{CastFrom, MetaTable, Resources}; trait Object { fn method1(&self) -> i32; fn method2(&mut self, x: i32); } impl<T> CastFrom<T> for Object where T: Object + 'static, { fn cast(t: &T) -> &Self { t } fn cast_mut(t: &mut T) -> &mut Self { t } } struct ImplementorA(i32); impl Object for ImplementorA { fn method1(&self) -> i32 { self.0 } fn method2(&mut self, x: i32) { self.0 += x; } } struct ImplementorB(i32); impl Object for ImplementorB { fn method1(&self) -> i32 { self.0 } fn method2(&mut self, x: i32) { self.0 *= x; } } let mut res = Resources::new(); res.insert(ImplementorA(3)); res.insert(ImplementorB(1)); let mut table = MetaTable::<Object>::new(); table.register(&ImplementorA(31415)); // Can just be some instance of type `&ImplementorA`. table.register(&ImplementorB(27182)); { let mut iter = table.iter(&mut res); assert_eq!(iter.next().unwrap().method1(), 3); assert_eq!(iter.next().unwrap().method1(), 1); }
Methods
impl<T> MetaTable<T> where
T: ?Sized,
T: ?Sized,
pub fn new() -> MetaTable<T>
Creates a new MetaTable
.
pub fn register<R>(&mut self, r: &R) where
R: Resource,
T: CastFrom<R> + 'static,
R: Resource,
T: CastFrom<R> + 'static,
Registers a resource R
that implements the trait T
.
This just needs some instance of type R
to retrieve the vtable.
It doesn't have to be the same object you're calling get
with later.
pub fn get(&self, res: &'a (dyn Resource + 'static)) -> Option<&'a T>
Tries to convert res
to a trait object of type &T
.
If res
doesn't have an implementation for T
(or it wasn't registered),
this will return None
.
pub fn get_mut(&self, res: &'a (dyn Resource + 'static)) -> Option<&'a mut T>
Tries to convert res
to a trait object of type &mut T
.
If res
doesn't have an implementation for T
(or it wasn't registered),
this will return None
.
ⓘImportant traits for MetaIter<'a, T>pub fn iter(&'a self, res: &'a Resources) -> MetaIter<'a, T>
Iterates all resources that implement T
and were registered.
ⓘImportant traits for MetaIterMut<'a, T>pub fn iter_mut(&'a self, res: &'a Resources) -> MetaIterMut<'a, T>
Iterates all resources that implement T
and were registered mutably.
Trait Implementations
Auto Trait Implementations
Blanket Implementations
impl<T> From for T
[src]
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Event for T where
T: Send + Sync + 'static,
[src]
T: Send + Sync + 'static,
impl<T> Erased for T
impl<T> Resource for T where
T: Any + Send + Sync,
T: Any + Send + Sync,
impl<T> TryDefault for T where
T: Default,
[src]
T: Default,
fn try_default() -> Result<T, String>
[src]
fn unwrap_default() -> Self
[src]
Calls try_default
and panics on an error case.
impl<T> Any for T where
T: Any,
T: Any,