@@ -121,11 +121,16 @@ extern crate std as alloc;
121
121
#[ cfg( feature = "serde" ) ]
122
122
mod serde;
123
123
124
+ #[ cfg( not( slab_no_const_generics) ) ]
125
+ mod get_disjoint_mut;
126
+
127
+ #[ cfg( not( slab_no_const_generics) ) ]
128
+ pub use get_disjoint_mut:: GetDisjointMutError ;
129
+
124
130
mod builder;
125
131
126
132
use alloc:: vec:: { self , Vec } ;
127
133
use core:: iter:: { self , FromIterator , FusedIterator } ;
128
- use core:: mem:: MaybeUninit ;
129
134
use core:: { fmt, mem, ops, slice} ;
130
135
131
136
/// Pre-allocated storage for a uniform data type
@@ -237,19 +242,6 @@ enum Entry<T> {
237
242
Occupied ( T ) ,
238
243
}
239
244
240
- #[ derive( Debug , Clone , PartialEq , Eq ) ]
241
- /// The error type returned by [`Slab::get_disjoint_mut`].
242
- pub enum GetDisjointMutError {
243
- /// An index provided was not associated with a value.
244
- IndexVacant ,
245
-
246
- /// An index provided was out-of-bounds for the slab.
247
- IndexOutOfBounds ,
248
-
249
- /// Two indices provided were overlapping.
250
- OverlappingIndices ,
251
- }
252
-
253
245
impl < T > Slab < T > {
254
246
/// Construct a new, empty `Slab`.
255
247
///
@@ -807,50 +799,6 @@ impl<T> Slab<T> {
807
799
}
808
800
}
809
801
810
- /// Returns mutable references to many indices at once.
811
- /// Returns [`GetDisjointMutError`] if the indices are out of bounds,
812
- /// overlapping, or vacant.
813
- #[ cfg( not( slab_no_const_generics) ) ]
814
- pub fn get_disjoint_mut < const N : usize > (
815
- & mut self ,
816
- keys : [ usize ; N ] ,
817
- ) -> Result < [ & mut T ; N ] , GetDisjointMutError > {
818
- let entries_cap = self . entries . capacity ( ) ;
819
-
820
- // NB: The optimizer should inline the loops into a sequence
821
- // of instructions without additional branching.
822
- for ( i, & key) in keys. iter ( ) . enumerate ( ) {
823
- if key >= entries_cap {
824
- return Err ( GetDisjointMutError :: IndexOutOfBounds ) ;
825
- }
826
- for & prev_key in & keys[ ..i] {
827
- if key == prev_key {
828
- return Err ( GetDisjointMutError :: OverlappingIndices ) ;
829
- }
830
- }
831
- }
832
-
833
- let mut res = MaybeUninit :: < [ & mut T ; N ] > :: uninit ( ) ;
834
- let res_ptr = res. as_mut_ptr ( ) as * mut & mut T ;
835
-
836
- let entries_ptr = self . entries . as_mut_ptr ( ) ;
837
-
838
- for ( i, & key) in keys. iter ( ) . enumerate ( ) {
839
- // SAFETY: we made sure above that this key is in bounds.
840
- match unsafe { & mut * entries_ptr. add ( key) } {
841
- Entry :: Vacant ( _) => return Err ( GetDisjointMutError :: IndexVacant ) ,
842
- Entry :: Occupied ( entry) => {
843
- // SAFETY: `res` and `keys` both have N elements so `i` must be in bounds.
844
- // We checked above that all selected `entry`s are distinct.
845
- unsafe { res_ptr. add ( i) . write ( entry) } ;
846
- }
847
- }
848
- }
849
- // SAFETY: the loop above only terminates successfully if it initialized
850
- // all elements of this array.
851
- Ok ( unsafe { res. assume_init ( ) } )
852
- }
853
-
854
802
/// Return a reference to the value associated with the given key without
855
803
/// performing bounds checking.
856
804
///
0 commit comments