Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove remaining unsafe #1997

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml.public-private-dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 }
Expand Down
15 changes: 0 additions & 15 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
*<P as Pixel>::from_slice(self.data.get_unchecked(indices))
}
}

impl<P, Container> GenericImage for ImageBuffer<P, Container>
Expand All @@ -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 = <P as Pixel>::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.
Expand Down
11 changes: 5 additions & 6 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::ops::{Index, IndexMut};

use bytemuck::TransparentWrapper;
use num_traits::{NumCast, ToPrimitive, Zero};

use crate::traits::{Enlargeable, Pixel, Primitive};
Expand Down Expand Up @@ -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<T> (pub [T; $channels]);

Expand Down Expand Up @@ -246,12 +247,10 @@ impl<T: $($bound+)*> Pixel for $ident<T> {
}

fn from_slice(slice: &[T]) -> &$ident<T> {
assert_eq!(slice.len(), $channels);
unsafe { &*(slice.as_ptr() as *const $ident<T>) }
Self::wrap_ref(slice.try_into().unwrap())
}
fn from_slice_mut(slice: &mut [T]) -> &mut $ident<T> {
assert_eq!(slice.len(), $channels);
unsafe { &mut *(slice.as_mut_ptr() as *mut $ident<T>) }
Self::wrap_mut(slice.try_into().unwrap())
}

fn to_rgb(&self) -> Rgb<T> {
Expand Down
24 changes: 0 additions & 24 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading