From 8c51f91d00d17534dce430593b906cf05b6f75d0 Mon Sep 17 00:00:00 2001 From: nullprop Date: Sat, 28 Jan 2023 14:11:54 +0200 Subject: [PATCH] clippy suggestions --- src/core/camera.rs | 32 ++++++++++++++--------------- src/core/instance.rs | 4 ++-- src/core/light.rs | 4 ++-- src/core/model.rs | 4 ++-- src/core/resources.rs | 8 ++------ src/core/state.rs | 14 ++++++------- src/core/texture.rs | 40 ++----------------------------------- src/lib.rs | 2 -- src/shaders/preprocessor.rs | 2 +- 9 files changed, 33 insertions(+), 77 deletions(-) diff --git a/src/core/camera.rs b/src/core/camera.rs index ae00432..ca7c0a2 100644 --- a/src/core/camera.rs +++ b/src/core/camera.rs @@ -23,7 +23,7 @@ impl Projection { } pub fn get_matrix(&self) -> cgmath::Matrix4 { - return cgmath::perspective(cgmath::Deg(self.fovy), self.aspect, self.znear, self.zfar); + cgmath::perspective(cgmath::Deg(self.fovy), self.aspect, self.znear, self.zfar) } } @@ -50,7 +50,7 @@ impl Camera { pub fn get_view_matrix(&self) -> cgmath::Matrix4 { let (_right, up, forward) = self.get_vecs(); - return cgmath::Matrix4::look_to_rh(self.position, forward, up); + cgmath::Matrix4::look_to_rh(self.position, forward, up) } pub fn get_vecs( @@ -67,7 +67,7 @@ impl Camera { cgmath::Vector3::new(pitch_cos * yaw_cos, pitch_sin, pitch_cos * yaw_sin).normalize(); let right = cgmath::Vector3::new(-yaw_sin, 0.0, yaw_cos).normalize(); let up = right.cross(forward); - return (right, up, forward); + (right, up, forward) } pub fn update(&mut self, dt: Duration, controller: &CameraController) { @@ -168,7 +168,7 @@ impl CameraController { window_event: Option<&WindowEvent>, device_event: Option<&DeviceEvent>, ) -> bool { - let mut handled = match window_event { + let handled = match window_event { None => false, Some(event) => match event { WindowEvent::KeyboardInput { @@ -185,27 +185,27 @@ impl CameraController { match keycode { VirtualKeyCode::W | VirtualKeyCode::Up => { self.move_forward = amount; - return true; + true } VirtualKeyCode::A | VirtualKeyCode::Left => { self.move_left = amount; - return true; + true } VirtualKeyCode::S | VirtualKeyCode::Down => { self.move_backward = amount; - return true; + true } VirtualKeyCode::D | VirtualKeyCode::Right => { self.move_right = amount; - return true; + true } VirtualKeyCode::Space => { self.move_up = amount; - return true; + true } VirtualKeyCode::LControl => { self.move_down = amount; - return true; + true } _ => false, } @@ -217,7 +217,7 @@ impl CameraController { } else { self.speed /= 2.0; } - return true; + true } MouseScrollDelta::PixelDelta(PhysicalPosition { y: scroll, .. }) => { if *scroll > 0.0 { @@ -225,7 +225,7 @@ impl CameraController { } else { self.speed /= 2.0; } - return true; + true } }, _ => false, @@ -236,15 +236,13 @@ impl CameraController { return true; } - handled = match device_event { + match device_event { Some(DeviceEvent::MouseMotion { delta }) => { self.deltax += delta.0 as f32; self.deltay += delta.1 as f32; - return true; + true } _ => false, - }; - - return handled; + } } } diff --git a/src/core/instance.rs b/src/core/instance.rs index 22c0491..d4a40e8 100644 --- a/src/core/instance.rs +++ b/src/core/instance.rs @@ -14,12 +14,12 @@ pub struct InstanceRaw { impl Instance { pub fn to_raw(&self) -> InstanceRaw { - return InstanceRaw { + InstanceRaw { model: (cgmath::Matrix4::from_translation(self.position) * cgmath::Matrix4::from(self.rotation)) .into(), normal: cgmath::Matrix3::from(self.rotation).into(), - }; + } } } diff --git a/src/core/light.rs b/src/core/light.rs index 3f7c8c3..157896c 100644 --- a/src/core/light.rs +++ b/src/core/light.rs @@ -12,11 +12,11 @@ pub struct LightUniform { impl LightUniform { pub fn new(position: [f32; 3], color: [f32; 4]) -> Self { - return LightUniform { + Self { position, _padding: 0, color, - }; + } } } diff --git a/src/core/model.rs b/src/core/model.rs index 3c47fa1..464eb0e 100644 --- a/src/core/model.rs +++ b/src/core/model.rs @@ -57,7 +57,7 @@ impl Material { label: None, }); - return Self { + Self { name: String::from(name), diffuse_texture, normal_texture, @@ -65,7 +65,7 @@ impl Material { metallic_factor, roughness_factor, bind_group, - }; + } } } diff --git a/src/core/resources.rs b/src/core/resources.rs index 8a8553b..14511ea 100644 --- a/src/core/resources.rs +++ b/src/core/resources.rs @@ -8,10 +8,6 @@ use crate::core::texture::Texture; #[folder = "res"] struct Asset; -pub fn load_binary(file_name: &str) -> Vec { - Asset::get(file_name).unwrap().data.into_owned() -} - pub fn load_string(file_name: &str) -> String { let binary = Asset::get(file_name).unwrap(); std::str::from_utf8(binary.data.as_ref()).unwrap().to_owned() @@ -258,7 +254,7 @@ pub async fn load_model_gltf( materials.push(Material::new( device, - &material.name().unwrap_or("Default Material"), + material.name().unwrap_or("Default Material"), diffuse_texture, normal_texture, rm_texture, @@ -336,5 +332,5 @@ fn gltf_pixels_to_wgpu(mut bytes: Vec, format: gltf::image::Format) -> Vec) { @@ -322,9 +322,9 @@ impl State { window_event: Option<&WindowEvent>, device_event: Option<&DeviceEvent>, ) -> bool { - return self + self .camera_controller - .process_events(window_event, device_event); + .process_events(window_event, device_event) } pub fn update(&mut self, dt: Duration) { @@ -411,7 +411,7 @@ impl State { self.queue.submit(std::iter::once(encoder.finish())); output.present(); - return Ok(()); + Ok(()) } } @@ -425,7 +425,7 @@ fn create_render_pipeline( ) -> wgpu::RenderPipeline { let shader = device.create_shader_module(shader); - return device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some("Render Pipeline"), layout: Some(layout), vertex: wgpu::VertexState { @@ -470,5 +470,5 @@ fn create_render_pipeline( alpha_to_coverage_enabled: false, }, multiview: None, - }); + }) } diff --git a/src/core/texture.rs b/src/core/texture.rs index ae55b4b..f56640a 100644 --- a/src/core/texture.rs +++ b/src/core/texture.rs @@ -1,5 +1,4 @@ use anyhow::*; -use image::GenericImageView; pub struct Texture { pub texture: wgpu::Texture, @@ -52,41 +51,6 @@ impl Texture { } } - pub fn from_bytes( - device: &wgpu::Device, - queue: &wgpu::Queue, - bytes: &[u8], - label: &str, - is_normal_map: bool, - ) -> Result { - let img = image::load_from_memory(bytes)?; - return Self::from_image(device, queue, &img, Some(label), is_normal_map); - } - - pub fn from_image( - device: &wgpu::Device, - queue: &wgpu::Queue, - img: &image::DynamicImage, - label: Option<&str>, - is_normal_map: bool, - ) -> Result { - let rgba = img.to_rgba8(); - let dimensions = img.dimensions(); - return Self::from_pixels( - device, - queue, - &rgba, - dimensions, - 4, - if is_normal_map { - wgpu::TextureFormat::Rgba8UnormSrgb - } else { - wgpu::TextureFormat::Rgba8Unorm - }, - label, - ); - } - pub fn from_pixels( device: &wgpu::Device, queue: &wgpu::Queue, @@ -140,10 +104,10 @@ impl Texture { ..Default::default() }); - return Ok(Self { + Ok(Self { texture, view, sampler, - }); + }) } } diff --git a/src/lib.rs b/src/lib.rs index 0bc75ad..88af929 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,3 @@ -#![allow(clippy::needless_return)] - #[cfg(target_arch = "wasm32")] use wasm_bindgen::prelude::*; diff --git a/src/shaders/preprocessor.rs b/src/shaders/preprocessor.rs index 793d5e2..e28fe84 100644 --- a/src/shaders/preprocessor.rs +++ b/src/shaders/preprocessor.rs @@ -19,5 +19,5 @@ pub fn preprocess_wgsl(filename: &str) -> wgpu::ShaderSource { source = source.replace(whole_match, &nested_source); } - return wgpu::ShaderSource::Wgsl(source.into()); + wgpu::ShaderSource::Wgsl(source.into()) }