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 13368ac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
18 changes: 6 additions & 12 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
12 changes: 6 additions & 6 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

0 comments on commit 13368ac

Please sign in to comment.