@@ -274,17 +274,21 @@ impl<K, V> Slice<K, V> {
274
274
indices : [ usize ; N ] ,
275
275
) -> Result < [ ( & K , & mut V ) ; N ] , GetDisjointMutError > {
276
276
let indices = indices. map ( Some ) ;
277
- let key_values = self . get_disjoint_opt_mut ( indices) ?;
277
+ let empty_tail = Self :: new_mut ( ) ;
278
+ let key_values = Self :: get_disjoint_opt_mut ( self , empty_tail, indices) ?;
278
279
Ok ( key_values. map ( Option :: unwrap) )
279
280
}
280
281
281
282
#[ allow( unsafe_code) ]
282
- pub ( crate ) fn get_disjoint_opt_mut < const N : usize > (
283
- & mut self ,
283
+ pub ( crate ) fn get_disjoint_opt_mut < ' a , const N : usize > (
284
+ head : & mut Self ,
285
+ tail : & mut Self ,
284
286
indices : [ Option < usize > ; N ] ,
285
- ) -> Result < [ Option < ( & K , & mut V ) > ; N ] , GetDisjointMutError > {
287
+ ) -> Result < [ Option < ( & ' a K , & ' a mut V ) > ; N ] , GetDisjointMutError > {
288
+ let mid = head. len ( ) ;
289
+ let len = mid + tail. len ( ) ;
290
+
286
291
// SAFETY: Can't allow duplicate indices as we would return several mutable refs to the same data.
287
- let len = self . len ( ) ;
288
292
for i in 0 ..N {
289
293
if let Some ( idx) = indices[ i] {
290
294
if idx >= len {
@@ -295,14 +299,20 @@ impl<K, V> Slice<K, V> {
295
299
}
296
300
}
297
301
298
- let entries_ptr = self . entries . as_mut_ptr ( ) ;
302
+ let head_ptr = head. entries . as_mut_ptr ( ) ;
303
+ let tail_ptr = tail. entries . as_mut_ptr ( ) ;
299
304
let out = indices. map ( |idx_opt| {
300
305
match idx_opt {
301
306
Some ( idx) => {
302
- // SAFETY: The base pointer is valid as it comes from a slice and the reference is always
307
+ // SAFETY: The base pointers are valid as they come from slices and the reference is always
303
308
// in-bounds & unique as we've already checked the indices above.
304
- let kv = unsafe { ( * ( entries_ptr. add ( idx) ) ) . ref_mut ( ) } ;
305
- Some ( kv)
309
+ unsafe {
310
+ let ptr = match idx. checked_sub ( mid) {
311
+ None => head_ptr. add ( idx) ,
312
+ Some ( tidx) => tail_ptr. add ( tidx) ,
313
+ } ;
314
+ Some ( ( * ptr) . ref_mut ( ) )
315
+ }
306
316
}
307
317
None => None ,
308
318
}
0 commit comments