Skip to content

Commit

Permalink
Remove zerocopy from public interface
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aschampion committed Dec 4, 2019
1 parent 8433f75 commit 2687d5d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/buffer.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -745,7 +744,7 @@ where
impl<P, Container> ImageBuffer<P, Container>
where
P: Pixel + 'static,
P::Subpixel: AsBytes,
[P::Subpixel]: AsBytes,
Container: Deref<Target = [P::Subpixel]>,
{
/// Saves the buffer to a file at the path specified.
Expand All @@ -770,7 +769,7 @@ where
impl<P, Container> ImageBuffer<P, Container>
where
P: Pixel + 'static,
P::Subpixel: AsBytes,
[P::Subpixel]: AsBytes,
Container: Deref<Target = [P::Subpixel]>,
{
/// Saves the buffer to a file at the specified path in
Expand Down
19 changes: 19 additions & 0 deletions src/traits.rs
Expand Up @@ -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<T> AsBytes for T where T: zerocopy::AsBytes {
fn as_bytes(&self) -> &[u8] {
zerocopy::AsBytes::as_bytes(self)
}
}

impl<T> 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<Self> + Clone + Bounded {}

Expand Down

0 comments on commit 2687d5d

Please sign in to comment.