From 244c64cd311299e82ce4f2b77fc9cbbf4901fd80 Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Sat, 2 Sep 2023 14:07:10 -0700 Subject: [PATCH 1/3] Remove remaining unsafe --- Cargo.toml | 2 +- src/buffer.rs | 15 --------------- src/color.rs | 11 +++++------ src/image.rs | 24 ------------------------ src/lib.rs | 1 + 5 files changed, 7 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 90be377f8f..500034eaaa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ include = [ ] [dependencies] -bytemuck = { version = "1.7.0", features = ["extern_crate_alloc"] } # includes cast_vec +bytemuck = { version = "1.7.0", features = ["extern_crate_alloc", "derive"] } # includes cast_vec byteorder = "1.3.2" num-traits = "0.2.0" gif = { version = "0.12", optional = true } diff --git a/src/buffer.rs b/src/buffer.rs index 4ed1111830..b7a70ea494 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -1180,13 +1180,6 @@ where fn get_pixel(&self, x: u32, y: u32) -> P { *self.get_pixel(x, y) } - - /// Returns the pixel located at (x, y), ignoring bounds checking. - #[inline(always)] - unsafe fn unsafe_get_pixel(&self, x: u32, y: u32) -> P { - let indices = self.pixel_indices_unchecked(x, y); - *

::from_slice(self.data.get_unchecked(indices)) - } } impl GenericImage for ImageBuffer @@ -1202,14 +1195,6 @@ where *self.get_pixel_mut(x, y) = pixel } - /// Puts a pixel at location (x, y), ignoring bounds checking. - #[inline(always)] - unsafe fn unsafe_put_pixel(&mut self, x: u32, y: u32, pixel: P) { - let indices = self.pixel_indices_unchecked(x, y); - let p =

::from_slice_mut(self.data.get_unchecked_mut(indices)); - *p = pixel - } - /// Put a pixel at location (x, y), taking into account alpha channels /// /// DEPRECATED: This method will be removed. Blend the pixel directly instead. diff --git a/src/color.rs b/src/color.rs index 5ec6a4677b..babf3facad 100644 --- a/src/color.rs +++ b/src/color.rs @@ -1,6 +1,7 @@ use std::ops::{Index, IndexMut}; use num_traits::{NumCast, ToPrimitive, Zero}; +use bytemuck::TransparentWrapper; use crate::traits::{Enlargeable, Pixel, Primitive}; @@ -211,8 +212,8 @@ macro_rules! define_colors { $( // START Structure definitions $(#[$doc])* -#[derive(PartialEq, Eq, Clone, Debug, Copy, Hash)] -#[repr(C)] +#[derive(PartialEq, Eq, Clone, Debug, Copy, Hash, TransparentWrapper)] +#[repr(transparent)] #[allow(missing_docs)] pub struct $ident (pub [T; $channels]); @@ -246,12 +247,10 @@ impl Pixel for $ident { } fn from_slice(slice: &[T]) -> &$ident { - assert_eq!(slice.len(), $channels); - unsafe { &*(slice.as_ptr() as *const $ident) } + Self::wrap_ref(slice.try_into().unwrap()) } fn from_slice_mut(slice: &mut [T]) -> &mut $ident { - assert_eq!(slice.len(), $channels); - unsafe { &mut *(slice.as_mut_ptr() as *mut $ident) } + Self::wrap_mut(slice.try_into().unwrap()) } fn to_rgb(&self) -> Rgb { diff --git a/src/image.rs b/src/image.rs index 5fc4e87d19..348c1141df 100644 --- a/src/image.rs +++ b/src/image.rs @@ -962,18 +962,6 @@ pub trait GenericImageView { /// Panics if `(x, y)` is out of bounds. fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel; - /// Returns the pixel located at (x, y). Indexed from top left. - /// - /// This function can be implemented in a way that ignores bounds checking. - /// # Safety - /// - /// The coordinates must be [`in_bounds`] of the image. - /// - /// [`in_bounds`]: #method.in_bounds - unsafe fn unsafe_get_pixel(&self, x: u32, y: u32) -> Self::Pixel { - self.get_pixel(x, y) - } - /// Returns an Iterator over the pixels of this image. /// The iterator yields the coordinates of each pixel /// along with their value @@ -1037,18 +1025,6 @@ pub trait GenericImage: GenericImageView { /// Panics if `(x, y)` is out of bounds. fn put_pixel(&mut self, x: u32, y: u32, pixel: Self::Pixel); - /// Puts a pixel at location (x, y). Indexed from top left. - /// - /// This function can be implemented in a way that ignores bounds checking. - /// # Safety - /// - /// The coordinates must be [`in_bounds`] of the image. - /// - /// [`in_bounds`]: traits.GenericImageView.html#method.in_bounds - unsafe fn unsafe_put_pixel(&mut self, x: u32, y: u32, pixel: Self::Pixel) { - self.put_pixel(x, y, pixel); - } - /// Put a pixel at location (x, y), taking into account alpha channels #[deprecated( since = "0.24.0", diff --git a/src/lib.rs b/src/lib.rs index bd625fbf7d..5b2a89100f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -121,6 +121,7 @@ #![allow(clippy::many_single_char_names)] // it's a backwards compatibility break #![allow(clippy::wrong_self_convention, clippy::enum_variant_names)] +#![forbid(unsafe_code)] #[cfg(all(test, feature = "benchmarks"))] extern crate test; From 4333bbadcf646a4d10e7fef42bb2f28b68d6816f Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Sat, 2 Sep 2023 15:37:19 -0700 Subject: [PATCH 2/3] More fixes --- Cargo.toml.public-private-dependencies | 2 +- src/color.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml.public-private-dependencies b/Cargo.toml.public-private-dependencies index 671dea984a..87e69e1fe5 100644 --- a/Cargo.toml.public-private-dependencies +++ b/Cargo.toml.public-private-dependencies @@ -29,7 +29,7 @@ name = "image" path = "./src/lib.rs" [dependencies] -bytemuck = { version = "1.7.0", features = ["extern_crate_alloc"] } # includes cast_vec +bytemuck = { version = "1.7.0", features = ["extern_crate_alloc", "derive"] } # includes cast_vec byteorder = "1.3.2" num-iter = "0.1.32" num-rational = { version = "0.4", default-features = false } diff --git a/src/color.rs b/src/color.rs index babf3facad..9e4b796c2c 100644 --- a/src/color.rs +++ b/src/color.rs @@ -1,7 +1,7 @@ use std::ops::{Index, IndexMut}; -use num_traits::{NumCast, ToPrimitive, Zero}; use bytemuck::TransparentWrapper; +use num_traits::{NumCast, ToPrimitive, Zero}; use crate::traits::{Enlargeable, Pixel, Primitive}; From 4b8f2db4d5b25e9ad5eae7856a825e9a7bf86b7d Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Sat, 2 Sep 2023 15:59:23 -0700 Subject: [PATCH 3/3] Use 2021 edition for public-private-dependencies --- Cargo.toml.public-private-dependencies | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml.public-private-dependencies b/Cargo.toml.public-private-dependencies index 87e69e1fe5..fbb5702738 100644 --- a/Cargo.toml.public-private-dependencies +++ b/Cargo.toml.public-private-dependencies @@ -3,7 +3,7 @@ cargo-features = ["public-dependency"] [package] name = "image" version = "0.24.0-alpha" -edition = "2018" +edition = "2021" rust-version = "1.56" license = "MIT"