From 085e50c798ecd3f782ce89e3991af2c647c4e51e Mon Sep 17 00:00:00 2001 From: Rukai Date: Mon, 7 Sep 2020 20:28:07 +1000 Subject: [PATCH] Replace unsafe impl Pod with safe derive --- Cargo.toml | 2 +- examples/cube/main.rs | 5 +---- examples/mipmap/main.rs | 5 +---- examples/msaa-line/main.rs | 5 +---- examples/shadow/main.rs | 21 ++++----------------- examples/texture-arrays/main.rs | 10 ++-------- examples/water/main.rs | 11 +++-------- examples/water/point_gen.rs | 13 +++---------- 8 files changed, 16 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0f7e5551b..469e28ec3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,7 @@ log = "0.4" png = "0.16" winit = { version = "0.22.1", features = ["web-sys"] } rand = { version = "0.7.2", features = ["wasm-bindgen"] } -bytemuck = "1" +bytemuck = { version = "1.4", features = ["derive"] } noise = "0.6" ddsfile = "0.4" futures = { version = "0.3", default-features = false, features = ["std", "executor"] } diff --git a/examples/cube/main.rs b/examples/cube/main.rs index 0c603c78f..ffcb19caf 100644 --- a/examples/cube/main.rs +++ b/examples/cube/main.rs @@ -5,15 +5,12 @@ use bytemuck::{Pod, Zeroable}; use wgpu::util::DeviceExt; #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Pod, Zeroable)] struct Vertex { _pos: [f32; 4], _tex_coord: [f32; 2], } -unsafe impl Pod for Vertex {} -unsafe impl Zeroable for Vertex {} - fn vertex(pos: [i8; 3], tc: [i8; 2]) -> Vertex { Vertex { _pos: [pos[0] as f32, pos[1] as f32, pos[2] as f32, 1.0], diff --git a/examples/mipmap/main.rs b/examples/mipmap/main.rs index 3fe0ea9bc..39fd568b0 100644 --- a/examples/mipmap/main.rs +++ b/examples/mipmap/main.rs @@ -8,15 +8,12 @@ use wgpu::util::DeviceExt; const TEXTURE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8UnormSrgb; #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Pod, Zeroable)] struct Vertex { #[allow(dead_code)] pos: [f32; 4], } -unsafe impl Pod for Vertex {} -unsafe impl Zeroable for Vertex {} - fn create_vertices() -> Vec { vec![ Vertex { diff --git a/examples/msaa-line/main.rs b/examples/msaa-line/main.rs index 331b2ee22..6b0bfde5c 100644 --- a/examples/msaa-line/main.rs +++ b/examples/msaa-line/main.rs @@ -16,15 +16,12 @@ use bytemuck::{Pod, Zeroable}; use wgpu::util::DeviceExt; #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Pod, Zeroable)] struct Vertex { _pos: [f32; 2], _color: [f32; 4], } -unsafe impl Pod for Vertex {} -unsafe impl Zeroable for Vertex {} - struct Example { bundle: wgpu::RenderBundle, vs_module: wgpu::ShaderModule, diff --git a/examples/shadow/main.rs b/examples/shadow/main.rs index c0e8dabb6..11fe586bf 100644 --- a/examples/shadow/main.rs +++ b/examples/shadow/main.rs @@ -7,16 +7,12 @@ use bytemuck::{Pod, Zeroable}; use wgpu::util::DeviceExt; #[repr(C)] -#[derive(Clone, Copy)] - +#[derive(Clone, Copy, Pod, Zeroable)] struct Vertex { _pos: [i8; 4], _normal: [i8; 4], } -unsafe impl Pod for Vertex {} -unsafe impl Zeroable for Vertex {} - fn vertex(pos: [i8; 3], nor: [i8; 3]) -> Vertex { Vertex { _pos: [pos[0], pos[1], pos[2], 1], @@ -102,16 +98,13 @@ struct Light { } #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Pod, Zeroable)] struct LightRaw { proj: [[f32; 4]; 4], pos: [f32; 4], color: [f32; 4], } -unsafe impl Pod for LightRaw {} -unsafe impl Zeroable for LightRaw {} - impl Light { fn to_raw(&self) -> LightRaw { use cgmath::{Deg, EuclideanSpace, Matrix4, PerspectiveFov, Point3, Vector3}; @@ -140,25 +133,19 @@ impl Light { } #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Pod, Zeroable)] struct ForwardUniforms { proj: [[f32; 4]; 4], num_lights: [u32; 4], } -unsafe impl Pod for ForwardUniforms {} -unsafe impl Zeroable for ForwardUniforms {} - #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Pod, Zeroable)] struct EntityUniforms { model: [[f32; 4]; 4], color: [f32; 4], } -unsafe impl Pod for EntityUniforms {} -unsafe impl Zeroable for EntityUniforms {} - #[repr(C)] struct ShadowUniforms { proj: [[f32; 4]; 4], diff --git a/examples/texture-arrays/main.rs b/examples/texture-arrays/main.rs index b0178dc4b..788ea30d5 100644 --- a/examples/texture-arrays/main.rs +++ b/examples/texture-arrays/main.rs @@ -6,25 +6,19 @@ use bytemuck::{Pod, Zeroable}; use wgpu::util::DeviceExt; #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Pod, Zeroable)] struct Vertex { _pos: [f32; 2], _tex_coord: [f32; 2], _index: u32, } -unsafe impl Pod for Vertex {} -unsafe impl Zeroable for Vertex {} - #[repr(C)] -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Pod, Zeroable)] struct Uniform { index: u32, } -unsafe impl Pod for Uniform {} -unsafe impl Zeroable for Uniform {} - fn vertex(pos: [i8; 2], tc: [i8; 2], index: i8) -> Vertex { Vertex { _pos: [pos[0] as f32, pos[1] as f32], diff --git a/examples/water/main.rs b/examples/water/main.rs index 8b57f050a..0b82e11b2 100644 --- a/examples/water/main.rs +++ b/examples/water/main.rs @@ -6,6 +6,7 @@ mod point_gen; use cgmath::Point3; use std::{iter, mem}; use wgpu::util::DeviceExt; +use bytemuck::{Pod, Zeroable}; /// /// Radius of the terrain. @@ -34,17 +35,14 @@ struct Matrices { } #[repr(C)] -#[derive(Copy, Clone, Debug, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq, Pod, Zeroable)] struct TerrainUniforms { view_projection: [f32; 16], clipping_plane: [f32; 4], } -unsafe impl bytemuck::Zeroable for TerrainUniforms {} -unsafe impl bytemuck::Pod for TerrainUniforms {} - #[repr(C)] -#[derive(Copy, Clone, Debug, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq, Pod, Zeroable)] struct WaterUniforms { view: [f32; 16], projection: [f32; 16], @@ -58,9 +56,6 @@ struct Uniforms { water: WaterUniforms, } -unsafe impl bytemuck::Zeroable for WaterUniforms {} -unsafe impl bytemuck::Pod for WaterUniforms {} - struct Example { water_vertex_buf: wgpu::Buffer, water_vertex_count: usize, diff --git a/examples/water/point_gen.rs b/examples/water/point_gen.rs index 7b380175e..54d74d245 100644 --- a/examples/water/point_gen.rs +++ b/examples/water/point_gen.rs @@ -3,6 +3,7 @@ //! use cgmath::{InnerSpace, Point3, Vector3}; +use bytemuck::{Pod, Zeroable}; use std::collections::HashMap; // The following constants are used in calculations. @@ -32,28 +33,20 @@ const C45: f32 = S45; const SQRT_3: f32 = 1.73205080757; #[repr(C)] -#[derive(Copy, Clone, Debug, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq, Pod, Zeroable)] pub struct TerrainVertexAttributes { position: [f32; 3], normal: [f32; 3], colour: [u8; 4], } -unsafe impl bytemuck::Pod for TerrainVertexAttributes {} - -unsafe impl bytemuck::Zeroable for TerrainVertexAttributes {} - #[repr(C)] -#[derive(Copy, Clone, Debug, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq, Pod, Zeroable)] pub struct WaterVertexAttributes { position: [i16; 2], offsets: [i8; 4], } -unsafe impl bytemuck::Pod for WaterVertexAttributes {} - -unsafe impl bytemuck::Zeroable for WaterVertexAttributes {} - /// /// Represents the center of a single hexagon. ///