Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_ecs/src/storage/mod.rs
6604 views
1
//! Storage layouts for ECS data.
2
//!
3
//! This module implements the low-level collections that store data in a [`World`]. These all offer minimal and often
4
//! unsafe APIs, and have been made `pub` primarily for debugging and monitoring purposes.
5
//!
6
//! # Fetching Storages
7
//! Each of the below data stores can be fetched via [`Storages`], which can be fetched from a
8
//! [`World`] via [`World::storages`]. It exposes a top level container for each class of storage within
9
//! ECS:
10
//!
11
//! - [`Tables`] - columnar contiguous blocks of memory, optimized for fast iteration.
12
//! - [`SparseSets`] - sparse `HashMap`-like mappings from entities to components, optimized for random
13
//! lookup and regular insertion/removal of components.
14
//! - [`Resources`] - singleton storage for the resources in the world
15
//!
16
//! # Safety
17
//! To avoid trivially unsound use of the APIs in this module, it is explicitly impossible to get a mutable
18
//! reference to [`Storages`] from [`World`], and none of the types publicly expose a mutable interface.
19
//!
20
//! [`World`]: crate::world::World
21
//! [`World::storages`]: crate::world::World::storages
22
23
mod blob_array;
24
mod blob_vec;
25
mod resource;
26
mod sparse_set;
27
mod table;
28
mod thin_array_ptr;
29
30
pub use resource::*;
31
pub use sparse_set::*;
32
pub use table::*;
33
34
use crate::component::{ComponentInfo, StorageType};
35
36
/// The raw data stores of a [`World`](crate::world::World)
37
#[derive(Default)]
38
pub struct Storages {
39
/// Backing storage for [`SparseSet`] components.
40
/// Note that sparse sets are only present for components that have been spawned or have had a relevant bundle registered.
41
pub sparse_sets: SparseSets,
42
/// Backing storage for [`Table`] components.
43
pub tables: Tables,
44
/// Backing storage for resources.
45
pub resources: Resources<true>,
46
/// Backing storage for `!Send` resources.
47
pub non_send_resources: Resources<false>,
48
}
49
50
impl Storages {
51
/// ensures that the component has its necessary storage initialize.
52
pub fn prepare_component(&mut self, component: &ComponentInfo) {
53
match component.storage_type() {
54
StorageType::Table => {
55
// table needs no preparation
56
}
57
StorageType::SparseSet => {
58
self.sparse_sets.get_or_insert(component);
59
}
60
}
61
}
62
}
63
64