diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2014-02-23 10:56:38 +1100 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-02-24 21:22:26 -0800 |
| commit | 16e635cdfbb6b041886d1bccd28fa5e7e34c9f47 (patch) | |
| tree | eae398f604ca7c7798707ec1fd935fef79a0a493 /src | |
| parent | 3ca01676bcbd092b04608cc0eee843b7031e46cb (diff) | |
| download | rust-16e635cdfbb6b041886d1bccd28fa5e7e34c9f47.tar.gz rust-16e635cdfbb6b041886d1bccd28fa5e7e34c9f47.zip | |
std: make .swap_remove return Option<T>.
This is one of the last raw "indexing" method on vectors that returns `T` instead of the Option.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libgreen/stack.rs | 2 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 50 | ||||
| -rw-r--r-- | src/libstd/vec_ng.rs | 10 |
3 files changed, 35 insertions, 27 deletions
diff --git a/src/libgreen/stack.rs b/src/libgreen/stack.rs index 8a5e6be17c8..053d73c010e 100644 --- a/src/libgreen/stack.rs +++ b/src/libgreen/stack.rs @@ -139,7 +139,7 @@ impl StackPool { pub fn take_stack(&mut self, min_size: uint) -> Stack { // Ideally this would be a binary search match self.stacks.iter().position(|s| min_size <= s.min_size) { - Some(idx) => self.stacks.swap_remove(idx), + Some(idx) => self.stacks.swap_remove(idx).unwrap(), None => Stack::new(min_size) } } diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index d8cb8bf3ed1..cf49ea53562 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -1368,13 +1368,24 @@ pub trait OwnedVector<T> { /// ``` fn remove(&mut self, i: uint) -> Option<T>; - /** - * Remove an element from anywhere in the vector and return it, replacing it - * with the last element. This does not preserve ordering, but is O(1). - * - * Fails if index >= length. - */ - fn swap_remove(&mut self, index: uint) -> T; + /// Remove an element from anywhere in the vector and return it, replacing it + /// with the last element. This does not preserve ordering, but is O(1). + /// + /// Returns `None` if `index` is out of bounds. + /// + /// # Example + /// ```rust + /// let mut v = ~[~"foo", ~"bar", ~"baz", ~"qux"]; + /// + /// assert_eq!(v.swap_remove(1), Some(~"bar")); + /// assert_eq!(v, ~[~"foo", ~"qux", ~"baz"]); + /// + /// assert_eq!(v.swap_remove(0), Some(~"foo")); + /// assert_eq!(v, ~[~"baz", ~"qux"]); + /// + /// assert_eq!(v.swap_remove(2), None); + /// ``` + fn swap_remove(&mut self, index: uint) -> Option<T>; /// Shorten a vector, dropping excess elements. fn truncate(&mut self, newlen: uint); @@ -1580,15 +1591,14 @@ impl<T> OwnedVector<T> for ~[T] { None } } - fn swap_remove(&mut self, index: uint) -> T { + fn swap_remove(&mut self, index: uint) -> Option<T> { let ln = self.len(); - if index >= ln { - fail!("vec::swap_remove - index {} >= length {}", index, ln); - } if index < ln - 1 { self.swap(index, ln - 1); + } else if index >= ln { + return None } - self.pop().unwrap() + self.pop() } fn truncate(&mut self, newlen: uint) { let oldlen = self.len(); @@ -3194,15 +3204,15 @@ mod tests { fn test_swap_remove() { let mut v = ~[1, 2, 3, 4, 5]; let mut e = v.swap_remove(0); - assert_eq!(v.len(), 4); - assert_eq!(e, 1); - assert_eq!(v[0], 5); + assert_eq!(e, Some(1)); + assert_eq!(v, ~[5, 2, 3, 4]); e = v.swap_remove(3); - assert_eq!(v.len(), 3); - assert_eq!(e, 4); - assert_eq!(v[0], 5); - assert_eq!(v[1], 2); - assert_eq!(v[2], 3); + assert_eq!(e, Some(4)); + assert_eq!(v, ~[5, 2, 3]); + + e = v.swap_remove(3); + assert_eq!(e, None); + assert_eq!(v, ~[5, 2, 3]); } #[test] diff --git a/src/libstd/vec_ng.rs b/src/libstd/vec_ng.rs index 3532e7b26a4..2f39adc25d3 100644 --- a/src/libstd/vec_ng.rs +++ b/src/libstd/vec_ng.rs @@ -277,15 +277,14 @@ impl<T> Vec<T> { } #[inline] - pub fn swap_remove(&mut self, index: uint) -> T { + pub fn swap_remove(&mut self, index: uint) -> Option<T> { let length = self.len(); - if index >= length { - fail!("Vec::swap_remove - index {} >= length {}", index, length); - } if index < length - 1 { self.as_mut_slice().swap(index, length - 1); + } else if index >= length { + return None } - self.pop().unwrap() + self.pop() } #[inline] @@ -392,4 +391,3 @@ impl<T> Drop for MoveItems<T> { } } } - |
