use crate::{RGB, RGBA}; use crate::alt::{Gray, GrayAlpha, BGR, BGRA}; use crate::alt::{ARGB, ABGR}; use crate::ComponentBytes; #[cfg(feature = "as-bytes")] unsafe impl crate::Pod for RGB where T: crate::Pod {} #[cfg(feature = "as-bytes")] unsafe impl crate::Pod for BGR where T: crate::Pod {} #[cfg(feature = "as-bytes")] unsafe impl crate::Zeroable for RGB where T: crate::Zeroable {} #[cfg(feature = "as-bytes")] unsafe impl crate::Zeroable for BGR 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 crate::Zeroable for ABGR where T: crate::Zeroable, A: crate::Zeroable { #[track_caller] #[inline(always)] fn zeroed() -> Self { unsafe { assert_no_padding::(); core::mem::zeroed() } } } #[track_caller] const fn assert_no_padding() { if core::mem::size_of::() + 3 * core::mem::size_of::() != core::mem::size_of::() { panic!("type has padding"); } } #[cfg(feature = "as-bytes")] /// This is unsound. You can disable `as-bytes` feature, enable `bytemuck`, and use `bytemuck::cast_slice()` instead. unsafe impl crate::Pod for RGBA 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 crate::Pod for BGRA 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 crate::Zeroable for RGBA where T: crate::Zeroable, A: crate::Zeroable { #[track_caller] #[inline(always)] fn zeroed() -> Self { unsafe { assert_no_padding::(); core::mem::zeroed() } } } #[cfg(feature = "as-bytes")] /// This is unsound. You can disable `as-bytes` feature, enable `bytemuck`, and use `bytemuck::cast_slice()` instead. unsafe impl crate::Pod for ARGB 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 crate::Pod for ABGR 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 crate::Zeroable for ARGB where T: crate::Zeroable, A: crate::Zeroable { #[track_caller] #[inline(always)] fn zeroed() -> Self { unsafe { assert_no_padding::(); core::mem::zeroed() } } } #[cfg(feature = "as-bytes")] /// This is unsound. You can disable `as-bytes` feature, enable `bytemuck`, and use `bytemuck::cast_slice()` instead. unsafe impl crate::Zeroable for BGRA where T: crate::Zeroable, A: crate::Zeroable { #[track_caller] #[inline(always)] fn zeroed() -> Self { unsafe { assert_no_padding::(); core::mem::zeroed() } } } #[cfg(feature = "as-bytes")] unsafe impl crate::Pod for Gray 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 crate::Pod for GrayAlpha where T: crate::Pod, A: crate::Pod {} #[cfg(feature = "as-bytes")] unsafe impl crate::Zeroable for Gray 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 crate::Zeroable for GrayAlpha where T: crate::Zeroable, A: crate::Zeroable { #[track_caller] #[inline(always)] fn zeroed() -> Self { unsafe { if core::mem::size_of::() + core::mem::size_of::() != core::mem::size_of::() { panic!("type has padding"); } core::mem::zeroed() } } } #[cfg(feature = "as-bytes")] impl ComponentBytes for [Gray] {} #[cfg(feature = "as-bytes")] impl ComponentBytes for [GrayAlpha] {} #[test] #[allow(dead_code)] fn shared_impl() { struct SharedPixelBuffer { data: [Pixel; 1], } impl SharedPixelBuffer where [Pixel]: crate::ComponentBytes, { pub fn as_bytes(&self) -> &[u8] { self.data.as_slice().as_bytes() } } let b = SharedPixelBuffer { data: [crate::RGB8::new(0,0,0)], }; let _ = b.as_bytes(); }