Skip to content

Commit 46e94f5

Browse files
authored
Remove ArrayOps and SliceOps (#30)
After #24, #25, and #26, these traits don't really need to exist and have largely been replaced by safe type conversions with appropriate bounds. In lieu of `ArrayOps<T, N>` we instead can use `U: ArraySize<ArrayType<T> = [T; N]>>` as a bound, which albeit a bit more verbose concretely describes the inner type of `Array` to the compiler. The `cast_slice_(to|from)_core(_mut)` methods previously defined on the trait have been preserved, but as static methods of `Array`, making the change largely a drop in replacement. Places where they were being called as e.g. `ArrayOps::cast_slice_to_core` just need to be called as `Array::cast_slice_to_core` instead.
1 parent ca926e4 commit 46e94f5

File tree

3 files changed

+45
-139
lines changed

3 files changed

+45
-139
lines changed

src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,40 @@ where
249249
}
250250
}
251251

252+
// Impls which depend on the inner array type being `[T; N]`.
253+
impl<T, U, const N: usize> Array<T, U>
254+
where
255+
U: ArraySize<ArrayType<T> = [T; N]>,
256+
{
257+
/// Transform slice to slice of core array type.
258+
#[inline]
259+
pub fn cast_slice_to_core(slice: &[Self]) -> &[[T; N]] {
260+
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; N]`
261+
unsafe { core::slice::from_raw_parts(slice.as_ptr().cast(), slice.len()) }
262+
}
263+
264+
/// Transform mutable slice to mutable slice of core array type.
265+
#[inline]
266+
pub fn cast_slice_to_core_mut(slice: &mut [Self]) -> &mut [[T; N]] {
267+
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; N]`
268+
unsafe { core::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len()) }
269+
}
270+
271+
/// Transform slice to slice of core array type.
272+
#[inline]
273+
pub fn cast_slice_from_core(slice: &[[T; N]]) -> &[Self] {
274+
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; N]`
275+
unsafe { core::slice::from_raw_parts(slice.as_ptr().cast(), slice.len()) }
276+
}
277+
278+
/// Transform mutable slice to mutable slice of core array type.
279+
#[inline]
280+
pub fn cast_slice_from_core_mut(slice: &mut [[T; N]]) -> &mut [Self] {
281+
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; N]`
282+
unsafe { core::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len()) }
283+
}
284+
}
285+
252286
impl<T, U, const N: usize> Array<MaybeUninit<T>, U>
253287
where
254288
U: ArraySize<ArrayType<MaybeUninit<T>> = [MaybeUninit<T>; N]>,

src/sizes.rs

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Macros for defining various array sizes, and their associated invocations.
22
3-
use super::{Array, ArrayOps, ArraySize, AssociatedArraySize};
3+
use super::{ArraySize, AssociatedArraySize};
44

55
macro_rules! impl_array_size {
66
($($len:expr => $ty:ident),+) => {
@@ -12,42 +12,6 @@ macro_rules! impl_array_size {
1212
impl<T> AssociatedArraySize for [T; $len] {
1313
type Size = typenum::$ty;
1414
}
15-
16-
impl<T> ArrayOps<T, $len> for Array<T, typenum::$ty> {
17-
const SIZE: usize = $len;
18-
19-
#[inline]
20-
fn map_to_core_array<F, U>(self, f: F) -> [U; $len]
21-
where
22-
F: FnMut(T) -> U
23-
{
24-
self.0.map(f)
25-
}
26-
27-
#[inline]
28-
fn cast_slice_to_core(slice: &[Self]) -> &[[T; $len]] {
29-
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]`
30-
unsafe { core::slice::from_raw_parts(slice.as_ptr().cast(), slice.len()) }
31-
}
32-
33-
#[inline]
34-
fn cast_slice_to_core_mut(slice: &mut [Self]) -> &mut [[T; $len]] {
35-
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]`
36-
unsafe { core::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len()) }
37-
}
38-
39-
#[inline]
40-
fn cast_slice_from_core(slice: &[[T; $len]]) -> &[Self] {
41-
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]`
42-
unsafe { core::slice::from_raw_parts(slice.as_ptr().cast(), slice.len()) }
43-
}
44-
45-
#[inline]
46-
fn cast_slice_from_core_mut(slice: &mut [[T; $len]]) -> &mut [Self] {
47-
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]`
48-
unsafe { core::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len()) }
49-
}
50-
}
5115
)+
5216
};
5317
}

src/traits.rs

Lines changed: 10 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Trait definitions.
22
3-
use crate::{slice_as_chunks, slice_as_chunks_mut, Array, FromFn};
3+
use crate::{Array, FromFn};
44
use core::{
55
borrow::{Borrow, BorrowMut},
66
ops::{Index, IndexMut, Range},
@@ -23,11 +23,18 @@ pub unsafe trait ArraySize: Unsigned {
2323
/// This is always defined to be `[T; N]` where `N` is the same as
2424
/// [`ArraySize::USIZE`][`typenum::Unsigned::USIZE`].
2525
type ArrayType<T>: AssociatedArraySize<Size = Self>
26+
+ AsRef<[T]>
27+
+ AsMut<[T]>
28+
+ Borrow<[T]>
29+
+ BorrowMut<[T]>
2630
+ From<Array<T, Self>>
2731
+ FromFn<T>
32+
+ Index<usize>
33+
+ Index<Range<usize>>
34+
+ IndexMut<usize>
35+
+ IndexMut<Range<usize>>
2836
+ Into<Array<T, Self>>
29-
+ IntoIterator<Item = T>
30-
+ SliceOps<T>;
37+
+ IntoIterator<Item = T>;
3138
}
3239

3340
/// Associates an [`ArraySize`] with a given type.
@@ -42,102 +49,3 @@ where
4249
{
4350
type Size = U;
4451
}
45-
46-
/// Array operations which are const generic over a given array size.
47-
pub trait ArrayOps<T, const N: usize>:
48-
Borrow<[T; N]>
49-
+ BorrowMut<[T; N]>
50-
+ From<[T; N]>
51-
+ FromFn<T>
52-
+ Into<[T; N]>
53-
+ IntoIterator<Item = T>
54-
+ Sized
55-
+ SliceOps<T>
56-
{
57-
/// Size of an array as a `usize`.
58-
///
59-
/// Not to be confused with [`AssociatedArraySize::Size`], which is [`typenum`]-based.
60-
const SIZE: usize;
61-
62-
/// Returns an array of the same size as `self`, with function `f` applied to each element
63-
/// in order.
64-
fn map_to_core_array<F, U>(self, f: F) -> [U; N]
65-
where
66-
F: FnMut(T) -> U;
67-
68-
/// Transform slice to slice of core array type
69-
fn cast_slice_to_core(slice: &[Self]) -> &[[T; N]];
70-
71-
/// Transform mutable slice to mutable slice of core array type
72-
fn cast_slice_to_core_mut(slice: &mut [Self]) -> &mut [[T; N]];
73-
74-
/// Transform slice to slice of core array type
75-
fn cast_slice_from_core(slice: &[[T; N]]) -> &[Self];
76-
77-
/// Transform mutable slice to mutable slice of core array type
78-
fn cast_slice_from_core_mut(slice: &mut [[T; N]]) -> &mut [Self];
79-
}
80-
81-
impl<T, const N: usize> ArrayOps<T, N> for [T; N] {
82-
const SIZE: usize = N;
83-
84-
#[inline]
85-
fn map_to_core_array<F, U>(self, f: F) -> [U; N]
86-
where
87-
F: FnMut(T) -> U,
88-
{
89-
self.map(f)
90-
}
91-
92-
#[inline]
93-
fn cast_slice_to_core(slice: &[Self]) -> &[[T; N]] {
94-
slice
95-
}
96-
97-
#[inline]
98-
fn cast_slice_to_core_mut(slice: &mut [Self]) -> &mut [[T; N]] {
99-
slice
100-
}
101-
102-
#[inline]
103-
fn cast_slice_from_core(slice: &[[T; N]]) -> &[Self] {
104-
slice
105-
}
106-
107-
#[inline]
108-
fn cast_slice_from_core_mut(slice: &mut [[T; N]]) -> &mut [Self] {
109-
slice
110-
}
111-
}
112-
113-
/// Slice operations which don't have access to a const generic array size.
114-
pub trait SliceOps<T>:
115-
AsRef<[T]>
116-
+ AsMut<[T]>
117-
+ Borrow<[T]>
118-
+ BorrowMut<[T]>
119-
+ Index<usize>
120-
+ Index<Range<usize>>
121-
+ IndexMut<usize>
122-
+ IndexMut<Range<usize>>
123-
{
124-
/// Splits the shared slice into a slice of `N`-element arrays.
125-
///
126-
/// See [`slice_as_chunks`] for more information.
127-
#[inline]
128-
fn as_array_chunks<N: ArraySize>(&self) -> (&[Array<T, N>], &[T]) {
129-
slice_as_chunks(self.as_ref())
130-
}
131-
132-
/// Splits the exclusive slice into a slice of `N`-element arrays.
133-
///
134-
/// See [`slice_as_chunks_mut`] for more information.
135-
#[inline]
136-
fn as_array_chunks_mut<N: ArraySize>(&mut self) -> (&mut [Array<T, N>], &mut [T]) {
137-
slice_as_chunks_mut(self.as_mut())
138-
}
139-
}
140-
141-
impl<T> SliceOps<T> for [T] {}
142-
impl<T, const N: usize> SliceOps<T> for [T; N] {}
143-
impl<T, U: ArraySize> SliceOps<T> for Array<T, U> {}

0 commit comments

Comments
 (0)