pub use shred::World;
pub use self::{
comp::Component,
entity::{
CreateIterAtomic, Entities, EntitiesRes, Entity, EntityResBuilder, Generation, Index,
},
lazy::{LazyBuilder, LazyUpdate},
world_ext::WorldExt,
};
use shred::{FetchMut, SystemData};
use crate::storage::WriteStorage;
mod comp;
mod entity;
mod lazy;
#[cfg(test)]
mod tests;
mod world_ext;
pub struct CreateIter<'a>(FetchMut<'a, EntitiesRes>);
impl<'a> Iterator for CreateIter<'a> {
type Item = Entity;
fn next(&mut self) -> Option<Entity> {
Some(self.0.alloc.allocate())
}
}
pub trait Builder {
#[cfg(feature = "parallel")]
fn with<C: Component + Send + Sync>(self, c: C) -> Self;
#[cfg(not(feature = "parallel"))]
fn with<C: Component>(self, c: C) -> Self;
#[cfg(feature = "parallel")]
fn maybe_with<C: Component + Send + Sync>(self, c: Option<C>) -> Self
where
Self: Sized,
{
match c {
Some(c) => self.with(c),
None => self,
}
}
#[cfg(not(feature = "parallel"))]
fn maybe_with<C: Component>(self, c: Option<C>) -> Self
where
Self: Sized,
{
match c {
Some(c) => self.with(c),
None => self,
}
}
fn build(self) -> Entity;
}
#[must_use = "Please call .build() on this to finish building it."]
pub struct EntityBuilder<'a> {
pub entity: Entity,
pub world: &'a World,
built: bool,
}
impl<'a> Builder for EntityBuilder<'a> {
#[inline]
fn with<T: Component>(self, c: T) -> Self {
{
let mut storage: WriteStorage<T> = SystemData::fetch(self.world);
storage.insert(self.entity, c).unwrap();
}
self
}
#[inline]
fn build(mut self) -> Entity {
self.built = true;
self.entity
}
}
impl<'a> Drop for EntityBuilder<'a> {
fn drop(&mut self) {
if !self.built {
self.world
.read_resource::<EntitiesRes>()
.delete(self.entity)
.unwrap();
}
}
}