1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
//! Shipyard is an Entity Component System focused on usability and speed.
//!
//! # Getting started
//!
//! The [user guide](https://leudz.github.io/shipyard/book) is a great place to learn all about Shipyard!
//! Here's two examples to get an idea of what it provides:
//! ```
//! # #[cfg(feature = "proc")]
//! # {
//! use shipyard::prelude::*;
//!
//! struct Health(f32);
//! struct Position { x: f32, y: f32 }
//!
//! #[system(InAcid)]
//! fn run(pos: &Position, mut health: &mut Health) {
//! (&pos, &mut health).iter()
//! .filter(|(pos, _)| is_in_acid(pos))
//! .for_each(|(pos, mut health)| {
//! health.0 -= 1.0;
//! });
//! }
//!
//! fn is_in_acid(pos: &Position) -> bool {
//! // it's wet season
//! true
//! }
//!
//! let world = World::new();
//!
//! {
//! let (mut entities, mut positions, mut healths) =
//! world.borrow::<(EntitiesMut, &mut Position, &mut Health)>();
//!
//! entities.add_entity(
//! (&mut positions, &mut healths),
//! (Position { x: 0.0, y: 0.0 },
//! Health(1000.0))
//! );
//! }
//!
//! world.run_system::<InAcid>();
//! # }
//! ```
//! # Let's make some pigs!
//! ```
//! # #[cfg(all(feature = "parallel", feature = "proc"))]
//! # {
//! use shipyard::prelude::*;
//!
//! struct Health(f32);
//! struct Fat(f32);
//!
//! #[system(Reproduction)]
//! fn run(mut fat: &mut Fat, mut health: &mut Health, mut entities: &mut Entities) {
//! let count = (&health, &fat).iter().filter(|(health, fat)| health.0 > 40.0 && fat.0 > 20.0).count();
//! (0..count).for_each(|_| {
//! entities.add_entity((&mut health, &mut fat), (Health(100.0), Fat(0.0)));
//! });
//! }
//!
//! #[system(Meal)]
//! fn run(mut fat: &mut Fat) {
//! (&mut fat).iter().into_chunk(8).ok().unwrap().for_each(|slice| {
//! for fat in slice {
//! fat.0 += 3.0;
//! }
//! });
//! }
//!
//! #[system(Age)]
//! fn run(mut health: &mut Health, thread_pool: ThreadPool) {
//! use rayon::prelude::ParallelIterator;
//!
//! thread_pool.install(|| {
//! (&mut health).par_iter().for_each(|health| {
//! health.0 -= 4.0;
//! });
//! });
//! }
//!
//! let world = World::new();
//!
//! world.run::<(EntitiesMut, &mut Health, &mut Fat), _, _>(|(mut entities, mut health, mut fat)| {
//! (0..100).for_each(|_| {
//! entities.add_entity(
//! (&mut health, &mut fat),
//! (Health(100.0), Fat(0.0))
//! );
//! })
//! });
//!
//! world.add_workload::<(Meal, Age), _>("Life");
//! world.add_workload::<Reproduction, _>("Reproduction");
//!
//! for day in 0..100 {
//! if day % 6 == 0 {
//! world.run_workload("Reproduction");
//! }
//! world.run_default();
//! }
//!
//! world.run::<&Health, _, _>(|health| {
//! // we've got some new pigs
//! assert_eq!(health.len(), 900);
//! });
//! # }
//! ```
//!
//! ## Features
//!
//! - **parallel** *(default)* — adds parallel iterators and dispatch
//! - **proc** *(default)* — adds `system` proc macro
//! - **serde** — adds (de)serialization support with [serde](https://github.com/serde-rs/serde)
//! - **non_send** — add methods and types required to work with `!Send` components
//! - **non_sync** — add methods and types required to work with `!Sync` components
//! - **std** *(default)* — let shipyard use the standard library
//!
//! ## Unsafe
//!
//! This crate uses `unsafe` both because sometimes there's no way around it, and for performance gain.
//! Releases should have all invocation of `unsafe` explained.
//! If you find places where a safe alternative is possible without repercussion (small ones are sometimes acceptable) feel free to open an issue or a PR.
#![deny(bare_trait_objects)]
#![deny(elided_lifetimes_in_paths)]
#![deny(trivial_casts)]
#![deny(trivial_numeric_casts)]
#![deny(unused_qualifications)]
#![cfg_attr(not(any(feature = "std", test)), no_std)]
#[macro_use]
extern crate alloc;
mod atomic_refcell;
mod delete;
/// Contains all error types.
pub mod error;
mod get;
/// Re-export types that aren't needed in most use cases.
pub mod internal;
mod iter;
mod not;
mod pack;
pub mod prelude;
mod remove;
mod run;
mod sparse_set;
mod storage;
mod unknown_storage;
mod views;
mod world;
pub use storage::{AllStorages, Entities, EntitiesMut, EntityId};
pub use world::World;
/// Type used to borrow the rayon::ThreadPool inside `World`.
#[cfg(feature = "parallel")]
pub struct ThreadPool;
/// Type used to access the value of a unique storage.
/// ### Example:
/// ```
/// # use shipyard::prelude::*;
/// let world = World::default();
/// world.add_unique(0usize);
///
/// world.run::<Unique<&mut usize>, _, _>(|mut x| {
/// *x += 1;
/// });
/// ```
pub struct Unique<T: ?Sized>(T);
/// Type used to access `!Send` storages.
#[cfg(feature = "non_send")]
pub struct NonSend<T: ?Sized>(T);
/// Type used to access `!Sync` storages.
#[cfg(feature = "non_sync")]
pub struct NonSync<T: ?Sized>(T);
/// Type used to access `!Send + !Sync` storages.
#[cfg(all(feature = "non_send", feature = "non_sync"))]
pub struct NonSendSync<T: ?Sized>(T);