diff --git a/Cargo.toml b/Cargo.toml index 6b697ae5dc..7eabff3040 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ name = "image" path = "./src/lib.rs" [dependencies] +bytemuck = "1" byteorder = "1.3.2" num-iter = "0.1.32" num-rational = { version = "0.2.1", default-features = false } @@ -29,7 +30,6 @@ jpeg = { package = "jpeg-decoder", version = "0.1", default-features = false, op png = { version = "0.15.2", optional = true } scoped_threadpool = { version = "0.1", optional = true } tiff = { version = "0.4.0", optional = true } -zerocopy = "0.2.8" [dev-dependencies] crc32fast = "1.2.0" diff --git a/src/dynimage.rs b/src/dynimage.rs index 24f45e4d02..60badd5922 100644 --- a/src/dynimage.rs +++ b/src/dynimage.rs @@ -851,7 +851,7 @@ fn decoder_to_image<'a, I: ImageDecoder<'a>>(decoder: I) -> ImageResult Vec { - use zerocopy::AsBytes; + use traits::EncodableLayout; match *image { // TODO: consider transmuting diff --git a/src/image.rs b/src/image.rs index a1eedf6468..994efa62a4 100644 --- a/src/image.rs +++ b/src/image.rs @@ -10,7 +10,6 @@ use std::ops::{Deref, DerefMut}; use buffer::{ImageBuffer, Pixel}; use color::{ColorType, ExtendedColorType}; -use zerocopy::{AsBytes, FromBytes}; use animation::Frames; @@ -373,10 +372,10 @@ pub(crate) fn load_rect<'a, D, F, F1, F2, E>(x: u32, y: u32, width: u32, height: /// Panics if there isn't enough memory to decode the image. pub(crate) fn decoder_to_vec<'a, T>(decoder: impl ImageDecoder<'a>) -> ImageResult> where - T: ::traits::Primitive + AsBytes + FromBytes, + T: ::traits::Primitive + bytemuck::Pod, { let mut buf = vec![num_traits::Zero::zero(); usize::try_from(decoder.total_bytes()).unwrap() / std::mem::size_of::()]; - decoder.read_image(buf.as_bytes_mut())?; + decoder.read_image(bytemuck::cast_slice_mut(buf.as_mut_slice()))?; Ok(buf) } diff --git a/src/lib.rs b/src/lib.rs index 75da06ee71..d518a572f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ // it's a bit of a pain otherwise #![allow(clippy::many_single_char_names)] +extern crate bytemuck; extern crate byteorder; extern crate num_iter; extern crate num_rational; @@ -19,7 +20,6 @@ extern crate num_traits; extern crate scoped_threadpool; #[cfg(all(test, feature = "benchmarks"))] extern crate test; -extern crate zerocopy; #[cfg(test)] #[macro_use] diff --git a/src/tiff.rs b/src/tiff.rs index 2a9c895b0f..30be7e2341 100644 --- a/src/tiff.rs +++ b/src/tiff.rs @@ -130,10 +130,10 @@ pub struct TiffEncoder { } // Utility to simplify and deduplicate error handling during 16-bit encoding. -fn u8_slice_as_u16(buf: &[u8]) -> ImageResult> { - zerocopy::LayoutVerified::new_slice(buf) +fn u8_slice_as_u16(buf: &[u8]) -> ImageResult<&[u16]> { + bytemuck::try_cast_slice(buf) // If the buffer is not aligned or the correct length for a u16 slice, err. - .ok_or_else(|| ImageError::IoError(std::io::ErrorKind::InvalidData.into())) + .map_err(|_| ImageError::IoError(std::io::ErrorKind::InvalidData.into())) } impl TiffEncoder { diff --git a/src/traits.rs b/src/traits.rs index 117e6c8d42..6e558d4a13 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -14,13 +14,13 @@ pub trait EncodableLayout: seals::EncodableLayout { impl EncodableLayout for [u8] { fn as_bytes(&self) -> &[u8] { - zerocopy::AsBytes::as_bytes(self) + bytemuck::cast_slice(self) } } impl EncodableLayout for [u16] { fn as_bytes(&self) -> &[u8] { - zerocopy::AsBytes::as_bytes(self) + bytemuck::cast_slice(self) } }