Skip to content

Commit

Permalink
Merge pull request #405 from Ogeon/const_fn
Browse files Browse the repository at this point in the history
Make hue `::from_degrees` and some `palette::cast::*` functions `const fn`
  • Loading branch information
Ogeon authored Jul 31, 2024
2 parents afa3b12 + 13368ac commit 7971660
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 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
2 changes: 1 addition & 1 deletion palette/src/hues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ macro_rules! make_hues {
impl<T: RealAngle> $name<T> {
/// Create a new hue from degrees. This is an alias for `new`.
#[inline]
pub fn from_degrees(degrees: T) -> Self {
pub const fn from_degrees(degrees: T) -> Self {
Self::new(degrees)
}

Expand Down

0 comments on commit 7971660

Please sign in to comment.