From 082249865489541905a38c079ee43e2b7da05648 Mon Sep 17 00:00:00 2001 From: Dominic Date: Sun, 3 Apr 2022 16:41:59 +0200 Subject: [PATCH] copy safety assertions from lottie2gif crate to lottie2webp crate --- lottie2webp/src/lib.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lottie2webp/src/lib.rs b/lottie2webp/src/lib.rs index a0158b47..529c19c9 100644 --- a/lottie2webp/src/lib.rs +++ b/lottie2webp/src/lib.rs @@ -13,6 +13,32 @@ pub use rlottie::Animation; #[macro_use] mod util; +/// It is very important that [`RGBA8`] and `[u8; 4]` have exactly the same size. +/// This mod does nothing other than fail to compile if that was not the case. +#[allow(dead_code)] +mod rgba_size { + use rgb::RGBA8; + use std::{marker::PhantomData, mem}; + + #[derive(Default)] + struct AssertSize(PhantomData<[(); N]>); + + impl AssertSize { + const fn new() -> Self { + Self(PhantomData) + } + } + + impl AssertSize<4> { + const fn assert_size_u8_4(self) {} + } + + const _: () = { + AssertSize::<{ mem::size_of::() }>::new().assert_size_u8_4(); + AssertSize::<{ mem::size_of::<[u8; 4]>() }>::new().assert_size_u8_4(); + }; +} + auto_vectorize! { pub(crate) fn bgra_to_rgba(buf_bgra: &[BGRA8], buf_rgba: &mut [RGBA8]) { for i in 0..buf_bgra.len() {