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

Implement Clone for @ and @mut types. #5751

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
33 changes: 32 additions & 1 deletion src/libcore/clone.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand Down Expand Up @@ -36,6 +36,16 @@ impl<T:Clone> Clone for ~T {
fn clone(&self) -> ~T { ~(**self).clone() }
}

impl<T:Clone> Clone for @T {
#[inline(always)]
fn clone(&self) -> @T { @(**self).clone() }
}

impl<T:Clone> Clone for @mut T {
#[inline(always)]
fn clone(&self) -> @mut T { @mut (**self).clone() }
}

macro_rules! clone_impl(
($t:ty) => {
impl Clone for $t {
Expand Down Expand Up @@ -63,3 +73,24 @@ clone_impl!(f64)

clone_impl!(bool)
clone_impl!(char)

#[test]
fn test_owned_clone() {
let a : ~int = ~5i;
let b : ~int = a.clone();
assert!(a == b);
}

#[test]
fn test_managed_clone() {
let a : @int = @5i;
let b : @int = a.clone();
assert!(a == b);
}

#[test]
fn test_managed_mut_clone() {
let a : @int = @5i;
let b : @int = a.clone();
assert!(a == b);
}
4 changes: 2 additions & 2 deletions src/test/run-pass/borrowck-borrow-from-expr-block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -13,7 +13,7 @@ fn borrow(x: &int, f: &fn(x: &int)) {
}

fn test1(x: @~int) {
do borrow(&*x.clone()) |p| {
do borrow(&**x.clone()) |p| {
let x_a = ptr::addr_of(&(**x));
assert!((x_a as uint) != ptr::to_uint(p));
assert!(unsafe{*x_a} == *p);
Expand Down