Skip to content

add clone_from and deep_clone_from #10376

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

Closed
wants to merge 2 commits into from
Closed
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
50 changes: 48 additions & 2 deletions src/libstd/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,27 @@ pub trait Clone {
/// are copied to maintain uniqueness, while the contents of
/// managed pointers are not copied.
fn clone(&self) -> Self;

/// Perform copy-assignment from `source`.
///
/// `a.clone_from(&b)` is equivalent to `a = b.clone()` in functionality,
/// but can be overriden to reuse the resources of `a` to avoid unnecessary
/// allocations.
#[inline(always)]
fn clone_from(&mut self, source: &Self) {
*self = source.clone()
}
}

impl<T: Clone> Clone for ~T {
/// Return a deep copy of the owned box.
/// Return a copy of the owned box.
#[inline]
fn clone(&self) -> ~T { ~(**self).clone() }

/// Perform copy-assignment from `source` by reusing the existing allocation.
fn clone_from(&mut self, source: &~T) {
**self = (**source).clone()
}
}

impl<T> Clone for @T {
Expand Down Expand Up @@ -118,16 +133,31 @@ extern_fn_clone!(A, B, C, D, E, F, G, H)

/// A trait distinct from `Clone` which represents "deep copies" of things like
/// managed boxes which would otherwise not be copied.
pub trait DeepClone {
pub trait DeepClone: Clone {
/// Return a deep copy of the value. Unlike `Clone`, the contents of shared pointer types
/// *are* copied.
fn deep_clone(&self) -> Self;

/// Perform deep copy-assignment from `source`.
///
/// `a.deep_clone_from(&b)` is equivalent to `a = b.deep_clone()` in
/// functionality, but can be overriden to reuse the resources of `a` to
/// avoid unnecessary allocations.
#[inline(always)]
fn deep_clone_from(&mut self, source: &Self) {
*self = source.deep_clone()
}
}

impl<T: DeepClone> DeepClone for ~T {
/// Return a deep copy of the owned box.
#[inline]
fn deep_clone(&self) -> ~T { ~(**self).deep_clone() }

/// Perform deep copy-assignment from `source` by reusing the existing allocation.
fn deep_clone_from(&mut self, source: &~T) {
**self = (**source).deep_clone()
}
}

// FIXME: #6525: should also be implemented for `T: Send + DeepClone`
Expand Down Expand Up @@ -234,6 +264,22 @@ fn test_borrowed_clone() {
assert_eq!(*z, 5);
}

#[test]
fn test_clone_from() {
let a = ~5;
let mut b = ~10;
b.clone_from(&a);
assert_eq!(*b, 5);
}

#[test]
fn test_deep_clone_from() {
let a = ~5;
let mut b = ~10;
b.deep_clone_from(&a);
assert_eq!(*b, 5);
}

#[test]
fn test_extern_fn_clone() {
trait Empty {}
Expand Down
24 changes: 23 additions & 1 deletion src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2029,7 +2029,7 @@ impl<'self, T:Clone> MutableCloneableVector<T> for &'self mut [T] {
#[inline]
fn copy_from(self, src: &[T]) -> uint {
for (a, b) in self.mut_iter().zip(src.iter()) {
*a = b.clone();
a.clone_from(b);
}
cmp::min(self.len(), src.len())
}
Expand Down Expand Up @@ -2282,13 +2282,35 @@ impl<A: Clone> Clone for ~[A] {
fn clone(&self) -> ~[A] {
self.iter().map(|item| item.clone()).collect()
}

fn clone_from(&mut self, source: &~[A]) {
if self.len() < source.len() {
*self = source.clone()
} else {
self.truncate(source.len());
for (x, y) in self.mut_iter().zip(source.iter()) {
x.clone_from(y);
}
}
}
}

impl<A: DeepClone> DeepClone for ~[A] {
#[inline]
fn deep_clone(&self) -> ~[A] {
self.iter().map(|item| item.deep_clone()).collect()
}

fn deep_clone_from(&mut self, source: &~[A]) {
if self.len() < source.len() {
*self = source.deep_clone()
} else {
self.truncate(source.len());
for (x, y) in self.mut_iter().zip(source.iter()) {
x.deep_clone_from(y);
}
}
}
}

// This works because every lifetime is a sub-lifetime of 'static
Expand Down