Skip to content

Commit

Permalink
To squash to 6bc4ca4: sealed encodable layout trait
Browse files Browse the repository at this point in the history
  • Loading branch information
aschampion committed Dec 4, 2019
1 parent b6369c4 commit a688199
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/buffer.rs
Expand Up @@ -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;


Expand Down Expand Up @@ -744,7 +744,7 @@ where
impl<P, Container> ImageBuffer<P, Container>
where
P: Pixel + 'static,
[P::Subpixel]: AsBytes,
[P::Subpixel]: EncodableLayout,
Container: Deref<Target = [P::Subpixel]>,
{
/// Saves the buffer to a file at the path specified.
Expand All @@ -769,7 +769,7 @@ where
impl<P, Container> ImageBuffer<P, Container>
where
P: Pixel + 'static,
[P::Subpixel]: AsBytes,
[P::Subpixel]: EncodableLayout,
Container: Deref<Target = [P::Subpixel]>,
{
/// Saves the buffer to a file at the specified path in
Expand Down
19 changes: 14 additions & 5 deletions src/traits.rs
Expand Up @@ -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<T> AsBytes for T where T: zerocopy::AsBytes {
impl EncodableLayout for [u8] {
fn as_bytes(&self) -> &[u8] {
zerocopy::AsBytes::as_bytes(self)
}
}

impl<T> AsBytes for [T] where [T]: zerocopy::AsBytes {
impl EncodableLayout for [u16] {
fn as_bytes(&self) -> &[u8] {
zerocopy::AsBytes::as_bytes(self)
}
Expand Down Expand Up @@ -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] {}
}

0 comments on commit a688199

Please sign in to comment.