Skip to content

Commit eabda2e

Browse files
committed
Add documentation for slice index methods
1 parent a78fd04 commit eabda2e

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/lib.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,38 @@ impl<T, R, A> Volatile<R, A>
391391
where
392392
R: Deref<Target = [T]>,
393393
{
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+
/// ```
394426
pub fn index<'a, I>(&'a self, index: I) -> Volatile<&'a I::Output, A>
395427
where
396428
I: SliceIndex<[T]>,
@@ -399,6 +431,40 @@ where
399431
self.map(|slice| slice.index(index))
400432
}
401433

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+
/// ```
402468
pub fn index_mut<'a, I>(&'a mut self, index: I) -> Volatile<&mut I::Output, A>
403469
where
404470
I: SliceIndex<[T]>,

0 commit comments

Comments
 (0)