diff options
| author | bors <bors@rust-lang.org> | 2014-06-13 22:46:35 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-06-13 22:46:35 +0000 |
| commit | 3851d68a27d41dfd15e12a75b590f362a0675870 (patch) | |
| tree | 5f8864590e55a30df81861a8b93b823cb01a0e74 /src/libcore | |
| parent | 63dcc9a4df50680686bee852e82a52fbc59b3c27 (diff) | |
| parent | 78053f08253de974ea6e8a6ee8bb1f4c082daeab (diff) | |
| download | rust-3851d68a27d41dfd15e12a75b590f362a0675870.tar.gz rust-3851d68a27d41dfd15e12a75b590f362a0675870.zip | |
auto merge of #14750 : bachm/rust/master, r=alexcrichton
This adds the missing `get_mut` method to the `MutableVector` trait, and implements it for `&'a mut [T]`.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/slice.rs | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index d579d044892..bb24d53458c 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -717,6 +717,9 @@ impl<'a, T: Ord> ImmutableOrdVector<T> for &'a [T] { /// Extension methods for vectors such that their elements are /// mutable. pub trait MutableVector<'a, T> { + /// Returns a mutable reference to the element at the given index, + /// or `None` if the index is out of bounds + fn get_mut(self, index: uint) -> Option<&'a mut T>; /// Work with `self` as a mut slice. /// Primarily intended for getting a &mut [T] from a [T, ..N]. fn as_mut_slice(self) -> &'a mut [T]; @@ -921,6 +924,11 @@ pub trait MutableVector<'a, T> { impl<'a,T> MutableVector<'a, T> for &'a mut [T] { #[inline] + fn get_mut(self, index: uint) -> Option<&'a mut T> { + if index < self.len() { Some(&mut self[index]) } else { None } + } + + #[inline] fn as_mut_slice(self) -> &'a mut [T] { self } fn mut_slice(self, start: uint, end: uint) -> &'a mut [T] { |
