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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//! Low-level structural error types.
//!
//! These are the foundational, self-contained error structs used throughout
//! the ECS. They have no dependencies on other error types within this module
//! and are composed into higher-level aggregate errors elsewhere.
//!
//! # Error Types
//!
//! | Error | Description |
//! |-------|-------------|
//! | [`CapacityError`] | Insufficient capacity to create or place additional entities. |
//! | [`ShardBoundsError`] | Shard index is outside the valid range for a shard set. |
//! | [`StaleEntityError`] | Entity handle is no longer valid (despawned or generation mismatch). |
//! | [`EmptyArchetypeError`] | Archetype contains no components when at least one was expected. |
//! | [`PositionOutOfBoundsError`] | `(ChunkID, RowID)` pair addresses a position outside storage bounds. |
//! | [`TypeMismatchError`] | Component write targets a storage slot with a mismatched element type. |
use fmt;
use crate;
/// Returned when the system cannot satisfy a request to create or place
/// additional entities because the target container has insufficient capacity.
///
/// This typically arises during batch spawns or when attempting to grow a shard
/// beyond its configured limit.
///
/// ### Fields
/// * `entities_needed` - Total number of entities the operation attempted to
/// create or accommodate.
/// * `capacity` - The current upper bound that prevented the operation.
///
/// ### Example
/// ```text
/// if requested > shard.capacity() {
/// return Err(CapacityError { entities_needed: requested as u64, capacity: shard.capacity() as u64 }.into());
/// }
/// ```
/// Returned when a shard index is outside the valid range for the target shard
/// set or collection.
///
/// ### Fields
/// * `index` - The shard index that was requested.
/// * `max_index` - The maximum valid shard index (inclusive).
///
/// ### Example
/// ```text
/// let max = shards.len().saturating_sub(1) as u32;
/// if idx > max {
/// return Err(ShardBoundsError { index: idx, max_index: max }.into());
/// }
/// ```
/// Returned when an `Entity` handle is no longer valid - typically because it
/// was despawned or its generation/version no longer matches live storage.
///
/// Use this to prevent use-after-free style logic errors at the API boundary.
///
/// ### Example
/// ```text
/// if !entities.is_live(entity) {
/// return Err(StaleEntityError.into());
/// }
/// ```
;
/// Returned when a `(ChunkID, RowID)` pair refers to a position outside
/// valid component storage bounds.
///
/// ## Context
/// Used by attribute and archetype storage to report invalid addressing,
/// typically caused by stale metadata or incorrect index calculations.
///
/// ## Invariants
/// - `chunk < chunks`
/// - `row < capacity` for all but the last chunk
/// Returned when an attribute/component write targets a storage slot whose
/// element type does not match the provided value's type.
///
/// This is a logic/configuration error surfaced by storage when component
/// type IDs diverge (e.g. writing `Velocity` into a `Position` column).
///
/// ### Fields
/// * `expected` - The [`TypeId`] that the destination storage declares.
/// * `actual` - The [`TypeId`] of the value provided by the caller.
/// * `expected_name` - Human-readable name of the expected type, obtained via
/// [`std::any::type_name`].
/// * `actual_name` - Human-readable name of the actual type, obtained via
/// [`std::any::type_name`].
///
/// ### Example
/// ```text
/// if actual_type != expected_type {
/// return Err(TypeMismatchError {
/// expected: expected_type,
/// actual: actual_type,
/// expected_name: std::any::type_name::<Expected>(),
/// actual_name: std::any::type_name::<Actual>(),
/// }.into());
/// }
/// ```