From a3bff9ee1cdcd1fe79e9bdc50696d243e8b1a5cb Mon Sep 17 00:00:00 2001 From: James Liu Date: Wed, 11 Jan 2023 23:31:22 +0000 Subject: [PATCH] Mark TableRow and TableId as repr(transparent) (#7166) # Objective Following #6681, both `TableRow` and `TableId` are now part of `EntityLocation`. However, the safety invariant on `EntityLocation` requires that all of the constituent fields are `repr(transprent)` or `repr(C)` and the bit pattern of all 1s must be valid. This is not true for `TableRow` and `TableId` currently. ## Solution Mark `TableRow` and `TableId` to satisfy the safety requirement. Add safety comments on `ArchetypeId`, `ArchetypeRow`, `TableId` and `TableRow`. --- crates/bevy_ecs/src/archetype.rs | 2 ++ crates/bevy_ecs/src/storage/table.rs | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/crates/bevy_ecs/src/archetype.rs b/crates/bevy_ecs/src/archetype.rs index e282901f965f1..a25551f013da3 100644 --- a/crates/bevy_ecs/src/archetype.rs +++ b/crates/bevy_ecs/src/archetype.rs @@ -40,6 +40,7 @@ use std::{ /// [`World`]: crate::world::World /// [`Entities::get`]: crate::entity::Entities #[derive(Debug, Copy, Clone, Eq, PartialEq)] +// SAFETY: Must be repr(transparent) due to the safety requirements on EntityLocation #[repr(transparent)] pub struct ArchetypeRow(u32); @@ -68,6 +69,7 @@ impl ArchetypeRow { /// [`World`]: crate::world::World /// [`EMPTY`]: crate::archetype::ArchetypeId::EMPTY #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +// SAFETY: Must be repr(transparent) due to the safety requirements on EntityLocation #[repr(transparent)] pub struct ArchetypeId(u32); diff --git a/crates/bevy_ecs/src/storage/table.rs b/crates/bevy_ecs/src/storage/table.rs index 5172ec41526e3..fe2fd6cd783ba 100644 --- a/crates/bevy_ecs/src/storage/table.rs +++ b/crates/bevy_ecs/src/storage/table.rs @@ -26,6 +26,8 @@ use std::{ /// [`Archetype`]: crate::archetype::Archetype /// [`Archetype::table_id`]: crate::archetype::Archetype::table_id #[derive(Debug, Clone, Copy, PartialEq, Eq)] +// SAFETY: Must be repr(transparent) due to the safety requirements on EntityLocation +#[repr(transparent)] pub struct TableId(u32); impl TableId { @@ -64,6 +66,8 @@ impl TableId { /// [`Archetype::table_id`]: crate::archetype::Archetype::table_id /// [`Entity`]: crate::entity::Entity #[derive(Debug, Clone, Copy, PartialEq, Eq)] +// SAFETY: Must be repr(transparent) due to the safety requirements on EntityLocation +#[repr(transparent)] pub struct TableRow(u32); impl TableRow {