@@ -365,6 +365,10 @@ where
365
365
/// Methods for volatile slices
366
366
#[ cfg( feature = "unstable" ) ]
367
367
impl < ' a , T , R , W > VolatilePtr < ' a , [ T ] , Access < R , W > > {
368
+ pub fn len ( & self ) -> usize {
369
+ self . pointer . len ( )
370
+ }
371
+
368
372
/// Applies the index operation on the wrapped slice.
369
373
///
370
374
/// Returns a shared `Volatile` reference to the resulting subslice.
@@ -401,17 +405,27 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
401
405
/// let subslice = volatile.index(1..);
402
406
/// assert_eq!(subslice.index(0).read(), 2);
403
407
/// ```
404
- pub fn index < I > ( & self , index : I ) -> VolatilePtr < I :: Output , Access < R , access:: NoAccess > >
408
+ pub fn index < I > (
409
+ & self ,
410
+ index : I ,
411
+ ) -> VolatilePtr < <I as SliceIndex < [ T ] > >:: Output , Access < R , access:: NoAccess > >
405
412
where
406
- I : SliceIndex < [ T ] > ,
413
+ I : SliceIndex < [ T ] > + SliceIndex < [ ( ) ] > + Clone ,
407
414
{
415
+ bounds_check ( self . pointer . len ( ) , index. clone ( ) ) ;
416
+
408
417
unsafe { self . map ( |slice| slice. get_unchecked_mut ( index) ) }
409
418
}
410
419
411
- pub fn index_mut < I > ( & mut self , index : I ) -> VolatilePtr < I :: Output , Access < R , W > >
420
+ pub fn index_mut < I > (
421
+ & mut self ,
422
+ index : I ,
423
+ ) -> VolatilePtr < <I as SliceIndex < [ T ] > >:: Output , Access < R , W > >
412
424
where
413
- I : SliceIndex < [ T ] > ,
425
+ I : SliceIndex < [ T ] > + SliceIndex < [ ( ) ] > + Clone ,
414
426
{
427
+ bounds_check ( self . pointer . len ( ) , index. clone ( ) ) ;
428
+
415
429
unsafe { self . map_mut ( |slice| slice. get_unchecked_mut ( index) ) }
416
430
}
417
431
@@ -913,3 +927,11 @@ where
913
927
. finish ( )
914
928
}
915
929
}
930
+
931
+ #[ cfg( feature = "unstable" ) ]
932
+ fn bounds_check ( len : usize , index : impl SliceIndex < [ ( ) ] > ) {
933
+ const MAX_ARRAY : [ ( ) ; usize:: MAX ] = [ ( ) ; usize:: MAX ] ;
934
+
935
+ let bound_check_slice = & MAX_ARRAY [ ..len] ;
936
+ & bound_check_slice[ index] ;
937
+ }
0 commit comments