Skip to content

Commit ef954cb

Browse files
authored
Remove uses of ArrayOps bounds (#25)
Continuation of the approach from #24 which replaces `ArrayOps<T, N>` bounds with `ArraySize<ArrayType<T> = [T; N]>`. Though a bit more verbose, this makes the inner type of `Array` explicit and therefore allows more safe conversions as well as making `unsafe` code easier to reason about, since the inner type is no longer implicit. This doesn't go as far as to remove the `ArrayOps` (and `SliceOps`) trait(s) yet, but that should now be possible.
1 parent aaa466e commit ef954cb

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

src/lib.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -252,23 +252,22 @@ where
252252

253253
impl<T, U, const N: usize> AsRef<[T; N]> for Array<T, U>
254254
where
255-
Self: ArrayOps<T, N>,
256-
U: ArraySize,
255+
U: ArraySize<ArrayType<T> = [T; N]>,
257256
{
258257
#[inline]
259258
fn as_ref(&self) -> &[T; N] {
260-
self.as_core_array()
259+
&self.0
261260
}
262261
}
263262

264263
impl<T, U, const N: usize> AsRef<Array<T, U>> for [T; N]
265264
where
266-
Array<T, U>: ArrayOps<T, N>,
267-
U: ArraySize,
265+
U: ArraySize<ArrayType<T> = [T; N]>,
268266
{
269267
#[inline]
270268
fn as_ref(&self) -> &Array<T, U> {
271-
Array::ref_from_core_array(self)
269+
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]`
270+
unsafe { &*self.as_ptr().cast() }
272271
}
273272
}
274273

@@ -284,23 +283,22 @@ where
284283

285284
impl<T, U, const N: usize> AsMut<[T; N]> for Array<T, U>
286285
where
287-
Self: ArrayOps<T, N>,
288-
U: ArraySize,
286+
U: ArraySize<ArrayType<T> = [T; N]>,
289287
{
290288
#[inline]
291289
fn as_mut(&mut self) -> &mut [T; N] {
292-
self.as_mut_core_array()
290+
&mut self.0
293291
}
294292
}
295293

296294
impl<T, U, const N: usize> AsMut<Array<T, U>> for [T; N]
297295
where
298-
Array<T, U>: ArrayOps<T, N>,
299-
U: ArraySize,
296+
U: ArraySize<ArrayType<T> = [T; N]>,
300297
{
301298
#[inline]
302299
fn as_mut(&mut self) -> &mut Array<T, U> {
303-
Array::ref_mut_from_core_array(self)
300+
// SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]`
301+
unsafe { &mut *self.as_mut_ptr().cast() }
304302
}
305303
}
306304

@@ -316,12 +314,11 @@ where
316314

317315
impl<T, U, const N: usize> Borrow<[T; N]> for Array<T, U>
318316
where
319-
Self: ArrayOps<T, N>,
320-
U: ArraySize,
317+
U: ArraySize<ArrayType<T> = [T; N]>,
321318
{
322319
#[inline]
323320
fn borrow(&self) -> &[T; N] {
324-
self.as_core_array()
321+
&self.0
325322
}
326323
}
327324

@@ -337,12 +334,11 @@ where
337334

338335
impl<T, U, const N: usize> BorrowMut<[T; N]> for Array<T, U>
339336
where
340-
Self: ArrayOps<T, N>,
341-
U: ArraySize,
337+
U: ArraySize<ArrayType<T> = [T; N]>,
342338
{
343339
#[inline]
344340
fn borrow_mut(&mut self) -> &mut [T; N] {
345-
self.as_mut_core_array()
341+
&mut self.0
346342
}
347343
}
348344

@@ -437,45 +433,41 @@ where
437433

438434
impl<'a, T, U, const N: usize> From<&'a [T; N]> for &'a Array<T, U>
439435
where
440-
Array<T, U>: ArrayOps<T, N>,
441-
U: ArraySize,
436+
U: ArraySize<ArrayType<T> = [T; N]>,
442437
{
443438
#[inline]
444439
fn from(array_ref: &'a [T; N]) -> &'a Array<T, U> {
445-
<Array<T, U>>::ref_from_core_array(array_ref)
440+
array_ref.as_ref()
446441
}
447442
}
448443

449444
impl<'a, T, U, const N: usize> From<&'a Array<T, U>> for &'a [T; N]
450445
where
451-
Array<T, U>: ArrayOps<T, N>,
452-
U: ArraySize,
446+
U: ArraySize<ArrayType<T> = [T; N]>,
453447
{
454448
#[inline]
455449
fn from(array_ref: &'a Array<T, U>) -> &'a [T; N] {
456-
array_ref.as_core_array()
450+
array_ref.as_ref()
457451
}
458452
}
459453

460454
impl<'a, T, U, const N: usize> From<&'a mut [T; N]> for &'a mut Array<T, U>
461455
where
462-
Array<T, U>: ArrayOps<T, N>,
463-
U: ArraySize,
456+
U: ArraySize<ArrayType<T> = [T; N]>,
464457
{
465458
#[inline]
466459
fn from(array_ref: &'a mut [T; N]) -> &'a mut Array<T, U> {
467-
<Array<T, U>>::ref_mut_from_core_array(array_ref)
460+
array_ref.as_mut()
468461
}
469462
}
470463

471464
impl<'a, T, U, const N: usize> From<&'a mut Array<T, U>> for &'a mut [T; N]
472465
where
473-
Array<T, U>: ArrayOps<T, N>,
474-
U: ArraySize,
466+
U: ArraySize<ArrayType<T> = [T; N]>,
475467
{
476468
#[inline]
477469
fn from(array_ref: &'a mut Array<T, U>) -> &'a mut [T; N] {
478-
array_ref.as_mut_core_array()
470+
array_ref.as_mut()
479471
}
480472
}
481473

0 commit comments

Comments
 (0)