about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2020-04-07 14:46:53 +0200
committerGitHub <noreply@github.com>2020-04-07 14:46:53 +0200
commit795bc2ccff5672389c1e9d2481f25bdcdfee299b (patch)
tree102f79b2bf4261484fb3354568a648c92c2fc2ae /src/liballoc
parent39b62533c7f9d0581a6ea9b9fc2cc51f21c3b5b0 (diff)
parentf854070bb820501d88d1b029660bfde663595530 (diff)
downloadrust-795bc2ccff5672389c1e9d2481f25bdcdfee299b.tar.gz
rust-795bc2ccff5672389c1e9d2481f25bdcdfee299b.zip
Rollup merge of #70201 - cuviper:clone_into, r=dtolnay
Small tweaks in ToOwned::clone_into

- `<[T]>::clone_into` is slightly more optimized.
- `CStr::clone_into` is new, letting it reuse its allocation.
- `OsStr::clone_into` now forwards to the underlying slice/`Vec`.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/slice.rs12
1 files changed, 6 insertions, 6 deletions
diff --git a/src/liballoc/slice.rs b/src/liballoc/slice.rs
index a3d9c78b7f5..cd750d25580 100644
--- a/src/liballoc/slice.rs
+++ b/src/liballoc/slice.rs
@@ -733,14 +733,14 @@ impl<T: Clone> ToOwned for [T] {
     fn clone_into(&self, target: &mut Vec<T>) {
         // drop anything in target that will not be overwritten
         target.truncate(self.len());
-        let len = target.len();
-
-        // reuse the contained values' allocations/resources.
-        target.clone_from_slice(&self[..len]);
 
         // target.len <= self.len due to the truncate above, so the
-        // slice here is always in-bounds.
-        target.extend_from_slice(&self[len..]);
+        // slices here are always in-bounds.
+        let (init, tail) = self.split_at(target.len());
+
+        // reuse the contained values' allocations/resources.
+        target.clone_from_slice(init);
+        target.extend_from_slice(tail);
     }
 }