From c09dac10738791460ff4897f5b3d12b62b2be9cc Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Sat, 4 Jan 2020 23:31:32 +0530 Subject: [PATCH 1/5] add partial eq bound to remove_item --- src/liballoc/vec.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 93a51ccb20737..0825dc228d823 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -1688,6 +1688,9 @@ impl Vec { pub fn dedup(&mut self) { self.dedup_by(|a, b| a == b) } +} + +impl Vec { /// Removes the first instance of `item` from the vector if the item exists. /// @@ -1701,11 +1704,15 @@ impl Vec { /// /// 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 { + pub fn remove_item(&mut self, item: &V) -> Option + where T: PartialEq + { let pos = self.iter().position(|x| *x == *item)?; Some(self.remove(pos)) } + } //////////////////////////////////////////////////////////////////////////////// From eb36688a01f0a51db53f6f830472f03836e4cf68 Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Sat, 4 Jan 2020 23:41:17 +0530 Subject: [PATCH 2/5] add tests --- src/liballoc/tests/vec.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/liballoc/tests/vec.rs b/src/liballoc/tests/vec.rs index 19acc70c73c8e..bf4a31c72dd43 100644 --- a/src/liballoc/tests/vec.rs +++ b/src/liballoc/tests/vec.rs @@ -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]; From f744ea03b46e860decb8672dc032805cbf9df652 Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Sat, 4 Jan 2020 23:57:34 +0530 Subject: [PATCH 3/5] ef em ti ... :P --- src/liballoc/tests/vec.rs | 6 +++--- src/liballoc/vec.rs | 7 +++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/liballoc/tests/vec.rs b/src/liballoc/tests/vec.rs index bf4a31c72dd43..2a9bfefc713e7 100644 --- a/src/liballoc/tests/vec.rs +++ b/src/liballoc/tests/vec.rs @@ -133,13 +133,13 @@ fn test_extend_ref() { #[test] fn test_remove_item() { - let mut v = vec![1,2,3]; + let mut v = vec![1, 2, 3]; v.remove_item(&1); assert_eq!(v.len(), 2); - assert_eq!(v, [2,3]); + assert_eq!(v, [2, 3]); - let mut w = vec![1,2,3]; + let mut w = vec![1, 2, 3]; w.remove_item(&4); assert_eq!(w.len(), 3); diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 0825dc228d823..9b48c21728adc 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -1690,8 +1690,7 @@ impl Vec { } } -impl Vec { - +impl Vec { /// Removes the first instance of `item` from the vector if the item exists. /// /// # Examples @@ -1707,12 +1706,12 @@ impl Vec { #[unstable(feature = "vec_remove_item", reason = "recently added", issue = "40062")] pub fn remove_item(&mut self, item: &V) -> Option - where T: PartialEq + where + T: PartialEq, { let pos = self.iter().position(|x| *x == *item)?; Some(self.remove(pos)) } - } //////////////////////////////////////////////////////////////////////////////// From 358b8983f2aae3dc8d2d08189eea8fd41898bb41 Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Sun, 5 Jan 2020 00:00:40 +0530 Subject: [PATCH 4/5] removed blank line --- src/liballoc/vec.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 9b48c21728adc..a27a13847d6a2 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -1703,7 +1703,6 @@ impl Vec { /// /// assert_eq!(vec, vec![2, 3, 1]); /// ``` - #[unstable(feature = "vec_remove_item", reason = "recently added", issue = "40062")] pub fn remove_item(&mut self, item: &V) -> Option where From e03d1c420435fd1924c72aca6d1d969da897b732 Mon Sep 17 00:00:00 2001 From: dylan_DPC Date: Sun, 5 Jan 2020 15:42:35 +0530 Subject: [PATCH 5/5] add feature gate --- src/liballoc/tests/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/liballoc/tests/lib.rs b/src/liballoc/tests/lib.rs index 3fdee8bbfdf10..c1ae67a1a339f 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};