Skip to content

Commit 2583764

Browse files
committed
Prevent creation of superfluous empty table
1 parent 2027700 commit 2583764

File tree

1 file changed

+17
-1
lines changed
  • crates/bevy_ecs/src/storage/table

1 file changed

+17
-1
lines changed

crates/bevy_ecs/src/storage/table/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,10 @@ impl Tables {
743743
component_ids: &[ComponentId],
744744
components: &Components,
745745
) -> TableId {
746+
if component_ids.is_empty() {
747+
return TableId::empty();
748+
}
749+
746750
let tables = &mut self.tables;
747751
let (_key, value) = self
748752
.table_ids
@@ -816,14 +820,26 @@ mod tests {
816820
component::{Component, Components, Tick},
817821
entity::Entity,
818822
ptr::OwningPtr,
819-
storage::{Storages, TableBuilder, TableRow},
823+
storage::{Storages, TableBuilder, TableId, TableRow, Tables},
820824
};
821825
#[cfg(feature = "track_change_detection")]
822826
use core::panic::Location;
823827

824828
#[derive(Component)]
825829
struct W<T>(T);
826830

831+
#[test]
832+
fn only_one_empty_table() {
833+
let components = Components::default();
834+
let mut tables = Tables::default();
835+
836+
let component_ids = &[];
837+
// SAFETY: component_ids is empty, so we know it cannot reference invalid component IDs
838+
let table_id = unsafe { tables.get_id_or_insert(component_ids, &components) };
839+
840+
assert_eq!(table_id, TableId::empty());
841+
}
842+
827843
#[test]
828844
fn table() {
829845
let mut components = Components::default();

0 commit comments

Comments
 (0)