Skip to content
Merged
Changes from all commits
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
78 changes: 29 additions & 49 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ where
U: ArraySize,
V: ArraySize,
{
/// Takes a `&mut Array<Array<T, N>,M>`, and flattens it to a `&mut [T]`.
/// Takes a `&Array<Array<T, N>, >>`, and flattens it to a `&[T]`.
///
/// # Panics
///
Expand All @@ -376,33 +376,28 @@ where
/// # Examples
///
/// ```
/// use hybrid_array::{Array, typenum::U3};
/// use hybrid_array::{Array, typenum::{U0, U2, U3, U5, U10}};
///
/// fn add_5_to_all(slice: &mut [i32]) {
/// for i in slice {
/// *i += 5;
/// }
/// }
/// let a: Array<Array<usize, U3>, U2> = Array([Array([1, 2, 3]), Array([4, 5, 6])]);
/// assert_eq!(a.as_flattened(), &[1, 2, 3, 4, 5, 6]);
///
/// let mut array: Array<Array<i32, U3>, U3> = Array([Array([1_i32, 2, 3]), Array([4, 5, 6]), Array([7, 8, 9])]);
/// add_5_to_all(array.as_flattened_mut());
/// assert_eq!(array, Array([Array([6, 7, 8]), Array([9, 10, 11]), Array([12, 13, 14])]));
/// let b: Array<Array<usize, U2>, U3> = Array([Array([1, 2]), Array([3, 4]), Array([5, 6])]);
/// assert_eq!(a.as_flattened(), b.as_flattened());
///
/// let c: Array<[usize; 2], U3> = Array([[1, 2], [3, 4], [5, 6]]);
/// assert_eq!(a.as_flattened(), c.as_flattened());
///
/// let slice_of_empty_arrays: &Array<Array<i32, U5>, U0> = &Array::from_fn(|_| Array([1, 2, 3, 4, 5]));
/// assert!(slice_of_empty_arrays.as_flattened().is_empty());
///
/// let empty_slice_of_arrays: &Array<Array<u32, U10>, U0> = &Array([]);
/// assert!(empty_slice_of_arrays.as_flattened().is_empty());
/// ```
pub fn as_flattened_mut(&mut self) -> &mut [T] {
let len = if size_of::<T>() == 0 {
self.len()
.checked_mul(U::USIZE)
.expect("slice len overflow")
} else {
// SAFETY: `self.len() * N` cannot overflow because `self` is
// already in the address space.
unsafe { self.len().unchecked_mul(U::USIZE) }
};
// SAFETY: `[T]` is layout-identical to `[T; U]`
unsafe { slice::from_raw_parts_mut(self.as_mut_ptr().cast(), len) }
pub fn as_flattened(&self) -> &[T] {
Array::slice_as_flattened(self.as_slice())
}

/// Takes a `&Array<Array<T, N>, >>`, and flattens it to a `&[T]`.
/// Takes a `&mut Array<Array<T, N>,M>`, and flattens it to a `&mut [T]`.
///
/// # Panics
///
Expand All @@ -415,35 +410,20 @@ where
/// # Examples
///
/// ```
/// use hybrid_array::{Array, typenum::{U0, U2, U3, U5, U10}};
///
/// let a: Array<Array<usize, U3>, U2> = Array([Array([1, 2, 3]), Array([4, 5, 6])]);
/// assert_eq!(a.as_flattened(), &[1, 2, 3, 4, 5, 6]);
///
/// let b: Array<Array<usize, U2>, U3> = Array([Array([1, 2]), Array([3, 4]), Array([5, 6])]);
/// assert_eq!(a.as_flattened(), b.as_flattened());
///
/// let c: Array<[usize; 2], U3> = Array([[1, 2], [3, 4], [5, 6]]);
/// assert_eq!(a.as_flattened(), c.as_flattened());
/// use hybrid_array::{Array, typenum::U3};
///
/// let slice_of_empty_arrays: &Array<Array<i32, U5>, U0> = &Array::from_fn(|_| Array([1, 2, 3, 4, 5]));
/// assert!(slice_of_empty_arrays.as_flattened().is_empty());
/// fn add_5_to_all(slice: &mut [i32]) {
/// for i in slice {
/// *i += 5;
/// }
/// }
///
/// let empty_slice_of_arrays: &Array<Array<u32, U10>, U0> = &Array([]);
/// assert!(empty_slice_of_arrays.as_flattened().is_empty());
/// let mut array: Array<Array<i32, U3>, U3> = Array([Array([1_i32, 2, 3]), Array([4, 5, 6]), Array([7, 8, 9])]);
/// add_5_to_all(array.as_flattened_mut());
/// assert_eq!(array, Array([Array([6, 7, 8]), Array([9, 10, 11]), Array([12, 13, 14])]));
/// ```
pub fn as_flattened(&self) -> &[T] {
let len = if size_of::<T>() == 0 {
self.len()
.checked_mul(U::USIZE)
.expect("slice len overflow")
} else {
// SAFETY: `self.len() * N` cannot overflow because `self` is
// already in the address space.
unsafe { self.len().unchecked_mul(U::USIZE) }
};
// SAFETY: `[T]` is layout-identical to `[T; U]`
unsafe { slice::from_raw_parts(self.as_ptr().cast(), len) }
pub fn as_flattened_mut(&mut self) -> &mut [T] {
Array::slice_as_flattened_mut(self.as_mut_slice())
}
}

Expand Down