use super::{
from_uint_array, from_uint_slice, from_uint_slice_mut, into_uint_array, into_uint_slice,
into_uint_slice_mut, UintCast,
};
#[cfg(feature = "alloc")]
use super::{from_uint_slice_box, from_uint_vec, into_uint_slice_box, into_uint_vec};
pub trait FromUints<U> {
fn from_uints(uints: U) -> Self;
}
impl<C, const N: usize> FromUints<[C::Uint; N]> for [C; N]
where
C: UintCast,
{
#[inline]
fn from_uints(uints: [C::Uint; N]) -> Self {
from_uint_array(uints)
}
}
macro_rules! impl_from_uints_slice {
($($owning:ty $(where ($($ty_input:tt)+))?),*) => {
$(
impl<'a, C $(, $($ty_input)+)?> FromUints<&'a $owning> for &'a [C]
where
C: UintCast,
{
#[inline]
fn from_uints(uints: &'a $owning) -> Self {
from_uint_slice(uints)
}
}
impl<'a, C $(, $($ty_input)+)?> FromUints<&'a mut $owning> for &'a mut [C]
where
C: UintCast,
{
#[inline]
fn from_uints(uints: &'a mut $owning) -> Self {
from_uint_slice_mut(uints)
}
}
)*
};
}
impl_from_uints_slice!([C::Uint], [C::Uint; N] where (const N: usize));
#[cfg(feature = "alloc")]
impl_from_uints_slice!(alloc::boxed::Box<[C::Uint]>, alloc::vec::Vec<C::Uint>);
#[cfg(feature = "alloc")]
impl<C> FromUints<alloc::boxed::Box<[C::Uint]>> for alloc::boxed::Box<[C]>
where
C: UintCast,
{
#[inline]
fn from_uints(uints: alloc::boxed::Box<[C::Uint]>) -> Self {
from_uint_slice_box(uints)
}
}
#[cfg(feature = "alloc")]
impl<C> FromUints<alloc::vec::Vec<C::Uint>> for alloc::vec::Vec<C>
where
C: UintCast,
{
#[inline]
fn from_uints(uints: alloc::vec::Vec<C::Uint>) -> Self {
from_uint_vec(uints)
}
}
pub trait IntoUints<U> {
fn into_uints(self) -> U;
}
impl<C, const N: usize> IntoUints<[C::Uint; N]> for [C; N]
where
C: UintCast,
{
#[inline]
fn into_uints(self) -> [C::Uint; N] {
into_uint_array(self)
}
}
macro_rules! impl_into_uints_slice {
($($owning:ty $(where ($($ty_input:tt)+))?),*) => {
$(
impl<'a, C $(, $($ty_input)+)?> IntoUints<&'a [C::Uint]> for &'a $owning
where
C: UintCast,
{
#[inline]
fn into_uints(self) -> &'a [C::Uint] {
into_uint_slice(self)
}
}
impl<'a, C $(, $($ty_input)+)?> IntoUints<&'a mut [C::Uint]> for &'a mut $owning
where
C: UintCast,
{
#[inline]
fn into_uints(self) -> &'a mut [C::Uint] {
into_uint_slice_mut(self)
}
}
)*
};
}
impl_into_uints_slice!([C], [C; M] where (const M: usize));
#[cfg(feature = "alloc")]
impl_into_uints_slice!(alloc::boxed::Box<[C]>, alloc::vec::Vec<C>);
#[cfg(feature = "alloc")]
impl<C> IntoUints<alloc::boxed::Box<[C::Uint]>> for alloc::boxed::Box<[C]>
where
C: UintCast,
{
#[inline]
fn into_uints(self) -> alloc::boxed::Box<[C::Uint]> {
into_uint_slice_box(self)
}
}
#[cfg(feature = "alloc")]
impl<C> IntoUints<alloc::vec::Vec<C::Uint>> for alloc::vec::Vec<C>
where
C: UintCast,
{
#[inline]
fn into_uints(self) -> alloc::vec::Vec<C::Uint> {
into_uint_vec(self)
}
}
pub trait UintsFrom<C> {
fn uints_from(colors: C) -> Self;
}
impl<C, U> UintsFrom<C> for U
where
C: IntoUints<U>,
{
#[inline]
fn uints_from(colors: C) -> Self {
colors.into_uints()
}
}
pub trait UintsInto<C> {
fn uints_into(self) -> C;
}
impl<C, U> UintsInto<C> for U
where
C: FromUints<U>,
{
#[inline]
fn uints_into(self) -> C {
C::from_uints(self)
}
}
#[cfg(test)]
mod test {
use crate::{rgb::PackedRgba, Srgba};
use super::{FromUints, IntoUints, UintsFrom, UintsInto};
#[test]
fn from_uints() {
let slice: &[u32] = &[0x01020304, 0x05060708];
let slice_mut: &mut [u32] = &mut [0x01020304, 0x05060708];
let mut array: [u32; 2] = [0x01020304, 0x05060708];
let _ = <&[PackedRgba]>::from_uints(slice);
let _ = <&[PackedRgba]>::from_uints(&array);
let _ = <&mut [PackedRgba]>::from_uints(slice_mut);
let _ = <&mut [PackedRgba]>::from_uints(&mut array);
let _ = <[PackedRgba; 2]>::from_uints(array);
}
#[cfg(feature = "alloc")]
#[test]
fn from_uints_alloc() {
let mut slice_box: Box<[u32]> = vec![0x01020304, 0x05060708].into_boxed_slice();
let mut vec: Vec<u32> = vec![0x01020304, 0x05060708];
let _ = <&[PackedRgba]>::from_uints(&slice_box);
let _ = <&[PackedRgba]>::from_uints(&vec);
let _ = <&mut [PackedRgba]>::from_uints(&mut slice_box);
let _ = <&mut [PackedRgba]>::from_uints(&mut vec);
let _ = Box::<[PackedRgba]>::from_uints(slice_box);
let _ = Vec::<PackedRgba>::from_uints(vec);
}
#[test]
fn uints_into() {
let slice: &[u32] = &[0x01020304, 0x05060708];
let slice_mut: &mut [u32] = &mut [0x01020304, 0x05060708];
let mut array: [u32; 2] = [0x01020304, 0x05060708];
let _: &[PackedRgba] = slice.uints_into();
let _: &[PackedRgba] = (&array).uints_into();
let _: &mut [PackedRgba] = slice_mut.uints_into();
let _: &mut [PackedRgba] = (&mut array).uints_into();
let _: [PackedRgba; 2] = array.uints_into();
}
#[cfg(feature = "alloc")]
#[test]
fn uints_into_alloc() {
let mut slice_box: Box<[u32]> = vec![0x01020304, 0x05060708].into_boxed_slice();
let mut vec: Vec<u32> = vec![0x01020304, 0x05060708];
let _: &[PackedRgba] = (&slice_box).uints_into();
let _: &[PackedRgba] = (&vec).uints_into();
let _: &mut [PackedRgba] = (&mut slice_box).uints_into();
let _: &mut [PackedRgba] = (&mut vec).uints_into();
let _: Box<[PackedRgba]> = slice_box.uints_into();
let _: Vec<PackedRgba> = vec.uints_into();
}
#[test]
fn into_uints() {
let slice: &[PackedRgba] = &[Srgba::new(1, 2, 3, 4).into(), Srgba::new(5, 6, 7, 8).into()];
let slice_mut: &mut [PackedRgba] =
&mut [Srgba::new(1, 2, 3, 4).into(), Srgba::new(5, 6, 7, 8).into()];
let mut array: [PackedRgba; 2] =
[Srgba::new(1, 2, 3, 4).into(), Srgba::new(5, 6, 7, 8).into()];
let _: &[u32] = slice.into_uints();
let _: &[u32] = (&array).into_uints();
let _: &mut [u32] = slice_mut.into_uints();
let _: &mut [u32] = (&mut array).into_uints();
let _: [u32; 2] = array.into_uints();
}
#[cfg(feature = "alloc")]
#[test]
fn into_uints_alloc() {
let mut slice_box: Box<[PackedRgba]> =
vec![Srgba::new(1, 2, 3, 4).into(), Srgba::new(5, 6, 7, 8).into()].into_boxed_slice();
let mut vec: Vec<PackedRgba> =
vec![Srgba::new(1, 2, 3, 4).into(), Srgba::new(5, 6, 7, 8).into()];
let _: &[u32] = (&slice_box).into_uints();
let _: &[u32] = (&vec).into_uints();
let _: &mut [u32] = (&mut slice_box).into_uints();
let _: &mut [u32] = (&mut vec).into_uints();
let _: Box<[u32]> = slice_box.into_uints();
let _: Vec<u32> = vec.into_uints();
}
#[test]
fn uints_from() {
let slice: &[PackedRgba] = &[Srgba::new(1, 2, 3, 4).into(), Srgba::new(5, 6, 7, 8).into()];
let slice_mut: &mut [PackedRgba] =
&mut [Srgba::new(1, 2, 3, 4).into(), Srgba::new(5, 6, 7, 8).into()];
let mut array: [PackedRgba; 2] =
[Srgba::new(1, 2, 3, 4).into(), Srgba::new(5, 6, 7, 8).into()];
let _ = <&[u32]>::uints_from(slice);
let _ = <&[u32]>::uints_from(&array);
let _ = <&mut [u32]>::uints_from(slice_mut);
let _ = <&mut [u32]>::uints_from(&mut array);
let _ = <[u32; 2]>::uints_from(array);
}
#[cfg(feature = "alloc")]
#[test]
fn uints_from_alloc() {
let mut slice_box: Box<[PackedRgba]> =
vec![Srgba::new(1, 2, 3, 4).into(), Srgba::new(5, 6, 7, 8).into()].into_boxed_slice();
let mut vec: Vec<PackedRgba> =
vec![Srgba::new(1, 2, 3, 4).into(), Srgba::new(5, 6, 7, 8).into()];
let _ = <&[u32]>::uints_from(&slice_box);
let _ = <&[u32]>::uints_from(&vec);
let _ = <&mut [u32]>::uints_from(&mut slice_box);
let _ = <&mut [u32]>::uints_from(&mut vec);
let _ = Box::<[u32]>::uints_from(slice_box);
let _ = Vec::<u32>::uints_from(vec);
}
}