Skip to content

Commit

Permalink
Undo some unnecessary changes
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxyUwU committed Apr 15, 2021
1 parent 5212e84 commit 59ed501
Show file tree
Hide file tree
Showing 19 changed files with 300 additions and 394 deletions.
8 changes: 3 additions & 5 deletions crates/bevy_app/src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
CoreStage, PluginGroup, PluginGroupBuilder, StartupStage,
};
use bevy_ecs::{
component::{Component, StorageType},
component::{Component, ComponentDescriptor},
event::Events,
schedule::{
RunOnce, Schedule, Stage, StageLabel, State, SystemDescriptor, SystemSet, SystemStage,
Expand Down Expand Up @@ -319,10 +319,8 @@ impl AppBuilder {
/// will result in an error.
///
/// See [World::register_component]
pub fn register_component<T: Component>(&mut self, storage_type: StorageType) -> &mut Self {
self.world_mut()
.register_component::<T>(storage_type)
.unwrap();
pub fn register_component(&mut self, descriptor: ComponentDescriptor) -> &mut Self {
self.world_mut().register_component(descriptor).unwrap();
self
}

Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ pub fn impl_query_set(_input: TokenStream) -> TokenStream {
fn new_archetype(&mut self, archetype: &Archetype, system_state: &mut SystemState) {
let (#(#query,)*) = &mut self.0;
#(
// FIXME(Relationships) investigate this function
for (relation_filter, cache) in #query.relation_filter_accesses.iter_mut() {
QueryState::<#query, #filter>::new_archetype(
&#query.fetch_state,
Expand Down
15 changes: 7 additions & 8 deletions crates/bevy_ecs/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ pub use bevy_ecs_macros::Bundle;
use crate::{
archetype::ComponentStatus,
component::{
Component, ComponentTicks, RelationKindId, RelationshipKindInfo, Relationships,
StorageType, TypeInfo,
Component, ComponentTicks, Components, RelationKindId, RelationKindInfo, StorageType,
TypeInfo,
},
entity::Entity,
storage::{SparseSetIndex, SparseSets, Table},
};
use bevy_ecs_macros::all_tuples;
use std::{any::TypeId, collections::HashMap, u8};
use std::{any::TypeId, collections::HashMap};

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

pub(crate) fn init_relationship_info<'a>(
&'a mut self,
relation_kind: &RelationshipKindInfo,
relation_kind: &RelationKindInfo,
relation_target: Option<Entity>,
) -> &'a BundleInfo {
let bundle_infos = &mut self.bundle_infos;
Expand All @@ -259,7 +259,7 @@ impl Bundles {

pub(crate) fn init_info<'a, T: Bundle>(
&'a mut self,
components: &mut Relationships,
components: &mut Components,
) -> &'a BundleInfo {
let bundle_infos = &mut self.bundle_infos;
let id = self.bundle_ids.entry(TypeId::of::<T>()).or_insert_with(|| {
Expand All @@ -278,14 +278,13 @@ fn initialize_bundle(
bundle_type_name: &'static str,
type_info: &[TypeInfo],
id: BundleId,
components: &mut Relationships,
components: &mut Components,
) -> BundleInfo {
let mut component_ids = Vec::new();
let mut storage_types = Vec::new();

for type_info in type_info {
let kind_info =
components.get_component_kind_or_insert(type_info.type_id(), type_info.clone().into());
let kind_info = components.get_component_kind_or_insert(type_info.clone().into());
component_ids.push((kind_info.id(), None));
storage_types.push(kind_info.data_layout().storage_type());
}
Expand Down
100 changes: 49 additions & 51 deletions crates/bevy_ecs/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct ComponentDescriptor {
}

impl ComponentDescriptor {
pub unsafe fn new(
pub unsafe fn new_dynamic(
name: Option<String>,
storage_type: StorageType,
is_send_and_sync: bool,
Expand All @@ -65,7 +65,7 @@ impl ComponentDescriptor {
}
}

pub fn from_generic<T: Component>(storage_type: StorageType) -> Self {
pub fn new<T: Component>(storage_type: StorageType) -> Self {
Self {
name: std::any::type_name::<T>().to_string(),
storage_type,
Expand All @@ -76,7 +76,7 @@ impl ComponentDescriptor {
}
}

pub fn non_send_from_generic<T: 'static>(storage_type: StorageType) -> Self {
pub fn new_non_send_sync<T: 'static>(storage_type: StorageType) -> Self {
Self {
name: std::any::type_name::<T>().to_string(),
storage_type,
Expand Down Expand Up @@ -146,12 +146,12 @@ impl SparseSetIndex for RelationKindId {
}

#[derive(Debug)]
pub struct RelationshipKindInfo {
pub struct RelationKindInfo {
data: ComponentDescriptor,
id: RelationKindId,
}

impl RelationshipKindInfo {
impl RelationKindInfo {
pub fn data_layout(&self) -> &ComponentDescriptor {
&self.data
}
Expand All @@ -161,98 +161,96 @@ impl RelationshipKindInfo {
}
}

#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
pub struct DummyInfo {
rust_type: Option<TypeId>,
id: DummyId,
}
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
pub struct DummyId(usize);

#[derive(Default, Debug)]
pub struct Relationships {
kinds: Vec<RelationshipKindInfo>,
#[derive(Debug, Default)]
pub struct Components {
kinds: Vec<RelationKindInfo>,
// These are only used by bevy. Scripting/dynamic components should
// use their own hashmap to lookup CustomId -> RelationshipKindId
component_indices: HashMap<TypeId, RelationKindId, fxhash::FxBuildHasher>,
resource_indices: HashMap<TypeId, RelationKindId, fxhash::FxBuildHasher>,
}

#[derive(Debug, Error)]
pub enum RelationshipsError {
#[error("A relationship of type {0:?} already exists")]
RelationshipAlreadyExists(RelationKindId),
pub enum RelationsError {
#[error("A component of type {name:?} ({type_id:?}) already exists")]
ComponentAlreadyExists { type_id: TypeId, name: String },
#[error("A resource of type {name:?} ({type_id:?}) already exists")]
ResourceAlreadyExists { type_id: TypeId, name: String },
}

impl Relationships {
pub fn new_component_kind(
&mut self,
type_id: TypeId,
layout: ComponentDescriptor,
) -> &RelationshipKindInfo {
impl Components {
pub fn new_relation_kind(&mut self, layout: ComponentDescriptor) -> &RelationKindInfo {
let id = RelationKindId(self.kinds.len());
self.kinds.push(RelationKindInfo { data: layout, id });
self.kinds.last().unwrap()
}

pub fn new_component_kind(&mut self, layout: ComponentDescriptor) -> &RelationKindInfo {
let id = RelationKindId(self.kinds.len());
let prev_inserted = self.component_indices.insert(type_id, id);
let prev_inserted = self.component_indices.insert(layout.type_id().unwrap(), id);
assert!(prev_inserted.is_none());
self.kinds.push(RelationshipKindInfo { data: layout, id });
self.kinds.push(RelationKindInfo { data: layout, id });
self.kinds.last().unwrap()
}

pub fn new_resource_kind(
&mut self,
type_id: TypeId,
layout: ComponentDescriptor,
) -> &RelationshipKindInfo {
pub fn new_resource_kind(&mut self, layout: ComponentDescriptor) -> &RelationKindInfo {
let id = RelationKindId(self.kinds.len());
let prev_inserted = self.resource_indices.insert(type_id, id);
let prev_inserted = self.resource_indices.insert(layout.type_id().unwrap(), id);
assert!(prev_inserted.is_none());
self.kinds.push(RelationshipKindInfo { data: layout, id });
self.kinds.push(RelationKindInfo { data: layout, id });
self.kinds.last().unwrap()
}

pub fn get_component_kind(&self, type_id: TypeId) -> Option<&RelationshipKindInfo> {
pub fn get_relation_kind(&self, id: RelationKindId) -> &RelationKindInfo {
self.kinds.get(id.0).unwrap()
}

pub fn get_component_kind(&self, type_id: TypeId) -> Option<&RelationKindInfo> {
let id = self.component_indices.get(&type_id).copied()?;
Some(&self.kinds[id.0])
}

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

pub fn get_component_kind_or_insert(
&mut self,
type_id: TypeId,
layout: ComponentDescriptor,
) -> &RelationshipKindInfo {
match self.component_indices.get(&type_id).copied() {
) -> &RelationKindInfo {
match self
.component_indices
.get(&layout.type_id().unwrap())
.copied()
{
Some(kind) => &self.kinds[kind.0],
None => self.new_component_kind(type_id, layout),
None => self.new_component_kind(layout),
}
}

pub fn get_resource_kind_or_insert(
&mut self,
type_id: TypeId,
layout: ComponentDescriptor,
) -> &RelationshipKindInfo {
match self.resource_indices.get(&type_id).copied() {
) -> &RelationKindInfo {
match self
.resource_indices
.get(&layout.type_id().unwrap())
.copied()
{
Some(kind) => &self.kinds[kind.0],
None => self.new_resource_kind(type_id, layout),
None => self.new_resource_kind(layout),
}
}

#[inline]
pub fn kinds_len(&self) -> usize {
pub fn len(&self) -> usize {
self.kinds.len()
}

#[inline]
pub fn kinds_is_empty(&self) -> bool {
self.kinds.len() == 0
}

pub fn get_relation_kind(&self, id: RelationKindId) -> Option<&RelationshipKindInfo> {
self.kinds.get(id.0)
pub fn is_empty(&self) -> bool {
self.kinds.is_empty()
}
}

Expand Down
22 changes: 12 additions & 10 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub mod prelude {
mod tests {
use crate::{
bundle::Bundle,
component::{Component, StorageType, TypeInfo},
component::{Component, ComponentDescriptor, StorageType, TypeInfo},
entity::Entity,
query::{Added, ChangeTrackers, Changed, FilterFetch, With, Without, WorldQuery},
world::{Mut, World},
Expand All @@ -57,7 +57,7 @@ mod tests {
fn random_access() {
let mut world = World::new();
world
.register_component::<i32>(StorageType::SparseSet)
.register_component(ComponentDescriptor::new::<i32>(StorageType::SparseSet))
.unwrap();
let e = world.spawn().insert_bundle(("abc", 123)).id();
let f = world.spawn().insert_bundle(("def", 456, true)).id();
Expand Down Expand Up @@ -91,7 +91,7 @@ mod tests {

let mut world = World::new();
world
.register_component::<i32>(StorageType::SparseSet)
.register_component(ComponentDescriptor::new::<i32>(StorageType::SparseSet))
.unwrap();
let e1 = world.spawn().insert_bundle(Foo { x: "abc", y: 123 }).id();
let e2 = world.spawn().insert_bundle(("def", 456, true)).id();
Expand Down Expand Up @@ -172,7 +172,7 @@ mod tests {
fn despawn_mixed_storage() {
let mut world = World::new();
world
.register_component::<i32>(StorageType::SparseSet)
.register_component(ComponentDescriptor::new::<i32>(StorageType::SparseSet))
.unwrap();
let e = world.spawn().insert_bundle(("abc", 123)).id();
let f = world.spawn().insert_bundle(("def", 456)).id();
Expand Down Expand Up @@ -322,7 +322,7 @@ mod tests {
fn query_filter_with_sparse() {
let mut world = World::new();
world
.register_component::<f32>(StorageType::SparseSet)
.register_component(ComponentDescriptor::new::<f32>(StorageType::SparseSet))
.unwrap();
world.spawn().insert_bundle((123u32, 1.0f32));
world.spawn().insert(456u32);
Expand All @@ -338,7 +338,7 @@ mod tests {
fn query_filter_with_sparse_for_each() {
let mut world = World::new();
world
.register_component::<f32>(StorageType::SparseSet)
.register_component(ComponentDescriptor::new::<f32>(StorageType::SparseSet))
.unwrap();
world.spawn().insert_bundle((123u32, 1.0f32));
world.spawn().insert(456u32);
Expand Down Expand Up @@ -381,7 +381,7 @@ mod tests {
fn query_optional_component_sparse() {
let mut world = World::new();
world
.register_component::<bool>(StorageType::SparseSet)
.register_component(ComponentDescriptor::new::<bool>(StorageType::SparseSet))
.unwrap();
let e = world.spawn().insert_bundle(("abc", 123)).id();
let f = world.spawn().insert_bundle(("def", 456, true)).id();
Expand All @@ -399,7 +399,7 @@ mod tests {
fn query_optional_component_sparse_no_match() {
let mut world = World::new();
world
.register_component::<bool>(StorageType::SparseSet)
.register_component(ComponentDescriptor::new::<bool>(StorageType::SparseSet))
.unwrap();
let e = world.spawn().insert_bundle(("abc", 123)).id();
let f = world.spawn().insert_bundle(("def", 456)).id();
Expand Down Expand Up @@ -486,7 +486,7 @@ mod tests {
fn sparse_set_add_remove_many() {
let mut world = World::default();
world
.register_component::<usize>(StorageType::SparseSet)
.register_component(ComponentDescriptor::new::<usize>(StorageType::SparseSet))
.unwrap();
let mut entities = Vec::with_capacity(1000);
for _ in 0..4 {
Expand Down Expand Up @@ -544,7 +544,9 @@ mod tests {
fn remove_tracking() {
let mut world = World::new();
world
.register_component::<&'static str>(StorageType::SparseSet)
.register_component(ComponentDescriptor::new::<&'static str>(
StorageType::SparseSet,
))
.unwrap();
let a = world.spawn().insert_bundle(("abc", 123)).id();
let b = world.spawn().insert_bundle(("abc", 123)).id();
Expand Down
Loading

0 comments on commit 59ed501

Please sign in to comment.