Skip to content

Commit

Permalink
Use simpler bytemuck impl if as-bytes is off
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jul 8, 2024
1 parent 559fd63 commit cc35614
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/as_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ unsafe impl<T> crate::Zeroable for RGB<T> where T: crate::Zeroable {}
unsafe impl<T> crate::Zeroable for BGR<T> where T: crate::Zeroable {}

#[cfg(feature = "as-bytes")]
/// This is unsound. You can disable `as-bytes` feature, enable `bytemuck`, and use `bytemuck::cast_slice()` instead.
unsafe impl<T, A> crate::Zeroable for ABGR<T, A> where T: crate::Zeroable, A: crate::Zeroable {
#[track_caller]
#[inline(always)]
Expand All @@ -32,12 +33,15 @@ const fn assert_no_padding<T, A, S>() {
}

#[cfg(feature = "as-bytes")]
/// This is unsound. You can disable `as-bytes` feature, enable `bytemuck`, and use `bytemuck::cast_slice()` instead.
unsafe impl<T, A> crate::Pod for RGBA<T, A> where T: crate::Pod, A: crate::Pod {}

#[cfg(feature = "as-bytes")]
/// This is unsound. You can disable `as-bytes` feature, enable `bytemuck`, and use `bytemuck::cast_slice()` instead.
unsafe impl<T, A> crate::Pod for BGRA<T, A> where T: crate::Pod, A: crate::Pod {}

#[cfg(feature = "as-bytes")]
/// This is unsound. You can disable `as-bytes` feature, enable `bytemuck`, and use `bytemuck::cast_slice()` instead.
unsafe impl<T, A> crate::Zeroable for RGBA<T, A> where T: crate::Zeroable, A: crate::Zeroable {
#[track_caller]
#[inline(always)]
Expand All @@ -50,12 +54,15 @@ unsafe impl<T, A> crate::Zeroable for RGBA<T, A> where T: crate::Zeroable, A: cr
}

#[cfg(feature = "as-bytes")]
/// This is unsound. You can disable `as-bytes` feature, enable `bytemuck`, and use `bytemuck::cast_slice()` instead.
unsafe impl<T, A> crate::Pod for ARGB<T, A> where T: crate::Pod, A: crate::Pod {}

#[cfg(feature = "as-bytes")]
/// This is unsound. You can disable `as-bytes` feature, enable `bytemuck`, and use `bytemuck::cast_slice()` instead.
unsafe impl<T, A> crate::Pod for ABGR<T, A> where T: crate::Pod, A: crate::Pod {}

#[cfg(feature = "as-bytes")]
/// This is unsound. You can disable `as-bytes` feature, enable `bytemuck`, and use `bytemuck::cast_slice()` instead.
unsafe impl<T, A> crate::Zeroable for ARGB<T, A> where T: crate::Zeroable, A: crate::Zeroable {
#[track_caller]
#[inline(always)]
Expand All @@ -68,6 +75,7 @@ unsafe impl<T, A> crate::Zeroable for ARGB<T, A> where T: crate::Zeroable, A: cr
}

#[cfg(feature = "as-bytes")]
/// This is unsound. You can disable `as-bytes` feature, enable `bytemuck`, and use `bytemuck::cast_slice()` instead.
unsafe impl<T, A> crate::Zeroable for BGRA<T, A> where T: crate::Zeroable, A: crate::Zeroable {
#[track_caller]
#[inline(always)]
Expand All @@ -82,10 +90,12 @@ unsafe impl<T, A> crate::Zeroable for BGRA<T, A> where T: crate::Zeroable, A: cr
#[cfg(feature = "as-bytes")]
unsafe impl<T> crate::Pod for Gray<T> where T: crate::Pod {}
#[cfg(feature = "as-bytes")]
/// This is unsound. You can disable `as-bytes` feature, enable `bytemuck`, and use `bytemuck::cast_slice()` instead.
unsafe impl<T, A> crate::Pod for GrayAlpha<T, A> where T: crate::Pod, A: crate::Pod {}
#[cfg(feature = "as-bytes")]
unsafe impl<T> crate::Zeroable for Gray<T> where T: crate::Zeroable {}
#[cfg(feature = "as-bytes")]
/// This is unsound. You can disable `as-bytes` feature, enable `bytemuck`, and use `bytemuck::cast_slice()` instead.
unsafe impl<T, A> crate::Zeroable for GrayAlpha<T, A> where T: crate::Zeroable, A: crate::Zeroable {
#[track_caller]
#[inline(always)]
Expand Down
23 changes: 23 additions & 0 deletions src/bytemuck_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::{Abgr, Argb, Bgr, Bgra, Grb, Rgb, Rgba};
use crate::formats::gray_a::GrayA;
use crate::formats::gray_alpha::GrayAlpha_v08;
use crate::formats::gray::Gray_v08;

macro_rules! bytemuck {
($name:ident) => {
unsafe impl<T: ::bytemuck::Zeroable> ::bytemuck::Zeroable for $name<T> {}
unsafe impl<T: ::bytemuck::Pod> ::bytemuck::Pod for $name<T> {}
};
}

bytemuck!(Rgb);
bytemuck!(Bgr);
bytemuck!(Grb);
bytemuck!(Rgba);
bytemuck!(Argb);
bytemuck!(Bgra);
bytemuck!(Abgr);
bytemuck!(GrayA);

bytemuck!(GrayAlpha_v08);
bytemuck!(Gray_v08);
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ pub(crate) mod legacy {

pub use legacy::alt;

#[cfg(all(feature = "bytemuck", not(feature = "as-bytes")))]
mod bytemuck_impl;
#[cfg(feature = "as-bytes")]
mod as_bytes;

Expand Down

0 comments on commit cc35614

Please sign in to comment.