Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hybrid-array: add slice cast methods to ArrayOps #1038

Merged
merged 4 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions hybrid-array/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,18 @@ pub trait ArrayOps<T, const N: usize>:
fn map_to_core_array<F, U>(self, f: F) -> [U; N]
where
F: FnMut(T) -> U;

/// Transform slice to slice of core array type
fn cast_slice_to_core(slice: &[Self]) -> &[[T; N]];

/// Transform mutable slice to mutable slice of core array type
fn cast_slice_to_core_mut(slice: &mut [Self]) -> &mut [[T; N]];

/// Transform slice to slice of core array type
fn cast_slice_from_core(slice: &[[T; N]]) -> &[Self];

/// Transform mutable slice to mutable slice of core array type
fn cast_slice_from_core_mut(slice: &mut [[T; N]]) -> &mut [Self];
}

impl<T, const N: usize> ArrayOps<T, N> for [T; N] {
Expand Down Expand Up @@ -697,6 +709,22 @@ impl<T, const N: usize> ArrayOps<T, N> for [T; N] {
{
self.map(f)
}

fn cast_slice_to_core(slice: &[Self]) -> &[[T; N]] {
slice
}

fn cast_slice_to_core_mut(slice: &mut [Self]) -> &mut [[T; N]] {
slice
}

fn cast_slice_from_core(slice: &[[T; N]]) -> &[Self] {
slice
}

fn cast_slice_from_core_mut(slice: &mut [[T; N]]) -> &mut [Self] {
slice
}
}

/// Slice operations which don't have access to a const generic array size.
Expand Down
20 changes: 20 additions & 0 deletions hybrid-array/src/sizes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ macro_rules! impl_array_size {
{
self.0.map(f)
}

fn cast_slice_to_core(slice: &[Self]) -> &[[T; $len]] {
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]`
unsafe { core::slice::from_raw_parts(slice.as_ptr().cast(), slice.len()) }
}

fn cast_slice_to_core_mut(slice: &mut [Self]) -> &mut [[T; $len]] {
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]`
unsafe { core::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len()) }
}

fn cast_slice_from_core(slice: &[[T; $len]]) -> &[Self] {
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]`
unsafe { core::slice::from_raw_parts(slice.as_ptr().cast(), slice.len()) }
}

fn cast_slice_from_core_mut(slice: &mut [[T; $len]]) -> &mut [Self] {
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]`
unsafe { core::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len()) }
}
}
)+
};
Expand Down