From defeeb375b5a419597dd28b8cf7fcf8140887767 Mon Sep 17 00:00:00 2001 From: Robin KAY Date: Mon, 19 Aug 2024 22:09:20 +0100 Subject: [PATCH] Fix size of MeshVertexAttributeId to be platform independent (#6624) # Objective `MeshVertexAttributeId` is currently a wrapper type around a `usize`. Application developers are exposed to the `usize` whenever they need to define a new custom vertex attribute, which requires them to generate a random `usize` ID to avoid clashes with any other custom vertex attributes in the same application. As the range of a `usize` is platform dependent, developers on 64-bit machines may inadvertently generate random values which will fail to compile for a 32-bit target. The use of a `usize` here encourages non-portable behaviour and should be replaced with a fixed width type. ## Solution In this PR I have changed the ID type from `usize` to `u64`, but equally a `u32` could be used at the risk of breaking some extant non-portable programs and increasing the chance of an ID collision. --- crates/bevy_render/src/mesh/mesh/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_render/src/mesh/mesh/mod.rs b/crates/bevy_render/src/mesh/mesh/mod.rs index 507084df15..9509ae1ffb 100644 --- a/crates/bevy_render/src/mesh/mesh/mod.rs +++ b/crates/bevy_render/src/mesh/mesh/mod.rs @@ -1289,7 +1289,7 @@ pub struct MeshVertexAttribute { /// The _unique_ id of the vertex attribute. This will also determine sort ordering /// when generating vertex buffers. Built-in / standard attributes will use "close to zero" - /// indices. When in doubt, use a random / very large usize to avoid conflicts. + /// indices. When in doubt, use a random / very large u64 to avoid conflicts. pub id: MeshVertexAttributeId, /// The format of the vertex attribute. @@ -1297,7 +1297,7 @@ pub struct MeshVertexAttribute { } impl MeshVertexAttribute { - pub const fn new(name: &'static str, id: usize, format: VertexFormat) -> Self { + pub const fn new(name: &'static str, id: u64, format: VertexFormat) -> Self { Self { name, id: MeshVertexAttributeId(id), @@ -1311,7 +1311,7 @@ impl MeshVertexAttribute { } #[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)] -pub struct MeshVertexAttributeId(usize); +pub struct MeshVertexAttributeId(u64); impl From for MeshVertexAttributeId { fn from(attribute: MeshVertexAttribute) -> Self {