From 2687d5d82d6d6ca5afdf33e1c56d5f7dcab59a7e Mon Sep 17 00:00:00 2001 From: Andrew Champion Date: Wed, 4 Dec 2019 18:09:33 +0000 Subject: [PATCH] Remove zerocopy from public interface Instead create a crate-local AsBytes wrapper trait. This trait is not sealed as Primitive was not sealed, so that users can delegate it for newtypes if needed. --- src/buffer.rs | 7 +++---- src/traits.rs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index a288000fd8..bfe7c850ac 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -8,9 +8,8 @@ use color::{ChannelsType, ColorType, FromColor, Luma, LumaA, Rgb, Rgba, Bgr, Bgr use flat::{FlatSamples, SampleLayout}; use dynimage::{save_buffer, save_buffer_with_format}; use image::{GenericImage, GenericImageView, ImageFormat, ImageResult}; -use traits::Primitive; +use traits::{AsBytes, Primitive}; use utils::expand_packed; -use zerocopy::AsBytes; /// A generalized pixel. @@ -745,7 +744,7 @@ where impl ImageBuffer where P: Pixel + 'static, - P::Subpixel: AsBytes, + [P::Subpixel]: AsBytes, Container: Deref, { /// Saves the buffer to a file at the path specified. @@ -770,7 +769,7 @@ where impl ImageBuffer where P: Pixel + 'static, - P::Subpixel: AsBytes, + [P::Subpixel]: AsBytes, Container: Deref, { /// Saves the buffer to a file at the specified path in diff --git a/src/traits.rs b/src/traits.rs index 11d25ec46b..b6435eb330 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -5,6 +5,25 @@ use num_traits::{Bounded, Num, NumCast}; use std::ops::AddAssign; +/// Types which are safe to treat as an immutable byte slice. This is a wrapper +/// around the `zerocopy::AsBytes` trait. +pub trait AsBytes { + /// Get the bytes of this value. + fn as_bytes(&self) -> &[u8]; +} + +impl AsBytes for T where T: zerocopy::AsBytes { + fn as_bytes(&self) -> &[u8] { + zerocopy::AsBytes::as_bytes(self) + } +} + +impl AsBytes for [T] where [T]: zerocopy::AsBytes { + fn as_bytes(&self) -> &[u8] { + zerocopy::AsBytes::as_bytes(self) + } +} + /// Primitive trait from old stdlib pub trait Primitive: Copy + NumCast + Num + PartialOrd + Clone + Bounded {}