Skip to content

Commit 005d9d5

Browse files
authored
Rollup merge of rust-lang#67873 - Dylan-DPC:feature/change-remove-to-partial, r=Amanieu
change remove to have a PartialEq bound Addresses [comment](rust-lang#67727 (comment)). References rust-lang#40062 r? @Amanieu
2 parents 3692075 + e03d1c4 commit 005d9d5

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/liballoc/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(associated_type_bounds)]
1212
#![feature(binary_heap_into_iter_sorted)]
1313
#![feature(binary_heap_drain_sorted)]
14+
#![feature(vec_remove_item)]
1415

1516
use std::collections::hash_map::DefaultHasher;
1617
use std::hash::{Hash, Hasher};

src/liballoc/tests/vec.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,21 @@ fn test_extend_ref() {
131131
assert_eq!(v, [1, 2, 3, 4, 5, 6, 7]);
132132
}
133133

134+
#[test]
135+
fn test_remove_item() {
136+
let mut v = vec![1, 2, 3];
137+
v.remove_item(&1);
138+
139+
assert_eq!(v.len(), 2);
140+
assert_eq!(v, [2, 3]);
141+
142+
let mut w = vec![1, 2, 3];
143+
w.remove_item(&4);
144+
145+
assert_eq!(w.len(), 3);
146+
w.remove_item(&4);
147+
}
148+
134149
#[test]
135150
fn test_slice_from_mut() {
136151
let mut values = vec![1, 2, 3, 4, 5];

src/liballoc/vec.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,9 @@ impl<T: PartialEq> Vec<T> {
16881688
pub fn dedup(&mut self) {
16891689
self.dedup_by(|a, b| a == b)
16901690
}
1691+
}
16911692

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

0 commit comments

Comments
 (0)