From 7be921495d94c161d7a419589808c01da8765a10 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sat, 4 Jan 2020 13:20:28 -0500 Subject: [PATCH] Fix clippy warnings and errors --- ffi/wgpu.h | 2 +- wgpu-core/src/command/bind.rs | 2 +- wgpu-core/src/command/compute.rs | 4 ++-- wgpu-core/src/command/mod.rs | 6 +++--- wgpu-core/src/command/render.rs | 4 ++-- wgpu-core/src/device.rs | 26 +++++++++++--------------- wgpu-core/src/id.rs | 6 +++--- wgpu-core/src/resource.rs | 4 ++-- wgpu-core/src/swap_chain.rs | 6 +++--- wgpu-core/src/track/range.rs | 2 +- wgpu-core/src/track/texture.rs | 2 +- wgpu-native/src/command.rs | 14 +++++++------- wgpu-native/src/device.rs | 22 +++++++++++----------- 13 files changed, 48 insertions(+), 52 deletions(-) diff --git a/ffi/wgpu.h b/ffi/wgpu.h index 82864978ce..c18e13d6cc 100644 --- a/ffi/wgpu.h +++ b/ffi/wgpu.h @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -/* Generated with cbindgen:0.12.1 */ +/* Generated with cbindgen:0.12.0 */ /* DO NOT MODIFY THIS MANUALLY! This file was generated using cbindgen. * To generate this file: diff --git a/wgpu-core/src/command/bind.rs b/wgpu-core/src/command/bind.rs index a10eb65096..63947f94fd 100644 --- a/wgpu-core/src/command/bind.rs +++ b/wgpu-core/src/command/bind.rs @@ -211,6 +211,6 @@ impl Binder { self.entries .iter() .position(|entry| !entry.is_valid()) - .unwrap_or(self.entries.len()) + .unwrap_or_else(|| self.entries.len()) } } diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index d722e40678..f2bba759cd 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -210,9 +210,9 @@ impl Global { } // Rebind resources - if pass.binder.pipeline_layout_id != Some(pipeline.layout_id.clone()) { + if pass.binder.pipeline_layout_id != Some(pipeline.layout_id) { let pipeline_layout = &pipeline_layout_guard[pipeline.layout_id]; - pass.binder.pipeline_layout_id = Some(pipeline.layout_id.clone()); + pass.binder.pipeline_layout_id = Some(pipeline.layout_id); pass.binder .reset_expectations(pipeline_layout.bind_group_layout_ids.len()); let mut is_compatible = true; diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index cfd5a3d4aa..0657ce2d2a 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -44,7 +44,7 @@ use crate::{ use arrayvec::ArrayVec; use hal::{adapter::PhysicalDevice as _, command::CommandBuffer as _, device::Device as _}; -use std::{borrow::Borrow, collections::hash_map::Entry, iter, mem, ptr, slice, thread::ThreadId}; +use std::{borrow::Borrow, collections::hash_map::Entry, iter, mem, slice, thread::ThreadId}; pub struct RenderBundle { @@ -484,10 +484,10 @@ impl> Global { let mut attachment_index = color_attachments.len(); if color_attachments .iter() - .any(|at| at.resolve_target != ptr::null()) + .any(|at| !at.resolve_target.is_null()) { for (i, at) in color_attachments.iter().enumerate() { - if at.resolve_target == ptr::null() { + if at.resolve_target.is_null() { resolve_ids.push(( hal::pass::ATTACHMENT_UNUSED, hal::image::Layout::ColorAttachmentOptimal, diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 75892fb1d5..3ca9004ae1 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -510,9 +510,9 @@ impl> Global { } // Rebind resource - if pass.binder.pipeline_layout_id != Some(pipeline.layout_id.clone()) { + if pass.binder.pipeline_layout_id != Some(pipeline.layout_id) { let pipeline_layout = &pipeline_layout_guard[pipeline.layout_id]; - pass.binder.pipeline_layout_id = Some(pipeline.layout_id.clone()); + pass.binder.pipeline_layout_id = Some(pipeline.layout_id); pass.binder .reset_expectations(pipeline_layout.bind_group_layout_ids.len()); let mut is_compatible = true; diff --git a/wgpu-core/src/device.rs b/wgpu-core/src/device.rs index 062930e60c..c140641c6d 100644 --- a/wgpu-core/src/device.rs +++ b/wgpu-core/src/device.rs @@ -218,7 +218,7 @@ impl PendingResources { .active .iter() .position(|a| unsafe { !device.get_fence_status(&a.fence).unwrap() }) - .unwrap_or(self.active.len()); + .unwrap_or_else(|| self.active.len()); for a in self.active.drain(.. done_count) { log::trace!("Active submission {} is done", a.index); @@ -541,7 +541,7 @@ impl Device { min_device_allocation: 0x1_00_00, }), }; - (mt.properties.into(), mt.heap_index as u32, config) + (mt.properties, mt.heap_index as u32, config) }); unsafe { Heaps::new(types, mem_props.memory_heaps.iter().cloned()) } }; @@ -819,9 +819,8 @@ impl> Global { &self, device_id: DeviceId, desc: &resource::BufferDescriptor, - mapped_ptr_out: *mut *mut u8, id_in: F::Input, - ) -> BufferId { + ) -> (BufferId, *mut u8) { let hub = B::hub(self); let mut token = Token::root(); let mut desc = desc.clone(); @@ -832,17 +831,13 @@ impl> Global { let mut buffer = device.create_buffer(device_id, &desc); let ref_count = buffer.life_guard.ref_count.clone(); - match map_buffer(&device.raw, &mut buffer, 0 .. desc.size, HostMap::Write) { - Ok(ptr) => unsafe { - *mapped_ptr_out = ptr; - }, + let pointer = match map_buffer(&device.raw, &mut buffer, 0 .. desc.size, HostMap::Write) { + Ok(ptr) => ptr, Err(e) => { log::error!("failed to create buffer in a mapped state: {:?}", e); - unsafe { - *mapped_ptr_out = ptr::null_mut(); - } + ptr::null_mut() } - } + }; let id = hub.buffers.register_identity(id_in, buffer, &mut token); device.trackers @@ -854,7 +849,8 @@ impl> Global { ) .unwrap() .set((), resource::BufferUsage::MAP_WRITE); - id + + (id, pointer) } pub fn device_set_buffer_sub_data( @@ -987,7 +983,7 @@ impl> Global { (texture.device_id.value, texture.life_guard.ref_count.clone()) }; - + let (device_guard, _) = hub.devices.read(&mut token); device_guard[device_id] .pending @@ -2109,7 +2105,7 @@ impl> Global { let num_frames = swap_chain::DESIRED_NUM_FRAMES .max(*caps.image_count.start()) .min(*caps.image_count.end()); - let mut config = desc.to_hal(num_frames, &device.features); + let mut config = desc.to_hal(num_frames, device.features); if let Some(formats) = formats { assert!( formats.contains(&config.format), diff --git a/wgpu-core/src/id.rs b/wgpu-core/src/id.rs index d5d609b839..4df6fea703 100644 --- a/wgpu-core/src/id.rs +++ b/wgpu-core/src/id.rs @@ -18,7 +18,7 @@ pub struct Id(u64, PhantomData); impl Id { pub const ERROR: Self = Self(0, PhantomData); - pub fn backend(&self) -> Backend { + pub fn backend(self) -> Backend { match self.0 >> (64 - BACKEND_BITS) as u8 { 0 => Backend::Empty, 1 => Backend::Vulkan, @@ -107,13 +107,13 @@ pub type ComputePassId = Id>; pub type SwapChainId = Id>; impl SurfaceId { - pub(crate) fn to_swap_chain_id(&self, backend: Backend) -> SwapChainId { + pub(crate) fn to_swap_chain_id(self, backend: Backend) -> SwapChainId { let (index, epoch, _) = self.unzip(); Id::zip(index, epoch, backend) } } impl SwapChainId { - pub(crate) fn to_surface_id(&self) -> SurfaceId { + pub(crate) fn to_surface_id(self) -> SurfaceId { let (index, epoch, _) = self.unzip(); Id::zip(index, epoch, Backend::Empty) } diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 7700e36ba6..ba7c857c7b 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -366,8 +366,8 @@ pub enum CompareFunction { } impl CompareFunction { - pub fn is_trivial(&self) -> bool { - match *self { + pub fn is_trivial(self) -> bool { + match self { CompareFunction::Never | CompareFunction::Always => true, _ => false, } diff --git a/wgpu-core/src/swap_chain.rs b/wgpu-core/src/swap_chain.rs index 412ad99c6c..350d7447c7 100644 --- a/wgpu-core/src/swap_chain.rs +++ b/wgpu-core/src/swap_chain.rs @@ -82,12 +82,12 @@ impl SwapChainDescriptor { pub(crate) fn to_hal( &self, num_frames: u32, - features: &Features, + features: Features, ) -> hal::window::SwapchainConfig { let mut config = hal::window::SwapchainConfig::new( self.width, self.height, - conv::map_texture_format(self.format, *features), + conv::map_texture_format(self.format, features), num_frames, ); //TODO: check for supported @@ -153,7 +153,7 @@ impl> Global { } Err(e) => { log::warn!("acquire_image() failed ({:?}), reconfiguring swapchain", e); - let desc = sc.desc.to_hal(sc.num_frames, &device.features); + let desc = sc.desc.to_hal(sc.num_frames, device.features); unsafe { suf.configure_swapchain(&device.raw, desc).unwrap(); suf.acquire_image(FRAME_TIMEOUT_MS * 1_000_000).unwrap() diff --git a/wgpu-core/src/track/range.rs b/wgpu-core/src/track/range.rs index 4eb0231fc8..fd8634262f 100644 --- a/wgpu-core/src/track/range.rs +++ b/wgpu-core/src/track/range.rs @@ -65,7 +65,7 @@ impl RangedStates { Some(elem) => elem, None => return, }; - while let Some(next) = iter.next() { + for next in iter { if cur.0.end == next.0.start && cur.1 == next.1 { num_removed += 1; cur.0.end = next.0.end; diff --git a/wgpu-core/src/track/texture.rs b/wgpu-core/src/track/texture.rs index 44166d7dd5..0f9a642917 100644 --- a/wgpu-core/src/track/texture.rs +++ b/wgpu-core/src/track/texture.rs @@ -183,7 +183,7 @@ impl ResourceState for TextureState { id, selector: hal::image::SubresourceRange { aspects, - levels: level .. level + 1, + levels: level .. level+1, layers: layers.clone(), }, usage: start.last .. to_usage, diff --git a/wgpu-native/src/command.rs b/wgpu-native/src/command.rs index fb943228bc..79f4a2bd3a 100644 --- a/wgpu-native/src/command.rs +++ b/wgpu-native/src/command.rs @@ -92,7 +92,7 @@ pub extern "C" fn wgpu_render_pass_end_pass(pass_id: id::RenderPassId) { } #[no_mangle] -pub extern "C" fn wgpu_render_pass_set_bind_group( +pub unsafe extern "C" fn wgpu_render_pass_set_bind_group( pass_id: id::RenderPassId, index: u32, bind_group_id: id::BindGroupId, @@ -100,7 +100,7 @@ pub extern "C" fn wgpu_render_pass_set_bind_group( offsets_length: usize, ) { let offsets = if offsets_length != 0 { - unsafe { slice::from_raw_parts(offsets, offsets_length) } + slice::from_raw_parts(offsets, offsets_length) } else { &[] }; @@ -138,15 +138,15 @@ pub extern "C" fn wgpu_render_pass_set_index_buffer( } #[no_mangle] -pub extern "C" fn wgpu_render_pass_set_vertex_buffers( +pub unsafe extern "C" fn wgpu_render_pass_set_vertex_buffers( pass_id: id::RenderPassId, start_slot: u32, buffers: *const id::BufferId, offsets: *const core::BufferAddress, length: usize, ) { - let buffers = unsafe { slice::from_raw_parts(buffers, length) }; - let offsets = unsafe { slice::from_raw_parts(offsets, length) }; + let buffers = slice::from_raw_parts(buffers, length); + let offsets = slice::from_raw_parts(offsets, length); gfx_select!(pass_id => GLOBAL.render_pass_set_vertex_buffers(pass_id, start_slot, buffers, offsets)) } @@ -258,7 +258,7 @@ pub extern "C" fn wgpu_compute_pass_end_pass(pass_id: id::ComputePassId) { } #[no_mangle] -pub extern "C" fn wgpu_compute_pass_set_bind_group( +pub unsafe extern "C" fn wgpu_compute_pass_set_bind_group( pass_id: id::ComputePassId, index: u32, bind_group_id: id::BindGroupId, @@ -266,7 +266,7 @@ pub extern "C" fn wgpu_compute_pass_set_bind_group( offsets_length: usize, ) { let offsets = if offsets_length != 0 { - unsafe { slice::from_raw_parts(offsets, offsets_length) } + slice::from_raw_parts(offsets, offsets_length) } else { &[] }; diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 7ad656bd73..340052850c 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -138,7 +138,7 @@ pub extern "C" fn wgpu_create_surface_from_windows_hwnd( } #[no_mangle] -pub extern "C" fn wgpu_request_adapter_async( +pub unsafe extern "C" fn wgpu_request_adapter_async( desc: Option<&core::instance::RequestAdapterOptions>, mask: core::instance::BackendBit, callback: RequestAdapterCallback, @@ -148,12 +148,10 @@ pub extern "C" fn wgpu_request_adapter_async( &desc.cloned().unwrap_or_default(), core::instance::AdapterInputs::Mask(mask, || PhantomData), ); - unsafe { - callback( - id.unwrap_or(id::AdapterId::ERROR), - userdata, - ) - }; + callback( + id.unwrap_or(id::AdapterId::ERROR), + userdata, + ); } #[no_mangle] @@ -186,12 +184,14 @@ pub extern "C" fn wgpu_device_create_buffer( } #[no_mangle] -pub extern "C" fn wgpu_device_create_buffer_mapped( +pub unsafe extern "C" fn wgpu_device_create_buffer_mapped( device_id: id::DeviceId, desc: &core::resource::BufferDescriptor, mapped_ptr_out: *mut *mut u8, ) -> id::BufferId { - gfx_select!(device_id => GLOBAL.device_create_buffer_mapped(device_id, desc, mapped_ptr_out, PhantomData)) + let (id, ptr) = gfx_select!(device_id => GLOBAL.device_create_buffer_mapped(device_id, desc, PhantomData)); + *mapped_ptr_out = ptr; + id } #[no_mangle] @@ -290,13 +290,13 @@ pub extern "C" fn wgpu_device_get_queue(device_id: id::DeviceId) -> id::QueueId } #[no_mangle] -pub extern "C" fn wgpu_queue_submit( +pub unsafe extern "C" fn wgpu_queue_submit( queue_id: id::QueueId, command_buffers: *const id::CommandBufferId, command_buffers_length: usize, ) { let command_buffer_ids = - unsafe { slice::from_raw_parts(command_buffers, command_buffers_length) }; + slice::from_raw_parts(command_buffers, command_buffers_length); gfx_select!(queue_id => GLOBAL.queue_submit(queue_id, command_buffer_ids)) }