diff options
| author | bors <bors@rust-lang.org> | 2015-09-24 14:19:20 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-09-24 14:19:20 +0000 |
| commit | 355bbfb8955ffdfb2bb7940ca1b38f2bbb5defd4 (patch) | |
| tree | 5ac9ad615163f8fddb3d0b2c4becdbd52069f39d | |
| parent | 8fe79bdfdacb2f5914971bd1a0b63b9577afbf6a (diff) | |
| parent | 97f2a325644eb88b50fb5b52e09933535fa29c40 (diff) | |
| download | rust-355bbfb8955ffdfb2bb7940ca1b38f2bbb5defd4.tar.gz rust-355bbfb8955ffdfb2bb7940ca1b38f2bbb5defd4.zip | |
Auto merge of #28602 - apasel422:clone_from, r=bluss
r? @bluss
| -rw-r--r-- | src/libcollections/binary_heap.rs | 12 | ||||
| -rw-r--r-- | src/libcollections/lib.rs | 2 | ||||
| -rw-r--r-- | src/libcollections/string.rs | 13 | ||||
| -rw-r--r-- | src/libcollections/vec.rs | 12 |
4 files changed, 28 insertions, 11 deletions
diff --git a/src/libcollections/binary_heap.rs b/src/libcollections/binary_heap.rs index a3e32b59b71..b7afe968577 100644 --- a/src/libcollections/binary_heap.rs +++ b/src/libcollections/binary_heap.rs @@ -167,13 +167,23 @@ use vec::{self, Vec}; /// item's ordering relative to any other item, as determined by the `Ord` /// trait, changes while it is in the heap. This is normally only possible /// through `Cell`, `RefCell`, global state, I/O, or unsafe code. -#[derive(Clone)] #[stable(feature = "rust1", since = "1.0.0")] pub struct BinaryHeap<T> { data: Vec<T>, } #[stable(feature = "rust1", since = "1.0.0")] +impl<T: Clone> Clone for BinaryHeap<T> { + fn clone(&self) -> Self { + BinaryHeap { data: self.data.clone() } + } + + fn clone_from(&mut self, source: &Self) { + self.data.clone_from(&source.data); + } +} + +#[stable(feature = "rust1", since = "1.0.0")] impl<T: Ord> Default for BinaryHeap<T> { #[inline] fn default() -> BinaryHeap<T> { BinaryHeap::new() } diff --git a/src/libcollections/lib.rs b/src/libcollections/lib.rs index 34bd1345fcb..03ee8ba31b1 100644 --- a/src/libcollections/lib.rs +++ b/src/libcollections/lib.rs @@ -62,7 +62,7 @@ #![feature(unsafe_no_drop_flag, filling_drop)] #![feature(decode_utf16)] #![feature(utf8_error)] -#![cfg_attr(test, feature(rand, test))] +#![cfg_attr(test, feature(clone_from_slice, rand, test))] #![feature(no_std)] #![no_std] diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index bb65d7469ab..ba921fed68b 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -30,7 +30,7 @@ use vec::Vec; use boxed::Box; /// A growable string stored as a UTF-8 encoded buffer. -#[derive(Clone, PartialOrd, Eq, Ord)] +#[derive(PartialOrd, Eq, Ord)] #[stable(feature = "rust1", since = "1.0.0")] pub struct String { vec: Vec<u8>, @@ -766,6 +766,17 @@ impl fmt::Display for FromUtf16Error { } #[stable(feature = "rust1", since = "1.0.0")] +impl Clone for String { + fn clone(&self) -> Self { + String { vec: self.vec.clone() } + } + + fn clone_from(&mut self, source: &Self) { + self.vec.clone_from(&source.vec); + } +} + +#[stable(feature = "rust1", since = "1.0.0")] impl FromIterator<char> for String { fn from_iter<I: IntoIterator<Item=char>>(iterable: I) -> String { let mut buf = String::new(); diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 4110faa41b3..de3e6f94e87 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1007,19 +1007,15 @@ impl<T:Clone> Clone for Vec<T> { fn clone_from(&mut self, other: &Vec<T>) { // drop anything in self that will not be overwritten - if self.len() > other.len() { - self.truncate(other.len()) - } + self.truncate(other.len()); + let len = self.len(); // reuse the contained values' allocations/resources. - for (place, thing) in self.iter_mut().zip(other) { - place.clone_from(thing) - } + self.clone_from_slice(&other[..len]); // self.len <= other.len due to the truncate above, so the // slice here is always in-bounds. - let slice = &other[self.len()..]; - self.push_all(slice); + self.push_all(&other[len..]); } } |
