Skip to content

Commit

Permalink
Rollup merge of rust-lang#67873 - Dylan-DPC:feature/change-remove-to-…
Browse files Browse the repository at this point in the history
…partial, r=Amanieu

change remove to have a PartialEq bound

Addresses [comment](rust-lang#67727 (comment)).

References rust-lang#40062

r? @Amanieu
  • Loading branch information
Dylan-DPC authored Jan 6, 2020
2 parents 3692075 + e03d1c4 commit 005d9d5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/liballoc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#![feature(associated_type_bounds)]
#![feature(binary_heap_into_iter_sorted)]
#![feature(binary_heap_drain_sorted)]
#![feature(vec_remove_item)]

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
Expand Down
15 changes: 15 additions & 0 deletions src/liballoc/tests/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ fn test_extend_ref() {
assert_eq!(v, [1, 2, 3, 4, 5, 6, 7]);
}

#[test]
fn test_remove_item() {
let mut v = vec![1, 2, 3];
v.remove_item(&1);

assert_eq!(v.len(), 2);
assert_eq!(v, [2, 3]);

let mut w = vec![1, 2, 3];
w.remove_item(&4);

assert_eq!(w.len(), 3);
w.remove_item(&4);
}

#[test]
fn test_slice_from_mut() {
let mut values = vec![1, 2, 3, 4, 5];
Expand Down
7 changes: 6 additions & 1 deletion src/liballoc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1688,7 +1688,9 @@ impl<T: PartialEq> Vec<T> {
pub fn dedup(&mut self) {
self.dedup_by(|a, b| a == b)
}
}

impl<T> Vec<T> {
/// Removes the first instance of `item` from the vector if the item exists.
///
/// # Examples
Expand All @@ -1702,7 +1704,10 @@ impl<T: PartialEq> Vec<T> {
/// assert_eq!(vec, vec![2, 3, 1]);
/// ```
#[unstable(feature = "vec_remove_item", reason = "recently added", issue = "40062")]
pub fn remove_item(&mut self, item: &T) -> Option<T> {
pub fn remove_item<V>(&mut self, item: &V) -> Option<T>
where
T: PartialEq<V>,
{
let pos = self.iter().position(|x| *x == *item)?;
Some(self.remove(pos))
}
Expand Down

0 comments on commit 005d9d5

Please sign in to comment.