despero_hecs/
lib.rs

1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! A handy ECS
9//!
10//! hecs provides a high-performance, minimalist entity-component-system (ECS) world. It is a
11//! library, not a framework. In place of an explicit "System" abstraction, a `World`'s entities are
12//! easily queried from regular code. Organize your application however you like!
13//!
14//! In order of importance, hecs pursues:
15//! - fast traversals
16//! - a simple interface
17//! - a small dependency closure
18//! - exclusion of externally-implementable functionality
19//!
20//! ```
21//! # use hecs::*;
22//! let mut world = World::new();
23//! // Nearly any type can be used as a component with zero boilerplate
24//! let a = world.spawn((123, true, "abc"));
25//! let b = world.spawn((42, false));
26//! // Systems can be simple for loops
27//! for (id, (mut number, &flag)) in world.query_mut::<(&mut i32, &bool)>() {
28//!   if flag { *number *= 2; }
29//! }
30//! // Random access is simple and safe
31//! assert_eq!(*world.get::<&i32>(a).unwrap(), 246);
32//! assert_eq!(*world.get::<&i32>(b).unwrap(), 42);
33//! ```
34
35#![warn(missing_docs)]
36#![no_std]
37#![cfg_attr(docsrs, feature(doc_cfg))]
38
39#[cfg(feature = "std")]
40extern crate std;
41
42extern crate alloc;
43
44/// Imagine macro parameters, but more like those Russian dolls.
45///
46/// Calls m!(A, B, C), m!(A, B), m!(B), and m!() for i.e. (m, A, B, C)
47/// where m is any macro, for any number of parameters.
48macro_rules! smaller_tuples_too {
49    ($m: ident, $ty: ident) => {
50        $m!{}
51        $m!{$ty}
52    };
53    ($m: ident, $ty: ident, $($tt: ident),*) => {
54        smaller_tuples_too!{$m, $($tt),*}
55        $m!{$ty, $($tt),*}
56    };
57}
58
59/// Implementations of ecs bundle
60pub mod bundle;
61/// ECS-queries
62pub mod query;
63#[cfg(any(feature = "row-serialize", feature = "column-serialize"))]
64pub mod serialize;
65
66mod archetype;
67mod batch;
68mod borrow;
69mod command_buffer;
70mod entities;
71mod entity_builder;
72mod entity_ref;
73mod query_one;
74mod tracked;
75mod take;
76mod world;
77
78pub use archetype::{Archetype, ArchetypeColumn, ArchetypeColumnMut};
79pub use batch::{BatchIncomplete, BatchWriter, ColumnBatch, ColumnBatchBuilder, ColumnBatchType};
80pub use bundle::{Bundle, DynamicBundle, DynamicBundleClone, MissingComponent};
81pub use command_buffer::CommandBuffer;
82pub use entities::{Entity, NoSuchEntity};
83pub use entity_builder::{BuiltEntity, BuiltEntityClone, EntityBuilder, EntityBuilderClone};
84pub use entity_ref::{ComponentRef, ComponentRefShared, EntityRef, Ref, RefMut};
85pub use query::{
86    Access, Batch, BatchedIter, Or, PreparedQuery, PreparedQueryBorrow, PreparedQueryIter,
87    PreparedView, Query, QueryBorrow, QueryIter, QueryMut, QueryShared, Satisfies, View, With,
88    Without,
89};
90pub use query_one::QueryOne;
91pub use tracked::{Added, Changed, Mutated};
92pub use take::TakenEntity;
93pub use world::{
94    ArchetypesGeneration, Component, ComponentError, Iter, QueryOneError, SpawnBatchIter,
95    SpawnColumnBatchIter, World,
96};
97
98// Unstable implementation details needed by the macros
99#[doc(hidden)]
100pub use archetype::TypeInfo;
101#[doc(hidden)]
102pub use bundle::DynamicClone;
103#[cfg(feature = "macros")]
104#[doc(hidden)]
105pub use lazy_static;
106#[doc(hidden)]
107pub use query::Fetch;
108
109#[cfg(feature = "macros")]
110pub use hecs_macros::{Bundle, DynamicBundleClone, Query};
111
112fn align(x: usize, alignment: usize) -> usize {
113    debug_assert!(alignment.is_power_of_two());
114    (x + alignment - 1) & (!alignment + 1)
115}