mirror of
https://github.com/bend-n/fimg.git
synced 2024-12-22 10:28:21 -06:00
futureproof transmutation issues
This commit is contained in:
parent
293121e5f5
commit
2c9b749230
14
src/lib.rs
14
src/lib.rs
|
@ -86,7 +86,7 @@
|
||||||
confusable_idents,
|
confusable_idents,
|
||||||
internal_features
|
internal_features
|
||||||
)]
|
)]
|
||||||
use std::{hint::assert_unchecked, num::NonZeroU32, ops::Range};
|
use std::{hint::assert_unchecked, intrinsics::transmute_unchecked, num::NonZeroU32, ops::Range};
|
||||||
|
|
||||||
mod affine;
|
mod affine;
|
||||||
#[cfg(feature = "blur")]
|
#[cfg(feature = "blur")]
|
||||||
|
@ -448,6 +448,18 @@ impl<T, const CHANNELS: usize> Image<T, CHANNELS> {
|
||||||
self.buffer().as_ref().len()
|
self.buffer().as_ref().len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Transforms the N
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// i think you can see why this is a problem.
|
||||||
|
///
|
||||||
|
/// # WHY???
|
||||||
|
///
|
||||||
|
/// sometimes rust is silly with generics
|
||||||
|
unsafe fn trans<const N: usize>(self) -> Image<T, N> {
|
||||||
|
unsafe { transmute_unchecked(self) }
|
||||||
|
}
|
||||||
/// # Safety
|
/// # Safety
|
||||||
///
|
///
|
||||||
/// the output index is not guaranteed to be in bounds
|
/// the output index is not guaranteed to be in bounds
|
||||||
|
|
|
@ -50,16 +50,17 @@ where
|
||||||
let (w, h) = fit((self.width(), self.height()));
|
let (w, h) = fit((self.width(), self.height()));
|
||||||
macro_rules! n {
|
macro_rules! n {
|
||||||
($n:literal) => {
|
($n:literal) => {
|
||||||
transmute::<Image<Box<[u8]>, $n>, Image<Box<[u8]>, N>>(
|
self.as_ref()
|
||||||
transmute::<Image<&[u8], N>, Image<&[u8], $n>>(self.as_ref())
|
.trans::<$n>()
|
||||||
.scale::<scale::Nearest>(w, h),
|
.scale::<scale::Nearest>(w, h)
|
||||||
)
|
.trans::<N>()
|
||||||
};
|
};
|
||||||
(o $n:literal) => {
|
(o $n:literal) => {
|
||||||
transmute::<Image<Box<[u8]>, 1>, Image<Box<[u8]>, N>>(
|
self.as_ref()
|
||||||
transmute::<Image<Vec<u8>, N>, Image<Vec<u8>, 1>>(self.as_ref().to_owned())
|
.to_owned()
|
||||||
.scale::<scale::Nearest>(w, h),
|
.trans::<1>()
|
||||||
)
|
.scale::<scale::Nearest>(w, h)
|
||||||
|
.trans::<N>()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// SAFETY: #[allow(clippy::undocumented_unsafe_blocks)]
|
// SAFETY: #[allow(clippy::undocumented_unsafe_blocks)]
|
||||||
|
|
|
@ -42,7 +42,7 @@ where
|
||||||
($n:literal) => {
|
($n:literal) => {
|
||||||
WritePng::write(
|
WritePng::write(
|
||||||
// SAFETY: ... i renounce traits
|
// SAFETY: ... i renounce traits
|
||||||
&unsafe { transmute::<Image<&[u8], N>, Image<&[u8], $n>>(self.as_ref()) },
|
&unsafe { self.as_ref().trans::<$n>() },
|
||||||
&mut d,
|
&mut d,
|
||||||
)
|
)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|
|
@ -45,7 +45,7 @@ impl<T: AsRef<[u8]>, const N: usize> Kitty<T, N> {
|
||||||
Cow::Owned(
|
Cow::Owned(
|
||||||
<Image<Box<[u8]>, 3>>::from({
|
<Image<Box<[u8]>, 3>>::from({
|
||||||
// SAFETY: ...
|
// SAFETY: ...
|
||||||
unsafe { transmute::<Image<&[u8], N>, Image<&[u8], $n>>(self.as_ref()) }
|
unsafe { self.as_ref().trans::<$n>() }
|
||||||
})
|
})
|
||||||
.take_buffer()
|
.take_buffer()
|
||||||
.to_vec(),
|
.to_vec(),
|
||||||
|
|
Loading…
Reference in a new issue