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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//! # `bevy_expected_components`
//!
//! Runtime validation for Bevy component dependencies. Like `#[require]` but validates
//! instead of auto-inserting.
//!
//! ## Why Use This?
//!
//! Bevy's `#[require(T)]` automatically inserts missing components using `Default`.
//! This doesn't work when:
//!
//! - Required components need world access to construct
//! - You want bugs to surface immediately rather than silently using defaults
//! - The required component has no sensible default
//!
//! `#[expects(T)]` solves this by panicking if expected components are missing at insert
//! time, making bugs immediately visible during development.
//!
//! ## Performance Warning
//!
//! This crate adds runtime overhead: every time a component with `#[expects(...)]` is
//! inserted, the plugin checks that all expected components exist on the entity.
//!
//! **Recommended usage:** Enable only in development and test builds.
//!
//! ```rust,ignore
//! // Only add the plugin in debug builds
//! #[cfg(debug_assertions)]
//! app.add_plugins(ExpectedComponentsPlugin);
//! ```
//!
//! ## Example
//!
//! ```rust,ignore
//! use bevy::prelude::*;
//! use bevy_expected_components::prelude::*;
//!
//! #[derive(Component, Default)]
//! struct Transform;
//!
//! #[derive(Component, Default)]
//! struct Velocity;
//!
//! // PhysicsBody expects Transform and Velocity to exist when it's inserted
//! #[derive(Component, ExpectComponents)]
//! #[expects(Transform, Velocity)]
//! struct PhysicsBody;
//!
//! fn main() {
//! let mut app = App::new();
//!
//! // Enable validation (only in debug builds recommended)
//! #[cfg(debug_assertions)]
//! app.add_plugins(ExpectedComponentsPlugin);
//!
//! // This works - all expected components present
//! app.world_mut().spawn((PhysicsBody, Transform, Velocity));
//!
//! // This panics - Velocity is missing!
//! // app.world_mut().spawn((PhysicsBody, Transform));
//! }
//! ```
//!
//! ## How It Works
//!
//! 1. `#[derive(ExpectComponents)]` generates an [`ExpectComponents`] trait implementation
//! 2. The derive macro registers the type with [`inventory`] at compile time
//! 3. [`ExpectedComponentsPlugin`] iterates all registered types and installs `on_add` hooks
//! 4. When a component is inserted, the hook validates expected components exist
//!
//! ## Comparison with `#[require]`
//!
//! | Feature | `#[require]` | `#[expects]` |
//! |---------|--------------|--------------|
//! | Missing component | Auto-inserted with `Default` | Panics |
//! | Requires `Default` | Yes | No |
//! | Runtime cost | Archetype lookup | Component existence check |
//! | Use case | Convenience bundles | Bug detection |
//!
//! ## Limitations
//!
//! **Validates insertion only, not removal.** If you later remove an expected component
//! from an entity, no error occurs. This keeps the implementation simple and covers
//! the main use case: catching mistakes at spawn time.
//!
//! ## Future of This Crate
//!
//! This crate may become unnecessary when Bevy adds native support for non-defaultable
//! required components. Relevant discussions:
//!
//! - [Issue #16194: Require components that can't be defaulted](https://github.com/bevyengine/bevy/issues/16194)
//! - [Issue #18717: Support required components which have no sensible default](https://github.com/bevyengine/bevy/issues/18717)
//! - Archetype invariants (future Bevy feature)
use TypeId;
use ;
use Component;
use HookContext;
use ;
// Re-export for macro use
pub use inventory;
// Re-export derive macro
pub use ExpectComponents;
/// Prelude module for convenient imports.
///
/// ```rust,ignore
/// use bevy_expected_components::prelude::*;
/// ```
/// Trait implemented by components that expect other components to be present.
///
/// This trait is automatically implemented by the `#[derive(ExpectComponents)]` macro.
/// You should not need to implement it manually.
///
/// # Example
///
/// ```rust,ignore
/// #[derive(Component, ExpectComponents)]
/// #[expects(Transform, Velocity)]
/// struct PhysicsBody;
/// ```
/// Registration entry for a component with expectations.
///
/// Created by the `#[derive(ExpectComponents)]` macro and collected via `inventory`.
/// You should not need to use this directly.
/// Registers component hooks for type T. Used by the derive macro.
collect!;
/// Plugin that enables runtime validation of component expectations.
///
/// When added to your app, this plugin registers `on_add` hooks for all components
/// that use `#[derive(ExpectComponents)]`. When those components are inserted,
/// the hooks validate that all expected components exist on the entity.
///
/// # Performance Warning
///
/// This plugin adds runtime overhead. It is recommended to only enable it in
/// development and test builds:
///
/// ```rust,ignore
/// #[cfg(debug_assertions)]
/// app.add_plugins(ExpectedComponentsPlugin);
/// ```
///
/// # Panics
///
/// When a component is inserted and its expected components are missing, the
/// plugin will panic with a message like:
///
/// ```text
/// my_crate::RoadNode expects bevy::transform::components::Transform
/// but it was not found on entity 42v3
/// ```
;
/// Validation hook called when a component with expectations is inserted.
// Bevy hook signature requires owned DeferredWorld