Skip to content

Commit

Permalink
Make a few palette::cast::* functions const fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Ogeon committed Jul 31, 2024
1 parent b60a87d commit 59ba91b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 48 deletions.
54 changes: 18 additions & 36 deletions palette/src/cast/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,12 @@ where
/// let array3 = <&[_; 3]>::from(&color);
/// ```
#[inline]
pub fn into_array_ref<T>(value: &T) -> &T::Array
pub const fn into_array_ref<T>(value: &T) -> &T::Array
where
T: ArrayCast,
{
assert_eq!(core::mem::size_of::<T::Array>(), core::mem::size_of::<T>());
assert_eq!(
core::mem::align_of::<T::Array>(),
core::mem::align_of::<T>()
);
assert!(core::mem::size_of::<T::Array>() == core::mem::size_of::<T>());
assert!(core::mem::align_of::<T::Array>() == core::mem::align_of::<T>());

let value: *const T = value;

Expand Down Expand Up @@ -282,15 +279,12 @@ where
/// let color3 = <&Srgb<u8>>::from(&array);
/// ```
#[inline]
pub fn from_array_ref<T>(value: &T::Array) -> &T
pub const fn from_array_ref<T>(value: &T::Array) -> &T
where
T: ArrayCast,
{
assert_eq!(core::mem::size_of::<T::Array>(), core::mem::size_of::<T>());
assert_eq!(
core::mem::align_of::<T::Array>(),
core::mem::align_of::<T>()
);
assert!(core::mem::size_of::<T::Array>() == core::mem::size_of::<T>());
assert!(core::mem::align_of::<T::Array>() == core::mem::align_of::<T>());

let value: *const T::Array = value;

Expand Down Expand Up @@ -598,15 +592,12 @@ where
/// assert_eq!(cast::into_array_slice(colors), &[[64, 139, 10], [93, 18, 214]])
/// ```
#[inline]
pub fn into_array_slice<T>(values: &[T]) -> &[T::Array]
pub const fn into_array_slice<T>(values: &[T]) -> &[T::Array]
where
T: ArrayCast,
{
assert_eq!(core::mem::size_of::<T::Array>(), core::mem::size_of::<T>());
assert_eq!(
core::mem::align_of::<T::Array>(),
core::mem::align_of::<T>()
);
assert!(core::mem::size_of::<T::Array>() == core::mem::size_of::<T>());
assert!(core::mem::align_of::<T::Array>() == core::mem::align_of::<T>());

// Safety: The requirements of implementing `ArrayCast`, as well as the size
// and alignment asserts, ensures that reading `T` as `T::Array` is safe.
Expand All @@ -623,15 +614,12 @@ where
/// assert_eq!(cast::into_component_slice(colors), &[64, 139, 10, 93, 18, 214])
/// ```
#[inline]
pub fn into_component_slice<T>(values: &[T]) -> &[<T::Array as ArrayExt>::Item]
pub const fn into_component_slice<T>(values: &[T]) -> &[<T::Array as ArrayExt>::Item]
where
T: ArrayCast,
{
assert_eq!(core::mem::size_of::<T::Array>(), core::mem::size_of::<T>());
assert_eq!(
core::mem::align_of::<T::Array>(),
core::mem::align_of::<T>()
);
assert!(core::mem::size_of::<T::Array>() == core::mem::size_of::<T>());
assert!(core::mem::align_of::<T::Array>() == core::mem::align_of::<T>());

let length = values.len() * T::Array::LENGTH;

Expand All @@ -658,15 +646,12 @@ where
/// )
/// ```
#[inline]
pub fn from_array_slice<T>(values: &[T::Array]) -> &[T]
pub const fn from_array_slice<T>(values: &[T::Array]) -> &[T]
where
T: ArrayCast,
{
assert_eq!(core::mem::size_of::<T::Array>(), core::mem::size_of::<T>());
assert_eq!(
core::mem::align_of::<T::Array>(),
core::mem::align_of::<T>()
);
assert!(core::mem::size_of::<T::Array>() == core::mem::size_of::<T>());
assert!(core::mem::align_of::<T::Array>() == core::mem::align_of::<T>());

// Safety: The requirements of implementing `ArrayCast`, as well as the size
// and alignment asserts, ensures that reading `T::Array` as `T` is safe.
Expand Down Expand Up @@ -737,17 +722,14 @@ where
/// assert!(cast::try_from_component_slice::<Srgb<u8>>(components).is_err());
/// ```
#[inline]
pub fn try_from_component_slice<T>(
pub const fn try_from_component_slice<T>(
values: &[<T::Array as ArrayExt>::Item],
) -> Result<&[T], SliceCastError>
where
T: ArrayCast,
{
assert_eq!(core::mem::size_of::<T::Array>(), core::mem::size_of::<T>());
assert_eq!(
core::mem::align_of::<T::Array>(),
core::mem::align_of::<T>()
);
assert!(core::mem::size_of::<T::Array>() == core::mem::size_of::<T>());
assert!(core::mem::align_of::<T::Array>() == core::mem::align_of::<T>());

if values.len() % T::Array::LENGTH != 0 {
return Err(SliceCastError);
Expand Down
24 changes: 12 additions & 12 deletions palette/src/cast/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ where
/// assert_eq!(cast::into_uint_ref(&color), &0xFF17C64C);
/// ```
#[inline]
pub fn into_uint_ref<T>(value: &T) -> &T::Uint
pub const fn into_uint_ref<T>(value: &T) -> &T::Uint
where
T: UintCast,
{
assert_eq!(core::mem::size_of::<T::Uint>(), core::mem::size_of::<T>());
assert_eq!(core::mem::align_of::<T::Uint>(), core::mem::align_of::<T>());
assert!(core::mem::size_of::<T::Uint>() == core::mem::size_of::<T>());
assert!(core::mem::align_of::<T::Uint>() == core::mem::align_of::<T>());

let value: *const T = value;

Expand All @@ -133,12 +133,12 @@ where
/// assert_eq!(cast::from_uint_ref::<PackedArgb>(&0xFF17C64C), &color);
/// ```
#[inline]
pub fn from_uint_ref<T>(value: &T::Uint) -> &T
pub const fn from_uint_ref<T>(value: &T::Uint) -> &T
where
T: UintCast,
{
assert_eq!(core::mem::size_of::<T::Uint>(), core::mem::size_of::<T>());
assert_eq!(core::mem::align_of::<T::Uint>(), core::mem::align_of::<T>());
assert!(core::mem::size_of::<T::Uint>() == core::mem::size_of::<T>());
assert!(core::mem::align_of::<T::Uint>() == core::mem::align_of::<T>());

let value: *const T::Uint = value;

Expand Down Expand Up @@ -255,12 +255,12 @@ where
/// assert_eq!(cast::into_uint_slice(colors), &[0xFF17C64C, 0xFF5D12D6])
/// ```
#[inline]
pub fn into_uint_slice<T>(values: &[T]) -> &[T::Uint]
pub const fn into_uint_slice<T>(values: &[T]) -> &[T::Uint]
where
T: UintCast,
{
assert_eq!(core::mem::size_of::<T::Uint>(), core::mem::size_of::<T>());
assert_eq!(core::mem::align_of::<T::Uint>(), core::mem::align_of::<T>());
assert!(core::mem::size_of::<T::Uint>() == core::mem::size_of::<T>());
assert!(core::mem::align_of::<T::Uint>() == core::mem::align_of::<T>());

// Safety: The requirements of implementing `UintCast`, as well as the size
// and alignment asserts, ensures that reading `T` as `T::Uint` is safe.
Expand All @@ -280,12 +280,12 @@ where
/// assert_eq!(cast::from_uint_slice::<PackedArgb>(&[0xFF17C64C, 0xFF5D12D6]), colors)
/// ```
#[inline]
pub fn from_uint_slice<T>(values: &[T::Uint]) -> &[T]
pub const fn from_uint_slice<T>(values: &[T::Uint]) -> &[T]
where
T: UintCast,
{
assert_eq!(core::mem::size_of::<T::Uint>(), core::mem::size_of::<T>());
assert_eq!(core::mem::align_of::<T::Uint>(), core::mem::align_of::<T>());
assert!(core::mem::size_of::<T::Uint>() == core::mem::size_of::<T>());
assert!(core::mem::align_of::<T::Uint>() == core::mem::align_of::<T>());

// Safety: The requirements of implementing `UintCast`, as well as the size
// and alignment asserts, ensures that reading `T::Uint` as `T` is safe.
Expand Down

0 comments on commit 59ba91b

Please sign in to comment.