From 012b60b73178f80e479588d04683a7f7a7739f8c Mon Sep 17 00:00:00 2001 From: ebola Date: Sun, 18 Dec 2022 07:20:41 -0500 Subject: [PATCH 1/2] Constify SpritePipelineKey implementation. --- crates/bevy_sprite/src/render/mod.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/crates/bevy_sprite/src/render/mod.rs b/crates/bevy_sprite/src/render/mod.rs index 1cdc6d93782d8..97229cb6dd0e6 100644 --- a/crates/bevy_sprite/src/render/mod.rs +++ b/crates/bevy_sprite/src/render/mod.rs @@ -160,17 +160,22 @@ impl SpritePipelineKey { const MSAA_MASK_BITS: u32 = 0b111; const MSAA_SHIFT_BITS: u32 = 32 - Self::MSAA_MASK_BITS.count_ones(); - pub fn from_msaa_samples(msaa_samples: u32) -> Self { + #[inline] + pub const fn from_msaa_samples(msaa_samples: u32) -> Self { let msaa_bits = (msaa_samples.trailing_zeros() & Self::MSAA_MASK_BITS) << Self::MSAA_SHIFT_BITS; - Self::from_bits(msaa_bits).unwrap() + // SAFETY: The input is the correct type and the mask will ensure the output range is + // capped + unsafe { Self::from_bits_unchecked(msaa_bits) } } - pub fn msaa_samples(&self) -> u32 { + #[inline] + pub const fn msaa_samples(&self) -> u32 { 1 << ((self.bits >> Self::MSAA_SHIFT_BITS) & Self::MSAA_MASK_BITS) } - pub fn from_colored(colored: bool) -> Self { + #[inline] + pub const fn from_colored(colored: bool) -> Self { if colored { SpritePipelineKey::COLORED } else { @@ -178,7 +183,8 @@ impl SpritePipelineKey { } } - pub fn from_hdr(hdr: bool) -> Self { + #[inline] + pub const fn from_hdr(hdr: bool) -> Self { if hdr { SpritePipelineKey::HDR } else { From 4d0fcb6632b08921404ff5a9d97beeaf47b65a62 Mon Sep 17 00:00:00 2001 From: ebola Date: Wed, 21 Dec 2022 23:08:59 -0500 Subject: [PATCH 2/2] Use bitflags::from_bits_truncate instead of unsafe bitflags::from_bits_unchecked in order to be const. --- crates/bevy_sprite/src/render/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/bevy_sprite/src/render/mod.rs b/crates/bevy_sprite/src/render/mod.rs index 97229cb6dd0e6..765679b09b425 100644 --- a/crates/bevy_sprite/src/render/mod.rs +++ b/crates/bevy_sprite/src/render/mod.rs @@ -164,9 +164,7 @@ impl SpritePipelineKey { pub const fn from_msaa_samples(msaa_samples: u32) -> Self { let msaa_bits = (msaa_samples.trailing_zeros() & Self::MSAA_MASK_BITS) << Self::MSAA_SHIFT_BITS; - // SAFETY: The input is the correct type and the mask will ensure the output range is - // capped - unsafe { Self::from_bits_unchecked(msaa_bits) } + Self::from_bits_truncate(msaa_bits) } #[inline]