Skip to content

Commit 13b82b4

Browse files
committed
Undo some unnecessary changes
1 parent 5212e84 commit 13b82b4

File tree

21 files changed

+305
-404
lines changed

21 files changed

+305
-404
lines changed

crates/bevy_app/src/app_builder.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
CoreStage, PluginGroup, PluginGroupBuilder, StartupStage,
55
};
66
use bevy_ecs::{
7-
component::{Component, StorageType},
7+
component::{Component, ComponentDescriptor},
88
event::Events,
99
schedule::{
1010
RunOnce, Schedule, Stage, StageLabel, State, SystemDescriptor, SystemSet, SystemStage,
@@ -319,10 +319,8 @@ impl AppBuilder {
319319
/// will result in an error.
320320
///
321321
/// See [World::register_component]
322-
pub fn register_component<T: Component>(&mut self, storage_type: StorageType) -> &mut Self {
323-
self.world_mut()
324-
.register_component::<T>(storage_type)
325-
.unwrap();
322+
pub fn register_component(&mut self, descriptor: ComponentDescriptor) -> &mut Self {
323+
self.world_mut().register_component(descriptor).unwrap();
326324
self
327325
}
328326

crates/bevy_ecs/macros/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ pub fn impl_query_set(_input: TokenStream) -> TokenStream {
253253
fn new_archetype(&mut self, archetype: &Archetype, system_state: &mut SystemState) {
254254
let (#(#query,)*) = &mut self.0;
255255
#(
256-
// FIXME(Relationships) investigate this function
257256
for (relation_filter, cache) in #query.relation_filter_accesses.iter_mut() {
258257
QueryState::<#query, #filter>::new_archetype(
259258
&#query.fetch_state,

crates/bevy_ecs/src/bundle.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ pub use bevy_ecs_macros::Bundle;
33
use crate::{
44
archetype::ComponentStatus,
55
component::{
6-
Component, ComponentTicks, RelationKindId, RelationshipKindInfo, Relationships,
7-
StorageType, TypeInfo,
6+
Component, ComponentTicks, Components, RelationKindId, RelationKindInfo, StorageType,
7+
TypeInfo,
88
},
99
entity::Entity,
1010
storage::{SparseSetIndex, SparseSets, Table},
1111
};
1212
use bevy_ecs_macros::all_tuples;
13-
use std::{any::TypeId, collections::HashMap, u8};
13+
use std::{any::TypeId, collections::HashMap};
1414

1515
/// An ordered collection of components, commonly used for spawning entities, and adding and
1616
/// removing components in bulk.
@@ -237,7 +237,7 @@ impl Bundles {
237237

238238
pub(crate) fn init_relationship_info<'a>(
239239
&'a mut self,
240-
relation_kind: &RelationshipKindInfo,
240+
relation_kind: &RelationKindInfo,
241241
relation_target: Option<Entity>,
242242
) -> &'a BundleInfo {
243243
let bundle_infos = &mut self.bundle_infos;
@@ -259,7 +259,7 @@ impl Bundles {
259259

260260
pub(crate) fn init_info<'a, T: Bundle>(
261261
&'a mut self,
262-
components: &mut Relationships,
262+
components: &mut Components,
263263
) -> &'a BundleInfo {
264264
let bundle_infos = &mut self.bundle_infos;
265265
let id = self.bundle_ids.entry(TypeId::of::<T>()).or_insert_with(|| {
@@ -278,14 +278,13 @@ fn initialize_bundle(
278278
bundle_type_name: &'static str,
279279
type_info: &[TypeInfo],
280280
id: BundleId,
281-
components: &mut Relationships,
281+
components: &mut Components,
282282
) -> BundleInfo {
283283
let mut component_ids = Vec::new();
284284
let mut storage_types = Vec::new();
285285

286286
for type_info in type_info {
287-
let kind_info =
288-
components.get_component_kind_or_insert(type_info.type_id(), type_info.clone().into());
287+
let kind_info = components.get_component_kind_or_insert(type_info.clone().into());
289288
component_ids.push((kind_info.id(), None));
290289
storage_types.push(kind_info.data_layout().storage_type());
291290
}

crates/bevy_ecs/src/component/mod.rs

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub struct ComponentDescriptor {
4848
}
4949

5050
impl ComponentDescriptor {
51-
pub unsafe fn new(
51+
pub unsafe fn new_dynamic(
5252
name: Option<String>,
5353
storage_type: StorageType,
5454
is_send_and_sync: bool,
@@ -65,7 +65,7 @@ impl ComponentDescriptor {
6565
}
6666
}
6767

68-
pub fn from_generic<T: Component>(storage_type: StorageType) -> Self {
68+
pub fn new<T: Component>(storage_type: StorageType) -> Self {
6969
Self {
7070
name: std::any::type_name::<T>().to_string(),
7171
storage_type,
@@ -76,7 +76,7 @@ impl ComponentDescriptor {
7676
}
7777
}
7878

79-
pub fn non_send_from_generic<T: 'static>(storage_type: StorageType) -> Self {
79+
pub fn new_non_send_sync<T: 'static>(storage_type: StorageType) -> Self {
8080
Self {
8181
name: std::any::type_name::<T>().to_string(),
8282
storage_type,
@@ -146,12 +146,12 @@ impl SparseSetIndex for RelationKindId {
146146
}
147147

148148
#[derive(Debug)]
149-
pub struct RelationshipKindInfo {
149+
pub struct RelationKindInfo {
150150
data: ComponentDescriptor,
151151
id: RelationKindId,
152152
}
153153

154-
impl RelationshipKindInfo {
154+
impl RelationKindInfo {
155155
pub fn data_layout(&self) -> &ComponentDescriptor {
156156
&self.data
157157
}
@@ -161,98 +161,96 @@ impl RelationshipKindInfo {
161161
}
162162
}
163163

164-
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
165-
pub struct DummyInfo {
166-
rust_type: Option<TypeId>,
167-
id: DummyId,
168-
}
169-
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
170-
pub struct DummyId(usize);
171-
172-
#[derive(Default, Debug)]
173-
pub struct Relationships {
174-
kinds: Vec<RelationshipKindInfo>,
164+
#[derive(Debug, Default)]
165+
pub struct Components {
166+
kinds: Vec<RelationKindInfo>,
175167
// These are only used by bevy. Scripting/dynamic components should
176168
// use their own hashmap to lookup CustomId -> RelationshipKindId
177169
component_indices: HashMap<TypeId, RelationKindId, fxhash::FxBuildHasher>,
178170
resource_indices: HashMap<TypeId, RelationKindId, fxhash::FxBuildHasher>,
179171
}
180172

181173
#[derive(Debug, Error)]
182-
pub enum RelationshipsError {
183-
#[error("A relationship of type {0:?} already exists")]
184-
RelationshipAlreadyExists(RelationKindId),
174+
pub enum RelationsError {
175+
#[error("A component of type {name:?} ({type_id:?}) already exists")]
176+
ComponentAlreadyExists { type_id: TypeId, name: String },
177+
#[error("A resource of type {name:?} ({type_id:?}) already exists")]
178+
ResourceAlreadyExists { type_id: TypeId, name: String },
185179
}
186180

187-
impl Relationships {
188-
pub fn new_component_kind(
189-
&mut self,
190-
type_id: TypeId,
191-
layout: ComponentDescriptor,
192-
) -> &RelationshipKindInfo {
181+
impl Components {
182+
pub fn new_relation_kind(&mut self, layout: ComponentDescriptor) -> &RelationKindInfo {
183+
let id = RelationKindId(self.kinds.len());
184+
self.kinds.push(RelationKindInfo { data: layout, id });
185+
self.kinds.last().unwrap()
186+
}
187+
188+
pub fn new_component_kind(&mut self, layout: ComponentDescriptor) -> &RelationKindInfo {
193189
let id = RelationKindId(self.kinds.len());
194-
let prev_inserted = self.component_indices.insert(type_id, id);
190+
let prev_inserted = self.component_indices.insert(layout.type_id().unwrap(), id);
195191
assert!(prev_inserted.is_none());
196-
self.kinds.push(RelationshipKindInfo { data: layout, id });
192+
self.kinds.push(RelationKindInfo { data: layout, id });
197193
self.kinds.last().unwrap()
198194
}
199195

200-
pub fn new_resource_kind(
201-
&mut self,
202-
type_id: TypeId,
203-
layout: ComponentDescriptor,
204-
) -> &RelationshipKindInfo {
196+
pub fn new_resource_kind(&mut self, layout: ComponentDescriptor) -> &RelationKindInfo {
205197
let id = RelationKindId(self.kinds.len());
206-
let prev_inserted = self.resource_indices.insert(type_id, id);
198+
let prev_inserted = self.resource_indices.insert(layout.type_id().unwrap(), id);
207199
assert!(prev_inserted.is_none());
208-
self.kinds.push(RelationshipKindInfo { data: layout, id });
200+
self.kinds.push(RelationKindInfo { data: layout, id });
209201
self.kinds.last().unwrap()
210202
}
211203

212-
pub fn get_component_kind(&self, type_id: TypeId) -> Option<&RelationshipKindInfo> {
204+
pub fn get_relation_kind(&self, id: RelationKindId) -> &RelationKindInfo {
205+
self.kinds.get(id.0).unwrap()
206+
}
207+
208+
pub fn get_component_kind(&self, type_id: TypeId) -> Option<&RelationKindInfo> {
213209
let id = self.component_indices.get(&type_id).copied()?;
214210
Some(&self.kinds[id.0])
215211
}
216212

217-
pub fn get_resource_kind(&self, type_id: TypeId) -> Option<&RelationshipKindInfo> {
213+
pub fn get_resource_kind(&self, type_id: TypeId) -> Option<&RelationKindInfo> {
218214
let id = self.resource_indices.get(&type_id).copied()?;
219215
Some(&self.kinds[id.0])
220216
}
221217

222218
pub fn get_component_kind_or_insert(
223219
&mut self,
224-
type_id: TypeId,
225220
layout: ComponentDescriptor,
226-
) -> &RelationshipKindInfo {
227-
match self.component_indices.get(&type_id).copied() {
221+
) -> &RelationKindInfo {
222+
match self
223+
.component_indices
224+
.get(&layout.type_id().unwrap())
225+
.copied()
226+
{
228227
Some(kind) => &self.kinds[kind.0],
229-
None => self.new_component_kind(type_id, layout),
228+
None => self.new_component_kind(layout),
230229
}
231230
}
232231

233232
pub fn get_resource_kind_or_insert(
234233
&mut self,
235-
type_id: TypeId,
236234
layout: ComponentDescriptor,
237-
) -> &RelationshipKindInfo {
238-
match self.resource_indices.get(&type_id).copied() {
235+
) -> &RelationKindInfo {
236+
match self
237+
.resource_indices
238+
.get(&layout.type_id().unwrap())
239+
.copied()
240+
{
239241
Some(kind) => &self.kinds[kind.0],
240-
None => self.new_resource_kind(type_id, layout),
242+
None => self.new_resource_kind(layout),
241243
}
242244
}
243245

244246
#[inline]
245-
pub fn kinds_len(&self) -> usize {
247+
pub fn len(&self) -> usize {
246248
self.kinds.len()
247249
}
248250

249251
#[inline]
250-
pub fn kinds_is_empty(&self) -> bool {
251-
self.kinds.len() == 0
252-
}
253-
254-
pub fn get_relation_kind(&self, id: RelationKindId) -> Option<&RelationshipKindInfo> {
255-
self.kinds.get(id.0)
252+
pub fn is_empty(&self) -> bool {
253+
self.kinds.is_empty()
256254
}
257255
}
258256

crates/bevy_ecs/src/lib.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub mod prelude {
3939
mod tests {
4040
use crate::{
4141
bundle::Bundle,
42-
component::{Component, StorageType, TypeInfo},
42+
component::{Component, ComponentDescriptor, StorageType, TypeInfo},
4343
entity::Entity,
4444
query::{Added, ChangeTrackers, Changed, FilterFetch, With, Without, WorldQuery},
4545
world::{Mut, World},
@@ -57,7 +57,7 @@ mod tests {
5757
fn random_access() {
5858
let mut world = World::new();
5959
world
60-
.register_component::<i32>(StorageType::SparseSet)
60+
.register_component(ComponentDescriptor::new::<i32>(StorageType::SparseSet))
6161
.unwrap();
6262
let e = world.spawn().insert_bundle(("abc", 123)).id();
6363
let f = world.spawn().insert_bundle(("def", 456, true)).id();
@@ -91,7 +91,7 @@ mod tests {
9191

9292
let mut world = World::new();
9393
world
94-
.register_component::<i32>(StorageType::SparseSet)
94+
.register_component(ComponentDescriptor::new::<i32>(StorageType::SparseSet))
9595
.unwrap();
9696
let e1 = world.spawn().insert_bundle(Foo { x: "abc", y: 123 }).id();
9797
let e2 = world.spawn().insert_bundle(("def", 456, true)).id();
@@ -172,7 +172,7 @@ mod tests {
172172
fn despawn_mixed_storage() {
173173
let mut world = World::new();
174174
world
175-
.register_component::<i32>(StorageType::SparseSet)
175+
.register_component(ComponentDescriptor::new::<i32>(StorageType::SparseSet))
176176
.unwrap();
177177
let e = world.spawn().insert_bundle(("abc", 123)).id();
178178
let f = world.spawn().insert_bundle(("def", 456)).id();
@@ -322,7 +322,7 @@ mod tests {
322322
fn query_filter_with_sparse() {
323323
let mut world = World::new();
324324
world
325-
.register_component::<f32>(StorageType::SparseSet)
325+
.register_component(ComponentDescriptor::new::<f32>(StorageType::SparseSet))
326326
.unwrap();
327327
world.spawn().insert_bundle((123u32, 1.0f32));
328328
world.spawn().insert(456u32);
@@ -338,7 +338,7 @@ mod tests {
338338
fn query_filter_with_sparse_for_each() {
339339
let mut world = World::new();
340340
world
341-
.register_component::<f32>(StorageType::SparseSet)
341+
.register_component(ComponentDescriptor::new::<f32>(StorageType::SparseSet))
342342
.unwrap();
343343
world.spawn().insert_bundle((123u32, 1.0f32));
344344
world.spawn().insert(456u32);
@@ -381,7 +381,7 @@ mod tests {
381381
fn query_optional_component_sparse() {
382382
let mut world = World::new();
383383
world
384-
.register_component::<bool>(StorageType::SparseSet)
384+
.register_component(ComponentDescriptor::new::<bool>(StorageType::SparseSet))
385385
.unwrap();
386386
let e = world.spawn().insert_bundle(("abc", 123)).id();
387387
let f = world.spawn().insert_bundle(("def", 456, true)).id();
@@ -399,7 +399,7 @@ mod tests {
399399
fn query_optional_component_sparse_no_match() {
400400
let mut world = World::new();
401401
world
402-
.register_component::<bool>(StorageType::SparseSet)
402+
.register_component(ComponentDescriptor::new::<bool>(StorageType::SparseSet))
403403
.unwrap();
404404
let e = world.spawn().insert_bundle(("abc", 123)).id();
405405
let f = world.spawn().insert_bundle(("def", 456)).id();
@@ -486,7 +486,7 @@ mod tests {
486486
fn sparse_set_add_remove_many() {
487487
let mut world = World::default();
488488
world
489-
.register_component::<usize>(StorageType::SparseSet)
489+
.register_component(ComponentDescriptor::new::<usize>(StorageType::SparseSet))
490490
.unwrap();
491491
let mut entities = Vec::with_capacity(1000);
492492
for _ in 0..4 {
@@ -544,7 +544,9 @@ mod tests {
544544
fn remove_tracking() {
545545
let mut world = World::new();
546546
world
547-
.register_component::<&'static str>(StorageType::SparseSet)
547+
.register_component(ComponentDescriptor::new::<&'static str>(
548+
StorageType::SparseSet,
549+
))
548550
.unwrap();
549551
let a = world.spawn().insert_bundle(("abc", 123)).id();
550552
let b = world.spawn().insert_bundle(("abc", 123)).id();

0 commit comments

Comments
 (0)