From 0e2427cd3c52f251d135b7f8c0d73f93369b6e7e Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Tue, 5 Sep 2017 15:32:02 -0600 Subject: [PATCH 1/4] Remove unneeded copy --- src/liballoc/vec.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 8141851b8c9..158331af2ae 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -2691,7 +2691,8 @@ impl<'a, T, F> Iterator for DrainFilter<'a, T, F> self.del += 1; return Some(ptr::read(&v[i])); } else if self.del > 0 { - v.swap(i - self.del, i); + let del = self.del; + ptr::copy_nonoverlapping(self.vec.as_ptr().offset(i), self.vec.as_mut_ptr().offset(i - del), 1); } } None From 4817754677826b07af9035133f8433b94c364ff7 Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Tue, 5 Sep 2017 15:38:45 -0600 Subject: [PATCH 2/4] Fix ownership issues --- src/liballoc/vec.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 158331af2ae..64e84063b7a 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -2692,7 +2692,9 @@ impl<'a, T, F> Iterator for DrainFilter<'a, T, F> return Some(ptr::read(&v[i])); } else if self.del > 0 { let del = self.del; - ptr::copy_nonoverlapping(self.vec.as_ptr().offset(i), self.vec.as_mut_ptr().offset(i - del), 1); + let src = self.vec.as_ptr().offset(i); + let dst = self.vec.as_mut_ptr().offset(i - del); + ptr::copy_nonoverlapping(src, dst, 1); } } None From 4de0cf122d342fa6b1eb6eb30ae8a04d1d55353d Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Tue, 5 Sep 2017 23:05:22 -0600 Subject: [PATCH 3/4] Try using ref to raw conversion --- src/liballoc/vec.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 64e84063b7a..cc0a60e747e 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -2692,8 +2692,8 @@ impl<'a, T, F> Iterator for DrainFilter<'a, T, F> return Some(ptr::read(&v[i])); } else if self.del > 0 { let del = self.del; - let src = self.vec.as_ptr().offset(i); - let dst = self.vec.as_mut_ptr().offset(i - del); + let src: *const T = &v[i]; + let dst: *mut T = &mut v[i - del]; ptr::copy_nonoverlapping(src, dst, 1); } } From 10384ab18a386e6f7b58c714def62e405182d968 Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Mon, 18 Sep 2017 10:15:17 -0600 Subject: [PATCH 4/4] Add requested comment --- src/liballoc/vec.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index cc0a60e747e..c5aaaac8f76 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -2694,6 +2694,9 @@ impl<'a, T, F> Iterator for DrainFilter<'a, T, F> let del = self.del; let src: *const T = &v[i]; let dst: *mut T = &mut v[i - del]; + // This is safe because self.vec has length 0 + // thus its elements will not have Drop::drop + // called on them in the event of a panic. ptr::copy_nonoverlapping(src, dst, 1); } }