From b22a06000d68faf7fe080dc9fd5a2686502d212f Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Fri, 5 Apr 2013 16:41:47 -0600 Subject: [PATCH] Implement Clone for @ and @mut types. The borrowck-borrow-from-expr-block test had to be updated. I'm not sure why it compiled before since ~int was already clonable. --- src/libcore/clone.rs | 10 +++++++ .../borrowck-borrow-from-expr-block.rs | 2 +- src/test/run-pass/clones.rs | 27 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/test/run-pass/clones.rs diff --git a/src/libcore/clone.rs b/src/libcore/clone.rs index 7b86355c91e..10cce4f69c6 100644 --- a/src/libcore/clone.rs +++ b/src/libcore/clone.rs @@ -36,6 +36,16 @@ impl Clone for ~T { fn clone(&self) -> ~T { ~(**self).clone() } } +impl Clone for @T { + #[inline(always)] + fn clone(&self) -> @T { @(**self).clone() } +} + +impl Clone for @mut T { + #[inline(always)] + fn clone(&self) -> @mut T { @mut (**self).clone() } +} + macro_rules! clone_impl( ($t:ty) => { impl Clone for $t { diff --git a/src/test/run-pass/borrowck-borrow-from-expr-block.rs b/src/test/run-pass/borrowck-borrow-from-expr-block.rs index 401985023bc..fc7786d08cb 100644 --- a/src/test/run-pass/borrowck-borrow-from-expr-block.rs +++ b/src/test/run-pass/borrowck-borrow-from-expr-block.rs @@ -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); diff --git a/src/test/run-pass/clones.rs b/src/test/run-pass/clones.rs new file mode 100644 index 00000000000..f4fa1b81ab1 --- /dev/null +++ b/src/test/run-pass/clones.rs @@ -0,0 +1,27 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + let a : ~int = ~5i; + let b : ~int = a.clone(); + + debug!(fmt!("a: %?, b: %?", a, b)); + + let a : @int = @5i; + let b : @int = a.clone(); + + debug!(fmt!("a: %?, b: %?", a, b)); + + let a : @mut int = @mut 5i; + let b : @mut int = a.clone(); + *b = 6; + + debug!(fmt!("a: %?, b: %?", a, b)); +}