@@ -4629,13 +4629,11 @@ impl<T> [T] {
4629
4629
pub fn get_many_mut < I , const N : usize > (
4630
4630
& mut self ,
4631
4631
indices : [ I ; N ] ,
4632
- ) -> Result < [ & mut I :: Output ; N ] , GetManyMutError < N > >
4632
+ ) -> Result < [ & mut I :: Output ; N ] , GetManyMutError >
4633
4633
where
4634
4634
I : GetManyMutIndex + SliceIndex < Self > ,
4635
4635
{
4636
- if !get_many_check_valid ( & indices, self . len ( ) ) {
4637
- return Err ( GetManyMutError { _private : ( ) } ) ;
4638
- }
4636
+ get_many_check_valid ( & indices, self . len ( ) ) ?;
4639
4637
// SAFETY: The `get_many_check_valid()` call checked that all indices
4640
4638
// are disjunct and in bounds.
4641
4639
unsafe { Ok ( self . get_many_unchecked_mut ( indices) ) }
@@ -4978,53 +4976,59 @@ impl<T, const N: usize> SlicePattern for [T; N] {
4978
4976
/// This will do `binomial(N + 1, 2) = N * (N + 1) / 2 = 0, 1, 3, 6, 10, ..`
4979
4977
/// comparison operations.
4980
4978
#[ inline]
4981
- fn get_many_check_valid < I : GetManyMutIndex , const N : usize > ( indices : & [ I ; N ] , len : usize ) -> bool {
4979
+ fn get_many_check_valid < I : GetManyMutIndex , const N : usize > (
4980
+ indices : & [ I ; N ] ,
4981
+ len : usize ,
4982
+ ) -> Result < ( ) , GetManyMutError > {
4982
4983
// NB: The optimizer should inline the loops into a sequence
4983
4984
// of instructions without additional branching.
4984
- let mut valid = true ;
4985
4985
for ( i, idx) in indices. iter ( ) . enumerate ( ) {
4986
- valid &= idx. is_in_bounds ( len) ;
4986
+ if !idx. is_in_bounds ( len) {
4987
+ return Err ( GetManyMutError :: IndexOutOfBounds ) ;
4988
+ }
4987
4989
for idx2 in & indices[ ..i] {
4988
- valid &= !idx. is_overlapping ( idx2) ;
4990
+ if idx. is_overlapping ( idx2) {
4991
+ return Err ( GetManyMutError :: OverlappingIndices ) ;
4992
+ }
4989
4993
}
4990
4994
}
4991
- valid
4995
+ Ok ( ( ) )
4992
4996
}
4993
4997
4994
- /// The error type returned by [`get_many_mut<N> `][`slice::get_many_mut`].
4998
+ /// The error type returned by [`get_many_mut`][`slice::get_many_mut`].
4995
4999
///
4996
5000
/// It indicates one of two possible errors:
4997
5001
/// - An index is out-of-bounds.
4998
- /// - The same index appeared multiple times in the array.
5002
+ /// - The same index appeared multiple times in the array
5003
+ /// (or different but overlapping indices when ranges are provided).
4999
5004
///
5000
5005
/// # Examples
5001
5006
///
5002
5007
/// ```
5003
5008
/// #![feature(get_many_mut)]
5009
+ /// use std::slice::GetManyMutError;
5004
5010
///
5005
5011
/// let v = &mut [1, 2, 3];
5006
- /// assert !(v.get_many_mut([0, 999]).is_err( ));
5007
- /// assert !(v.get_many_mut([1, 1]).is_err( ));
5012
+ /// assert_eq !(v.get_many_mut([0, 999]), Err(GetManyMutError::IndexOutOfBounds ));
5013
+ /// assert_eq !(v.get_many_mut([1, 1]), Err(GetManyMutError::OverlappingIndices ));
5008
5014
/// ```
5009
5015
#[ unstable( feature = "get_many_mut" , issue = "104642" ) ]
5010
- // NB: The N here is there to be forward-compatible with adding more details
5011
- // to the error type at a later point
5012
- #[ derive( Clone , PartialEq , Eq ) ]
5013
- pub struct GetManyMutError < const N : usize > {
5014
- _private : ( ) ,
5016
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
5017
+ pub enum GetManyMutError {
5018
+ /// An index provided was out-of-bounds for the slice.
5019
+ IndexOutOfBounds ,
5020
+ /// Two indices provided were overlapping.
5021
+ OverlappingIndices ,
5015
5022
}
5016
5023
5017
5024
#[ unstable( feature = "get_many_mut" , issue = "104642" ) ]
5018
- impl < const N : usize > fmt:: Debug for GetManyMutError < N > {
5025
+ impl fmt:: Display for GetManyMutError {
5019
5026
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
5020
- f. debug_struct ( "GetManyMutError" ) . finish_non_exhaustive ( )
5021
- }
5022
- }
5023
-
5024
- #[ unstable( feature = "get_many_mut" , issue = "104642" ) ]
5025
- impl < const N : usize > fmt:: Display for GetManyMutError < N > {
5026
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
5027
- fmt:: Display :: fmt ( "an index is out of bounds or appeared multiple times in the array" , f)
5027
+ let msg = match self {
5028
+ GetManyMutError :: IndexOutOfBounds => "an index is out of bounds" ,
5029
+ GetManyMutError :: OverlappingIndices => "there were overlapping indices" ,
5030
+ } ;
5031
+ fmt:: Display :: fmt ( msg, f)
5028
5032
}
5029
5033
}
5030
5034
0 commit comments