Skip to content

Commit b22a060

Browse files
committed
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.
1 parent babe506 commit b22a060

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/libcore/clone.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ impl<T:Clone> Clone for ~T {
3636
fn clone(&self) -> ~T { ~(**self).clone() }
3737
}
3838

39+
impl<T:Clone> Clone for @T {
40+
#[inline(always)]
41+
fn clone(&self) -> @T { @(**self).clone() }
42+
}
43+
44+
impl<T:Clone> Clone for @mut T {
45+
#[inline(always)]
46+
fn clone(&self) -> @mut T { @mut (**self).clone() }
47+
}
48+
3949
macro_rules! clone_impl(
4050
($t:ty) => {
4151
impl Clone for $t {

src/test/run-pass/borrowck-borrow-from-expr-block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn borrow(x: &int, f: &fn(x: &int)) {
1313
}
1414

1515
fn test1(x: @~int) {
16-
do borrow(&*x.clone()) |p| {
16+
do borrow(&**x.clone()) |p| {
1717
let x_a = ptr::addr_of(&(**x));
1818
assert!((x_a as uint) != ptr::to_uint(p));
1919
assert!(unsafe{*x_a} == *p);

src/test/run-pass/clones.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let a : ~int = ~5i;
13+
let b : ~int = a.clone();
14+
15+
debug!(fmt!("a: %?, b: %?", a, b));
16+
17+
let a : @int = @5i;
18+
let b : @int = a.clone();
19+
20+
debug!(fmt!("a: %?, b: %?", a, b));
21+
22+
let a : @mut int = @mut 5i;
23+
let b : @mut int = a.clone();
24+
*b = 6;
25+
26+
debug!(fmt!("a: %?, b: %?", a, b));
27+
}

0 commit comments

Comments
 (0)