diff --git a/src/buffer.rs b/src/buffer.rs index 2b7b81dd63..15860ec628 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -8,7 +8,7 @@ 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::{AsBytes, Primitive}; +use traits::{EncodableLayout, Primitive}; use utils::expand_packed; @@ -744,7 +744,7 @@ where impl ImageBuffer where P: Pixel + 'static, - [P::Subpixel]: AsBytes, + [P::Subpixel]: EncodableLayout, Container: Deref, { /// Saves the buffer to a file at the path specified. @@ -769,7 +769,7 @@ where impl ImageBuffer where P: Pixel + 'static, - [P::Subpixel]: AsBytes, + [P::Subpixel]: EncodableLayout, Container: Deref, { /// Saves the buffer to a file at the specified path in diff --git a/src/traits.rs b/src/traits.rs index b6435eb330..117e6c8d42 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -5,20 +5,20 @@ 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 { +/// Types which are safe to treat as an immutable byte slice in a pixel layout +/// for image encoding. +pub trait EncodableLayout: seals::EncodableLayout { /// Get the bytes of this value. fn as_bytes(&self) -> &[u8]; } -impl AsBytes for T where T: zerocopy::AsBytes { +impl EncodableLayout for [u8] { fn as_bytes(&self) -> &[u8] { zerocopy::AsBytes::as_bytes(self) } } -impl AsBytes for [T] where [T]: zerocopy::AsBytes { +impl EncodableLayout for [u16] { fn as_bytes(&self) -> &[u8] { zerocopy::AsBytes::as_bytes(self) } @@ -64,3 +64,12 @@ impl Enlargeable for u16 { impl Enlargeable for u32 { type Larger = u64; } + + +/// Private module for supertraits of sealed traits. +mod seals { + pub trait EncodableLayout {} + + impl EncodableLayout for [u8] {} + impl EncodableLayout for [u16] {} +}