Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve clone_from impls for collections #28602

Merged
merged 2 commits into from
Sep 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/libcollections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,22 @@ 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]
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
13 changes: 12 additions & 1 deletion src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -765,6 +765,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 {
Expand Down
12 changes: 4 additions & 8 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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..]);
}
}

Expand Down