Skip to content

Commit

Permalink
Switch from zerocopy to bytemuck
Browse files Browse the repository at this point in the history
Bytemuck is more feature constrained and builds on Rust 1.34.2.
  • Loading branch information
aschampion committed Dec 12, 2019
1 parent 33cebef commit 5dae1fd
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -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 }
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/dynimage.rs
Expand Up @@ -851,7 +851,7 @@ fn decoder_to_image<'a, I: ImageDecoder<'a>>(decoder: I) -> ImageResult<DynamicI

#[allow(deprecated)]
fn image_to_bytes(image: &DynamicImage) -> Vec<u8> {
use zerocopy::AsBytes;
use traits::EncodableLayout;

match *image {
// TODO: consider transmuting
Expand Down
5 changes: 2 additions & 3 deletions src/image.rs
Expand Up @@ -10,7 +10,6 @@ use std::ops::{Deref, DerefMut};

use buffer::{ImageBuffer, Pixel};
use color::{ColorType, ExtendedColorType};
use zerocopy::{AsBytes, FromBytes};

use animation::Frames;

Expand Down Expand Up @@ -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<Vec<T>>
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::<T>()];
decoder.read_image(buf.as_bytes_mut())?;
decoder.read_image(bytemuck::cast_slice_mut(buf.as_mut_slice()))?;
Ok(buf)
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -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;
Expand All @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions src/tiff.rs
Expand Up @@ -130,10 +130,10 @@ pub struct TiffEncoder<W> {
}

// Utility to simplify and deduplicate error handling during 16-bit encoding.
fn u8_slice_as_u16(buf: &[u8]) -> ImageResult<zerocopy::LayoutVerified<&[u8], [u16]>> {
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<W: Write + Seek> TiffEncoder<W> {
Expand Down
4 changes: 2 additions & 2 deletions src/traits.rs
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 5dae1fd

Please sign in to comment.