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