Skip to content

Commit af4847f

Browse files
authored
hybrid-array: make slice conversions const fn (#140)
I noticed all of these functions can be trivially made `const fn`, not that I have a particular use case in mind.
1 parent 433a709 commit af4847f

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ where
292292
/// Panics if `U` is 0.
293293
#[allow(clippy::arithmetic_side_effects)]
294294
#[inline]
295-
pub fn slice_as_chunks(buf: &[T]) -> (&[Self], &[T]) {
296-
assert_ne!(U::USIZE, 0, "chunk size must be non-zero");
295+
pub const fn slice_as_chunks(buf: &[T]) -> (&[Self], &[T]) {
296+
assert!(U::USIZE != 0, "chunk size must be non-zero");
297297
// Arithmetic safety: we have checked that `N::USIZE` is not zero, thus
298298
// division always returns correct result. `tail_pos` can not be bigger than `buf.len()`,
299299
// thus overflow on multiplication and underflow on substraction are impossible.
@@ -315,8 +315,8 @@ where
315315
/// Panics if `U` is 0.
316316
#[allow(clippy::arithmetic_side_effects)]
317317
#[inline]
318-
pub fn slice_as_chunks_mut(buf: &mut [T]) -> (&mut [Self], &mut [T]) {
319-
assert_ne!(U::USIZE, 0, "chunk size must be non-zero");
318+
pub const fn slice_as_chunks_mut(buf: &mut [T]) -> (&mut [Self], &mut [T]) {
319+
assert!(U::USIZE != 0, "chunk size must be non-zero");
320320
// Arithmetic safety: we have checked that `N::USIZE` is not zero, thus
321321
// division always returns correct result. `tail_pos` can not be bigger than `buf.len()`,
322322
// thus overflow on multiplication and underflow on substraction are impossible.
@@ -375,28 +375,28 @@ where
375375
{
376376
/// Transform slice to slice of core array type.
377377
#[inline]
378-
pub fn cast_slice_to_core(slice: &[Self]) -> &[[T; N]] {
378+
pub const fn cast_slice_to_core(slice: &[Self]) -> &[[T; N]] {
379379
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; N]`
380380
unsafe { slice::from_raw_parts(slice.as_ptr().cast(), slice.len()) }
381381
}
382382

383383
/// Transform mutable slice to mutable slice of core array type.
384384
#[inline]
385-
pub fn cast_slice_to_core_mut(slice: &mut [Self]) -> &mut [[T; N]] {
385+
pub const fn cast_slice_to_core_mut(slice: &mut [Self]) -> &mut [[T; N]] {
386386
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; N]`
387387
unsafe { slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len()) }
388388
}
389389

390390
/// Transform slice to slice of core array type.
391391
#[inline]
392-
pub fn cast_slice_from_core(slice: &[[T; N]]) -> &[Self] {
392+
pub const fn cast_slice_from_core(slice: &[[T; N]]) -> &[Self] {
393393
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; N]`
394394
unsafe { slice::from_raw_parts(slice.as_ptr().cast(), slice.len()) }
395395
}
396396

397397
/// Transform mutable slice to mutable slice of core array type.
398398
#[inline]
399-
pub fn cast_slice_from_core_mut(slice: &mut [[T; N]]) -> &mut [Self] {
399+
pub const fn cast_slice_from_core_mut(slice: &mut [[T; N]]) -> &mut [Self] {
400400
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; N]`
401401
unsafe { slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len()) }
402402
}

0 commit comments

Comments
 (0)