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
//! Opaque, versioned entity identifier and bit-packing helpers.
//!
//! This module defines [`Entity`], the primary handle type used throughout the
//! ECS engine to refer to live entities, along with the low-level bit-packing
//! primitives that construct and decompose those handles.
//!
//! # Layout
//!
//! Every [`Entity`] wraps a single [`EntityID`] integer whose bits are divided
//! into three contiguous fields, from least-significant to most-significant:
//!
//! ```text
//! +-----------------+---------------+------------------------------+
//! | version | shard | index |
//! | (upper bits) | (SHARD_BITS) | (INDEX_BITS) |
//! +-----------------+---------------+------------------------------+
//! ```
//!
//! - **`index`** (`INDEX_BITS` wide) - slot within the owning shard's storage array.
//! - **`shard`** (`SHARD_BITS` wide) - identifies which shard owns the entity.
//! - **`version`** (remaining upper bits) - incremented each time a slot is
//! recycled, so stale handles from before a despawn can be detected.
//!
//! The exact widths of each field are controlled by the constants imported from
//! [`crate::engine::types`].
//!
//! # Key items
//!
//! | Item | Kind | Description |
//! |---|---|---|
//! | [`Entity`] | `struct` | The public handle type; cheap to copy, hash, and compare. |
//! | [`make_entity`] | `fn` | Constructs an [`Entity`] from `(shard, index, version)`. |
//! | [`make_id`] | `fn` | `const` variant that returns a raw [`EntityID`]. |
//! | [`split_entity`] | `fn` | Decomposes an [`Entity`] back into its three fields. |
//!
//! # Validity and liveness
//!
//! An [`Entity`] handle is *valid* if it was produced by this engine (i.e. via
//! [`make_entity`]) and has not been forged from an arbitrary integer. A valid
//! handle is *live* if the version encoded in the handle matches the version
//! currently stored in the shard slot **and** that slot is marked alive.
//! Handles that fail either check are considered stale and must not be used to
//! access component data.
//!
//! # Crate-internal helpers
//!
//! [`make_entity`], [`make_id`], and [`split_entity`] are `pub(super)` and
//! intended only for use by the engine's shard and registry layers. External
//! code should interact exclusively with the [`Entity`] API and obtain handles
//! through the engine's spawn/query interfaces.
use crate;
/// Opaque, versioned identifier for an ECS entity.
///
/// ## Purpose
/// `Entity` is a compact handle that uniquely identifies an entity instance
/// at a point in time. It encodes enough information to:
///
/// - Detect stale or recycled entity references
/// - Route entity operations to the correct shard
/// - Index directly into shard-local storage
///
/// ## Representation
/// Internally, an `Entity` packs three values into a single integer:
///
/// - **Shard ID** - identifies which shard owns the entity
/// - **Index** - slot within the shard
/// - **Version** - incremented on despawn to invalidate stale handles
///
/// ## Invariants
/// - Two entities with the same `(shard, index)` but different versions
/// are considered distinct.
/// - An entity is alive iff its version matches the stored version and
/// its slot is marked alive.
///
/// ## Notes
/// `Entity` values are cheap to copy and compare and are safe to pass
/// across threads.
;
pub const
pub
pub const