Struct shred::MetaTable

source ·
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: *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>();
table.register::<ImplementorB>();

{
    let mut iter = table.iter(&mut world);
    assert_eq!(iter.next().unwrap().method1(), 3);
    assert_eq!(iter.next().unwrap().method1(), 1);
}

Implementations§

source§

impl<T: ?Sized> MetaTable<T>

source

pub fn new() -> Self

Creates a new MetaTable.

source

pub fn register<R>(&mut self)
where R: Resource, T: CastFrom<R> + 'static,

Registers a resource R that implements the trait T.

source

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.

source

pub fn get_mut<'a>(&self, res: &'a mut 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.

source

pub fn iter<'a>(&'a self, res: &'a World) -> MetaIter<'a, T>

Iterates all resources that implement T and were registered.

source

pub fn iter_mut<'a>(&'a self, res: &'a World) -> MetaIterMut<'a, T>

Iterates all resources that implement T and were registered mutably.

Trait Implementations§

source§

impl<T> Default for MetaTable<T>
where T: ?Sized,

source§

fn default() -> Self

Returns the “default value” for a type. Read more

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§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> Resource for T
where T: Any + Send + Sync,