diff options
| author | Dylan DPC <dylan.dpc@gmail.com> | 2020-01-06 12:00:17 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-06 12:00:17 +0530 |
| commit | 005d9d5b6e1dd6ccdfa40c1dfce5517622c92018 (patch) | |
| tree | 3a47da3e7add0b6bd3ff3490e453d365c371448e /src/liballoc | |
| parent | 36920750499f5ad03cd2c35dabf09094fba7b679 (diff) | |
| parent | e03d1c420435fd1924c72aca6d1d969da897b732 (diff) | |
| download | rust-005d9d5b6e1dd6ccdfa40c1dfce5517622c92018.tar.gz rust-005d9d5b6e1dd6ccdfa40c1dfce5517622c92018.zip | |
Rollup merge of #67873 - Dylan-DPC:feature/change-remove-to-partial, r=Amanieu
change remove to have a PartialEq bound Addresses [comment](https://github.com/rust-lang/rust/pull/67727#issuecomment-570660301). References #40062 r? @Amanieu
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/tests/lib.rs | 1 | ||||
| -rw-r--r-- | src/liballoc/tests/vec.rs | 15 | ||||
| -rw-r--r-- | src/liballoc/vec.rs | 7 |
3 files changed, 22 insertions, 1 deletions
diff --git a/src/liballoc/tests/lib.rs b/src/liballoc/tests/lib.rs index 3fdee8bbfdf..c1ae67a1a33 100644 --- a/src/liballoc/tests/lib.rs +++ b/src/liballoc/tests/lib.rs @@ -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}; diff --git a/src/liballoc/tests/vec.rs b/src/liballoc/tests/vec.rs index 19acc70c73c..2a9bfefc713 100644 --- a/src/liballoc/tests/vec.rs +++ b/src/liballoc/tests/vec.rs @@ -132,6 +132,21 @@ fn test_extend_ref() { } #[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]; { diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 93a51ccb207..a27a13847d6 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -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 @@ -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)) } |
