pub struct MetaTable<T: ?Sized> { /* private fields */ }
Expand description
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, World};
trait Object {
fn method1(&self) -> i32;
fn method2(&mut self, x: i32);
}
unsafe impl<T> CastFrom<T> for dyn 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 world = World::empty();
world.insert(ImplementorA(3));
world.insert(ImplementorB(1));
let mut table = MetaTable::<dyn Object>::new();
table.register(&ImplementorA(31415)); // Can just be some instance of type `&ImplementorA`.
table.register(&ImplementorB(27182));
{
let mut iter = table.iter(&mut world);
assert_eq!(iter.next().unwrap().method1(), 3);
assert_eq!(iter.next().unwrap().method1(), 1);
}
Implementations
sourceimpl<T: ?Sized> MetaTable<T>
impl<T: ?Sized> MetaTable<T>
sourcepub fn register<R>(&mut self, r: &R) where
R: Resource,
T: CastFrom<R> + 'static,
pub fn register<R>(&mut self, r: &R) where
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.
sourcepub fn get<'a>(&self, res: &'a dyn Resource) -> Option<&'a T>
pub fn get<'a>(&self, res: &'a dyn Resource) -> Option<&'a T>
Tries to convert world
to a trait object of type &T
.
If world
doesn’t have an implementation for T
(or it wasn’t
registered), this will return None
.
sourcepub fn get_mut<'a>(&self, res: &'a dyn Resource) -> Option<&'a mut T>
pub fn get_mut<'a>(&self, res: &'a dyn Resource) -> Option<&'a mut T>
Tries to convert world
to a trait object of type &mut T
.
If world
doesn’t have an implementation for T
(or it wasn’t
registered), this will return None
.
sourcepub fn iter<'a>(&'a self, res: &'a World) -> MetaIter<'a, T>ⓘNotable traits for MetaIter<'a, T>impl<'a, T> Iterator for MetaIter<'a, T> where
T: ?Sized + 'a, type Item = &'a T;
pub fn iter<'a>(&'a self, res: &'a World) -> MetaIter<'a, T>ⓘNotable traits for MetaIter<'a, T>impl<'a, T> Iterator for MetaIter<'a, T> where
T: ?Sized + 'a, type Item = &'a T;
T: ?Sized + 'a, type Item = &'a T;
Iterates all resources that implement T
and were registered.
sourcepub fn iter_mut<'a>(&'a self, res: &'a World) -> MetaIterMut<'a, T>ⓘNotable traits for MetaIterMut<'a, T>impl<'a, T> Iterator for MetaIterMut<'a, T> where
T: ?Sized + 'a, type Item = &'a mut T;
pub fn iter_mut<'a>(&'a self, res: &'a World) -> MetaIterMut<'a, T>ⓘNotable traits for MetaIterMut<'a, T>impl<'a, T> Iterator for MetaIterMut<'a, T> where
T: ?Sized + 'a, type Item = &'a mut T;
T: ?Sized + 'a, type Item = &'a mut T;
Iterates all resources that implement T
and were registered mutably.
Trait Implementations
Auto Trait Implementations
impl<T: ?Sized> RefUnwindSafe for MetaTable<T> where
T: RefUnwindSafe,
impl<T: ?Sized> Send for MetaTable<T>
impl<T: ?Sized> Sync for MetaTable<T>
impl<T: ?Sized> Unpin for MetaTable<T>
impl<T: ?Sized> UnwindSafe for MetaTable<T> where
T: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more