@@ -391,6 +391,38 @@ impl<T, R, A> Volatile<R, A>
391
391
where
392
392
R : Deref < Target = [ T ] > ,
393
393
{
394
+ /// Applies the index operation on the wrapped slice.
395
+ ///
396
+ /// Returns a shared `Volatile` reference to the resulting subslice.
397
+ ///
398
+ /// This is a convenience method for the `map(|slice| slice.index(index))` operation, so it
399
+ /// has the same behavior as the indexing operation on slice (e.g. panic if index is
400
+ /// out-of-bounds).
401
+ ///
402
+ /// ## Examples
403
+ ///
404
+ /// Accessing a single slice element:
405
+ ///
406
+ /// ```
407
+ /// use volatile::Volatile;
408
+ ///
409
+ /// let array = [1, 2, 3];
410
+ /// let slice = &array[..];
411
+ /// let volatile = Volatile::new(slice);
412
+ /// assert_eq!(volatile.index(1).read(), 2);
413
+ /// ```
414
+ ///
415
+ /// Accessing a subslice:
416
+ ///
417
+ /// ```
418
+ /// use volatile::Volatile;
419
+ ///
420
+ /// let array = [1, 2, 3];
421
+ /// let slice = &array[..];
422
+ /// let volatile = Volatile::new(slice);
423
+ /// let subslice = volatile.index(1..);
424
+ /// assert_eq!(subslice.index(0).read(), 2);
425
+ /// ```
394
426
pub fn index < ' a , I > ( & ' a self , index : I ) -> Volatile < & ' a I :: Output , A >
395
427
where
396
428
I : SliceIndex < [ T ] > ,
@@ -399,6 +431,40 @@ where
399
431
self . map ( |slice| slice. index ( index) )
400
432
}
401
433
434
+ /// Applies the mutable index operation on the wrapped slice.
435
+ ///
436
+ /// Returns a mutable `Volatile` reference to the resulting subslice.
437
+ ///
438
+ /// This is a convenience method for the `map_mut(|slice| slice.index_mut(index))`
439
+ /// operation, so it has the same behavior as the indexing operation on slice
440
+ /// (e.g. panic if index is out-of-bounds).
441
+ ///
442
+ /// ## Examples
443
+ ///
444
+ /// Accessing a single slice element:
445
+ ///
446
+ /// ```
447
+ /// use volatile::Volatile;
448
+ ///
449
+ /// let mut array = [1, 2, 3];
450
+ /// let slice = &mut array[..];
451
+ /// let mut volatile = Volatile::new(slice);
452
+ /// volatile.index_mut(1).write(6);
453
+ /// assert_eq!(volatile.index(1).read(), 6);
454
+ /// ```
455
+ ///
456
+ /// Accessing a subslice:
457
+ ///
458
+ /// ```
459
+ /// use volatile::Volatile;
460
+ ///
461
+ /// let mut array = [1, 2, 3];
462
+ /// let slice = &mut array[..];
463
+ /// let mut volatile = Volatile::new(slice);
464
+ /// let mut subslice = volatile.index_mut(1..);
465
+ /// subslice.index_mut(0).write(6);
466
+ /// assert_eq!(subslice.index(0).read(), 6);
467
+ /// ```
402
468
pub fn index_mut < ' a , I > ( & ' a mut self , index : I ) -> Volatile < & mut I :: Output , A >
403
469
where
404
470
I : SliceIndex < [ T ] > ,
0 commit comments