Skip to content

Commit

Permalink
[std::vec] Rename .remove_opt() to .remove(), drop the old .remove() …
Browse files Browse the repository at this point in the history
…behavior
  • Loading branch information
SimonSapin committed Jan 21, 2014
1 parent b5e6573 commit e75d0a9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/libextra/treemap.rs
Expand Up @@ -1164,7 +1164,7 @@ mod test_treemap {

30.times(|| {
let r = rng.gen_range(0, ctrl.len());
let (key, _) = ctrl.remove(r);
let (key, _) = ctrl.remove(r).unwrap();
assert!(map.remove(&key));
check_structure(&map);
check_equal(ctrl, &map);
Expand Down
2 changes: 1 addition & 1 deletion src/libgreen/basic.rs
Expand Up @@ -84,7 +84,7 @@ impl BasicLoop {
}
RemoveRemote(i) => {
match self.remotes.iter().position(|&(id, _)| id == i) {
Some(i) => { self.remotes.remove(i); }
Some(i) => { self.remotes.remove(i).unwrap(); }
None => unreachable!()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sync/deque.rs
Expand Up @@ -157,7 +157,7 @@ impl<T: Send> BufferPool<T> {
unsafe {
self.pool.with(|pool| {
match pool.iter().position(|x| x.size() >= (1 << bits)) {
Some(i) => pool.remove(i),
Some(i) => pool.remove(i).unwrap(),
None => ~Buffer::new(bits)
}
})
Expand Down
60 changes: 13 additions & 47 deletions src/libstd/vec.rs
Expand Up @@ -1399,18 +1399,14 @@ pub trait OwnedVector<T> {
/// # Example
/// ```rust
/// let mut v = ~[1, 2, 3];
/// assert_eq!(v.remove_opt(1), Some(2));
/// assert_eq!(v.remove(1), Some(2));
/// assert_eq!(v, ~[1, 3]);
///
/// assert_eq!(v.remove_opt(4), None);
/// assert_eq!(v.remove(4), None);
/// // v is unchanged:
/// assert_eq!(v, ~[1, 3]);
/// ```
fn remove_opt(&mut self, i: uint) -> Option<T>;

/// Remove and return the element at position i within v, shifting
/// all elements after position i one position to the left.
fn remove(&mut self, i: uint) -> T;
fn remove(&mut self, i: uint) -> Option<T>;

/**
* Remove an element from anywhere in the vector and return it, replacing it
Expand Down Expand Up @@ -1577,7 +1573,7 @@ impl<T> OwnedVector<T> for ~[T] {

#[inline]
fn shift(&mut self) -> Option<T> {
self.remove_opt(0)
self.remove(0)
}

#[inline]
Expand All @@ -1604,15 +1600,7 @@ impl<T> OwnedVector<T> for ~[T] {
}
}

#[inline]
fn remove(&mut self, i: uint) -> T {
match self.remove_opt(i) {
Some(t) => t,
None => fail!("remove: the len is {} but the index is {}", self.len(), i)
}
}

fn remove_opt(&mut self, i: uint) -> Option<T> {
fn remove(&mut self, i: uint) -> Option<T> {
let len = self.len();
if i < len {
unsafe { // infallible
Expand Down Expand Up @@ -3617,48 +3605,26 @@ mod tests {
}

#[test]
fn test_remove_opt() {
fn test_remove() {
let mut a = ~[1,2,3,4];

assert_eq!(a.remove_opt(2), Some(3));
assert_eq!(a.remove(2), Some(3));
assert_eq!(a, ~[1,2,4]);

assert_eq!(a.remove_opt(2), Some(4));
assert_eq!(a.remove(2), Some(4));
assert_eq!(a, ~[1,2]);

assert_eq!(a.remove_opt(2), None);
assert_eq!(a.remove(2), None);
assert_eq!(a, ~[1,2]);

assert_eq!(a.remove_opt(0), Some(1));
assert_eq!(a.remove(0), Some(1));
assert_eq!(a, ~[2]);

assert_eq!(a.remove_opt(0), Some(2));
assert_eq!(a.remove(0), Some(2));
assert_eq!(a, ~[]);

assert_eq!(a.remove_opt(0), None);
assert_eq!(a.remove_opt(10), None);
}

#[test]
fn test_remove() {
let mut a = ~[1, 2, 3, 4];
a.remove(2);
assert_eq!(a, ~[1, 2, 4]);

let mut a = ~[1, 2, 3];
a.remove(0);
assert_eq!(a, ~[2, 3]);

let mut a = ~[1];
a.remove(0);
assert_eq!(a, ~[]);
}

#[test]
#[should_fail]
fn test_remove_oob() {
let mut a = ~[1, 2, 3];
a.remove(3);
assert_eq!(a.remove(0), None);
assert_eq!(a.remove(10), None);
}

#[test]
Expand Down

0 comments on commit e75d0a9

Please sign in to comment.