From 6f0eef7cf88356dd090db3fa38cfa9f6fd9941b5 Mon Sep 17 00:00:00 2001 From: meehl <12648828+meehl@users.noreply.github.com> Date: Thu, 27 Aug 2026 18:02:24 +0200 Subject: [PATCH] Get rid of unwraps --- src/batcher.rs | 6 ++++-- src/renderer.rs | 10 +++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/batcher.rs b/src/batcher.rs index 365ea31..674e7c5 100644 --- a/src/batcher.rs +++ b/src/batcher.rs @@ -1,3 +1,5 @@ +use std::num::NonZeroU32; + use ahash::HashMap; use crate::{ @@ -37,12 +39,12 @@ impl BatchBuilder { /// /// Returns slot_index into textures array if texture can be assigned. /// Returns `None` if maximum amount of texture slots is reached. - fn try_assign(&mut self, id: TextureId, max_slots: u32) -> Option { + fn try_assign(&mut self, id: TextureId, max_slots: NonZeroU32) -> Option { if let Some(&slot) = self.texture_mapping.get(&id) { return Some(slot); } - if self.textures.len() as u32 >= max_slots { + if self.textures.len() as u32 >= max_slots.get() { return None; } diff --git a/src/renderer.rs b/src/renderer.rs index 87c81d6..6277a89 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -21,7 +21,7 @@ pub mod image; mod mipmap; pub mod textures; -pub const BATCH_TEX_SLOTS: u32 = 32; +pub const BATCH_TEX_SLOTS: NonZeroU32 = NonZeroU32::new(32).expect("32 is non-zero"); const DUMMY_TEXTURE_ID: TextureId = 0; #[repr(C)] @@ -162,13 +162,13 @@ impl Renderer { sample_type: wgpu::TextureSampleType::Float { filterable: true }, view_dimension: wgpu::TextureViewDimension::D2Array, }, - count: Some(NonZeroU32::new(BATCH_TEX_SLOTS).unwrap()), + count: Some(BATCH_TEX_SLOTS), }, wgpu::BindGroupLayoutEntry { binding: 1, visibility: wgpu::ShaderStages::FRAGMENT, ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), - count: Some(NonZeroU32::new(BATCH_TEX_SLOTS).unwrap()), + count: Some(BATCH_TEX_SLOTS), }, ], }); @@ -497,8 +497,8 @@ impl Renderer { // so we pad to up MAX_SLOTS with a dummy view/sampler let dummy = &self.textures[&DUMMY_TEXTURE_ID]; let dummy_sampler = self.samplers.get(&dummy.options).unwrap(); - views.resize(BATCH_TEX_SLOTS as usize, &dummy.view); - samplers.resize(BATCH_TEX_SLOTS as usize, dummy_sampler); + views.resize(BATCH_TEX_SLOTS.get() as usize, &dummy.view); + samplers.resize(BATCH_TEX_SLOTS.get() as usize, dummy_sampler); device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("batch_textures_bind_group"),