Skip to content

Commit 1fc0dc1

Browse files
committed
Update array::split_array_*() methods to never panic
This turns invalid split indices into post-mono errors. In the future, these will return e.g. (&[T; M], &[T; N-M]) so that an invalid index becomes a type error.
1 parent 8510cf6 commit 1fc0dc1

File tree

2 files changed

+8
-48
lines changed

2 files changed

+8
-48
lines changed

library/core/src/array/mod.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -631,10 +631,6 @@ impl<T, const N: usize> [T; N] {
631631
/// the index `M` itself) and the second will contain all
632632
/// indices from `[M, N)` (excluding the index `N` itself).
633633
///
634-
/// # Panics
635-
///
636-
/// Panics if `M > N`.
637-
///
638634
/// # Examples
639635
///
640636
/// ```
@@ -667,6 +663,8 @@ impl<T, const N: usize> [T; N] {
667663
)]
668664
#[inline]
669665
pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T]) {
666+
// FIXME: remove once constraint is encoded in return type
667+
const { assert!(M <= N) }
670668
(&self[..]).split_array_ref::<M>().unwrap()
671669
}
672670

@@ -676,10 +674,6 @@ impl<T, const N: usize> [T; N] {
676674
/// the index `M` itself) and the second will contain all
677675
/// indices from `[M, N)` (excluding the index `N` itself).
678676
///
679-
/// # Panics
680-
///
681-
/// Panics if `M > N`.
682-
///
683677
/// # Examples
684678
///
685679
/// ```
@@ -700,6 +694,8 @@ impl<T, const N: usize> [T; N] {
700694
)]
701695
#[inline]
702696
pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T]) {
697+
// FIXME: remove once constraint is encoded in return type
698+
const { assert!(M <= N) }
703699
(&mut self[..]).split_array_mut::<M>().unwrap()
704700
}
705701

@@ -709,10 +705,6 @@ impl<T, const N: usize> [T; N] {
709705
/// the index `N - M` itself) and the second will contain all
710706
/// indices from `[N - M, N)` (excluding the index `N` itself).
711707
///
712-
/// # Panics
713-
///
714-
/// Panics if `M > N`.
715-
///
716708
/// # Examples
717709
///
718710
/// ```
@@ -745,6 +737,8 @@ impl<T, const N: usize> [T; N] {
745737
)]
746738
#[inline]
747739
pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M]) {
740+
// FIXME: remove once constraint is encoded in return type
741+
const { assert!(M <= N) }
748742
(&self[..]).rsplit_array_ref::<M>().unwrap()
749743
}
750744

@@ -754,10 +748,6 @@ impl<T, const N: usize> [T; N] {
754748
/// the index `N - M` itself) and the second will contain all
755749
/// indices from `[N - M, N)` (excluding the index `N` itself).
756750
///
757-
/// # Panics
758-
///
759-
/// Panics if `M > N`.
760-
///
761751
/// # Examples
762752
///
763753
/// ```
@@ -778,6 +768,8 @@ impl<T, const N: usize> [T; N] {
778768
)]
779769
#[inline]
780770
pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M]) {
771+
// FIXME: remove once constraint is encoded in return type
772+
const { assert!(M <= N) }
781773
(&mut self[..]).rsplit_array_mut::<M>().unwrap()
782774
}
783775
}

library/core/tests/array.rs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -488,38 +488,6 @@ fn array_rsplit_array_mut() {
488488
}
489489
}
490490

491-
#[should_panic]
492-
#[test]
493-
fn array_split_array_ref_out_of_bounds() {
494-
let v = [1, 2, 3, 4, 5, 6];
495-
496-
v.split_array_ref::<7>();
497-
}
498-
499-
#[should_panic]
500-
#[test]
501-
fn array_split_array_mut_out_of_bounds() {
502-
let mut v = [1, 2, 3, 4, 5, 6];
503-
504-
v.split_array_mut::<7>();
505-
}
506-
507-
#[should_panic]
508-
#[test]
509-
fn array_rsplit_array_ref_out_of_bounds() {
510-
let v = [1, 2, 3, 4, 5, 6];
511-
512-
v.rsplit_array_ref::<7>();
513-
}
514-
515-
#[should_panic]
516-
#[test]
517-
fn array_rsplit_array_mut_out_of_bounds() {
518-
let mut v = [1, 2, 3, 4, 5, 6];
519-
520-
v.rsplit_array_mut::<7>();
521-
}
522-
523491
#[test]
524492
fn array_intoiter_advance_by() {
525493
use std::cell::Cell;

0 commit comments

Comments
 (0)