{"record":{"id":"d2573b6356d0b77d","repo":"bevyengine/bevy","slug":"attempted-to-initialize-invalid-bits-as-an-entity","errorCode":null,"errorMessage":"Attempted to initialize invalid bits as an entity index","messagePattern":"Attempted to initialize invalid bits as an entity index","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bevy_ecs/src/entity/mod.rs","lineNumber":194,"sourceCode":"\n    /// Gets some bits that represent this value.\n    /// The bits are opaque and should not be regarded as meaningful.\n    #[inline(always)]\n    const fn to_bits(self) -> u32 {\n        // SAFETY: NonMax is repr transparent.\n        unsafe { mem::transmute::<NonMaxU32, u32>(self.0) }\n    }\n\n    /// Reconstruct an [`EntityIndex`] previously destructured with [`EntityIndex::to_bits`].\n    ///\n    /// Only useful when applied to results from `to_bits` in the same instance of an application.\n    ///\n    /// # Panics\n    ///\n    /// This method will likely panic if given `u32` values that did not come from [`EntityIndex::to_bits`].\n    #[inline]\n    const fn from_bits(bits: u32) -> Self {\n        Self::try_from_bits(bits).expect(\"Attempted to initialize invalid bits as an entity index\")\n    }\n\n    /// Reconstruct an [`EntityIndex`] previously destructured with [`EntityIndex::to_bits`].\n    ///\n    /// Only useful when applied to results from `to_bits` in the same instance of an application.\n    ///\n    /// This method is the fallible counterpart to [`EntityIndex::from_bits`].\n    #[inline(always)]\n    const fn try_from_bits(bits: u32) -> Option<Self> {\n        match NonZero::<u32>::new(bits) {\n            // SAFETY: NonMax and NonZero are repr transparent.\n            Some(underlying) => Some(Self(unsafe {\n                mem::transmute::<NonZero<u32>, NonMaxU32>(underlying)\n            })),\n            None => None,\n        }\n    }\n}","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_ecs/src/entity/mod.rs#L176-L212","documentation":"Runtime panic from EntityIndex::from_bits: the u32 argument does not encode a valid NonMax entity index (i.e. it is the sentinel u32::MAX reserved to mark 'no entity'). Bits are only meaningful when produced by to_bits in the same running application; reconstructing any other value hits this debug assertion-style guard.","triggerScenarios":"Thrown at crates/bevy_ecs/src/entity/mod.rs:194 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Only feed from_bits values that came from EntityIndex::to_bits in the same process","Persist entities via a proper serialization mapping instead of raw bits","If storing externally, check the value is not u32::MAX before reconstructing"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}