[][src]Module mecs::component

Components and their storage

All components live within a storage, which may be an enum, a boxed trait, etc..., anything that implements the Storage trait. All types that can live within a storage must implement Component of that storage.

Example

 
mecs::impl_enum_storage! {
	enum Components {
		A(i32),
		B(&'static str),
		C(f32),
	}
}

Manual Implementation

use std::any::Any;
use mecs::{Storage, Component};
 
enum Components<'a> {
	A(i32),
	B(&'a str),
}
 
impl<'a> Storage<'a> for Components<'a>
{
	type Id = usize;
 	
	fn id(&self) -> Self::Id {
		match self {
			Self::A(_) => <i32     as Component<Self>>::id(),
			Self::B(_) => <&'a str as Component<Self>>::id(),
		}
	}
}
 
impl<'a> Component<'a, Components<'a>> for i32 {
	fn id() -> <Components<'a> as Storage<'a>>::Id { 0 }
 	
	fn get<'b>(storage: &'b Components<'a>) -> Option<&'b Self> {
		if let Components::A(num) = storage { Some( num ) } else { None }
	}
 	
	fn get_mut<'b>(storage: &'b mut Components<'a>) -> Option<&'b mut Self> {
		if let Components::A(num) = storage { Some( num ) } else { None }
	}
}
 
impl<'a> Component<'a, Components<'a>> for &'a str {
	fn id() -> <Components<'a> as Storage<'a>>::Id { 1 }
 	
	fn get<'b>(storage: &'b Components<'a>) -> Option<&'b Self> {
		if let Components::B(name) = storage { Some( name ) } else { None }
	}
 	
	fn get_mut<'b>(storage: &'b mut Components<'a>) -> Option<&'b mut Self> {
		if let Components::B(name) = storage { Some( name ) } else { None }
	}
}

Re-exports

pub use dyn_storage::DynStorage;

Modules

dyn_storage

Dynamic storage for components

Traits

Component

Trait implemented by all types within a storage.

Storage

Storage for any number of components