Skip to content

Commit 748c2c9

Browse files
committed
impl Clone for ~T, ~[T], ~str
1 parent 2b059c6 commit 748c2c9

File tree

7 files changed

+51
-25
lines changed

7 files changed

+51
-25
lines changed

src/libcore/clone.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ impl Clone for () {
2020
fn clone(&self) -> () { () }
2121
}
2222

23+
impl<T:Clone> Clone for ~T {
24+
#[inline(always)]
25+
fn clone(&self) -> ~T { ~(**self).clone() }
26+
}
27+
2328
macro_rules! clone_impl(
2429
($t:ty) => {
2530
impl Clone for $t {

src/libcore/str.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use at_vec;
2121
use cast;
2222
use char;
23+
use clone::Clone;
2324
use cmp::{Equiv, TotalOrd, Ordering, Less, Equal, Greater};
2425
use libc;
2526
use option::{None, Option, Some};
@@ -2433,6 +2434,13 @@ impl OwnedStr for ~str {
24332434
}
24342435
}
24352436
2437+
impl Clone for ~str {
2438+
#[inline(always)]
2439+
fn clone(&self) -> ~str {
2440+
self.to_str() // hilarious
2441+
}
2442+
}
2443+
24362444
#[cfg(test)]
24372445
mod tests {
24382446
use char;

src/libcore/unstable/global.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ unsafe fn global_data_clone_create_<T:Owned + Clone>(
6868
match value {
6969
None => {
7070
let value = create();
71-
clone_value = Some(value.clone());
71+
clone_value = Some((*value).clone());
7272
Some(value)
7373
}
7474
Some(value) => {
75-
clone_value = Some(value.clone());
75+
clone_value = Some((*value).clone());
7676
Some(value)
7777
}
7878
}
@@ -193,7 +193,7 @@ fn get_global_state() -> Exclusive<GlobalState> {
193193
// Successfully installed the global pointer
194194

195195
// Take a handle to return
196-
let clone = state.clone();
196+
let clone = (*state).clone();
197197

198198
// Install a runtime exit function to destroy the global object
199199
do at_exit {

src/libcore/vec.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use container::{Container, Mutable};
1616
use cast;
1717
use cmp::{Eq, Equiv, Ord, TotalOrd, Ordering, Less, Equal, Greater};
18+
use clone::Clone;
1819
use iter::BaseIter;
1920
use iter;
2021
use kinds::Copy;
@@ -2501,6 +2502,18 @@ impl<A:Copy> iter::CopyableNonstrictIter<A> for @[A] {
25012502
}
25022503
}
25032504

2505+
impl<A:Clone> Clone for ~[A] {
2506+
#[inline]
2507+
fn clone(&self) -> ~[A] {
2508+
let mut dolly = ~[];
2509+
vec::reserve(&mut dolly, self.len());
2510+
for self.each |item| {
2511+
dolly.push(item.clone());
2512+
}
2513+
return dolly;
2514+
}
2515+
}
2516+
25042517
// ___________________________________________________________________________
25052518

25062519
#[cfg(test)]

src/libstd/arc.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ mod tests {
572572
#[test] #[should_fail] #[ignore(cfg(windows))]
573573
pub fn test_rw_arc_poison_wr() {
574574
let arc = ~RWARC(1);
575-
let arc2 = ~arc.clone();
575+
let arc2 = (*arc).clone();
576576
do task::try || {
577577
do arc2.write |one| {
578578
fail_unless!(*one == 2);
@@ -585,7 +585,7 @@ mod tests {
585585
#[test] #[should_fail] #[ignore(cfg(windows))]
586586
pub fn test_rw_arc_poison_ww() {
587587
let arc = ~RWARC(1);
588-
let arc2 = ~arc.clone();
588+
let arc2 = (*arc).clone();
589589
do task::try || {
590590
do arc2.write |one| {
591591
fail_unless!(*one == 2);
@@ -598,7 +598,7 @@ mod tests {
598598
#[test] #[should_fail] #[ignore(cfg(windows))]
599599
pub fn test_rw_arc_poison_dw() {
600600
let arc = ~RWARC(1);
601-
let arc2 = ~arc.clone();
601+
let arc2 = (*arc).clone();
602602
do task::try || {
603603
do arc2.write_downgrade |write_mode| {
604604
do (&write_mode).write |one| {
@@ -613,7 +613,7 @@ mod tests {
613613
#[test] #[ignore(cfg(windows))]
614614
pub fn test_rw_arc_no_poison_rr() {
615615
let arc = ~RWARC(1);
616-
let arc2 = ~arc.clone();
616+
let arc2 = (*arc).clone();
617617
do task::try || {
618618
do arc2.read |one| {
619619
fail_unless!(*one == 2);
@@ -626,7 +626,7 @@ mod tests {
626626
#[test] #[ignore(cfg(windows))]
627627
pub fn test_rw_arc_no_poison_rw() {
628628
let arc = ~RWARC(1);
629-
let arc2 = ~arc.clone();
629+
let arc2 = (*arc).clone();
630630
do task::try || {
631631
do arc2.read |one| {
632632
fail_unless!(*one == 2);
@@ -639,7 +639,7 @@ mod tests {
639639
#[test] #[ignore(cfg(windows))]
640640
pub fn test_rw_arc_no_poison_dr() {
641641
let arc = ~RWARC(1);
642-
let arc2 = ~arc.clone();
642+
let arc2 = (*arc).clone();
643643
do task::try || {
644644
do arc2.write_downgrade |write_mode| {
645645
let read_mode = arc2.downgrade(write_mode);
@@ -655,7 +655,7 @@ mod tests {
655655
#[test]
656656
pub fn test_rw_arc() {
657657
let arc = ~RWARC(0);
658-
let arc2 = ~arc.clone();
658+
let arc2 = (*arc).clone();
659659
let (p,c) = comm::stream();
660660

661661
do task::spawn || {
@@ -673,7 +673,7 @@ mod tests {
673673
// Readers try to catch the writer in the act
674674
let mut children = ~[];
675675
for 5.times {
676-
let arc3 = ~arc.clone();
676+
let arc3 = (*arc).clone();
677677
do task::task().future_result(|+r| children.push(r)).spawn
678678
|| {
679679
do arc3.read |num| {
@@ -704,7 +704,7 @@ mod tests {
704704
for 10.times {
705705
let ((rp1,rc1),(rp2,rc2)) = (comm::stream(),comm::stream());
706706
reader_convos.push((rc1, rp2));
707-
let arcn = ~arc.clone();
707+
let arcn = (*arc).clone();
708708
do task::spawn || {
709709
rp1.recv(); // wait for downgrader to give go-ahead
710710
do arcn.read |state| {
@@ -715,7 +715,7 @@ mod tests {
715715
}
716716

717717
// Writer task
718-
let arc2 = ~arc.clone();
718+
let arc2 = (*arc).clone();
719719
let ((wp1,wc1),(wp2,wc2)) = (comm::stream(),comm::stream());
720720
do task::spawn || {
721721
wp1.recv();

src/libstd/sync.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ mod tests {
827827
// "load tmp = move ptr; inc tmp; store ptr <- tmp" dance.
828828
let (p,c) = comm::stream();
829829
let m = ~Mutex();
830-
let m2 = ~m.clone();
830+
let m2 = m.clone();
831831
let mut sharedstate = ~0;
832832
let ptr = ptr::addr_of(&(*sharedstate));
833833
do task::spawn || {
@@ -1105,13 +1105,13 @@ mod tests {
11051105
// Test mutual exclusion between readers and writers. Just like the
11061106
// mutex mutual exclusion test, a ways above.
11071107
let (p,c) = comm::stream();
1108-
let x2 = ~x.clone();
1108+
let x2 = (*x).clone();
11091109
let mut sharedstate = ~0;
11101110
let ptr = ptr::addr_of(&(*sharedstate));
11111111
do task::spawn || {
11121112
let sharedstate: &mut int =
11131113
unsafe { cast::reinterpret_cast(&ptr) };
1114-
access_shared(sharedstate, x2, mode1, 10);
1114+
access_shared(sharedstate, &x2, mode1, 10);
11151115
c.send(());
11161116
}
11171117
access_shared(sharedstate, x, mode2, 10);
@@ -1150,14 +1150,14 @@ mod tests {
11501150
mode2: RWlockMode,
11511151
make_mode2_go_first: bool) {
11521152
// Much like sem_multi_resource.
1153-
let x2 = ~x.clone();
1153+
let x2 = (*x).clone();
11541154
let (p1,c1) = comm::stream();
11551155
let (p2,c2) = comm::stream();
11561156
do task::spawn || {
11571157
if !make_mode2_go_first {
11581158
let _ = p2.recv(); // parent sends to us once it locks, or ...
11591159
}
1160-
do lock_rwlock_in_mode(x2, mode2) {
1160+
do lock_rwlock_in_mode(&x2, mode2) {
11611161
if make_mode2_go_first {
11621162
c1.send(()); // ... we send to it once we lock
11631163
}
@@ -1207,7 +1207,7 @@ mod tests {
12071207
12081208
// Child wakes up parent
12091209
do x.write_cond |cond| {
1210-
let x2 = ~x.clone();
1210+
let x2 = (*x).clone();
12111211
do task::spawn || {
12121212
do x2.write_cond |cond| {
12131213
let woken = cond.signal();
@@ -1218,7 +1218,7 @@ mod tests {
12181218
}
12191219
// Parent wakes up child
12201220
let (port,chan) = comm::stream();
1221-
let x3 = ~x.clone();
1221+
let x3 = (*x).clone();
12221222
do task::spawn || {
12231223
do x3.write_cond |cond| {
12241224
chan.send(());
@@ -1253,11 +1253,11 @@ mod tests {
12531253
let mut ports = ~[];
12541254
12551255
for num_waiters.times {
1256-
let xi = ~x.clone();
1256+
let xi = (*x).clone();
12571257
let (port, chan) = comm::stream();
12581258
ports.push(port);
12591259
do task::spawn || {
1260-
do lock_cond(xi, dg1) |cond| {
1260+
do lock_cond(&xi, dg1) |cond| {
12611261
chan.send(());
12621262
cond.wait();
12631263
chan.send(());
@@ -1289,10 +1289,10 @@ mod tests {
12891289
pub fn rwlock_kill_helper(mode1: RWlockMode, mode2: RWlockMode) {
12901290
// Mutex must get automatically unlocked if failed/killed within.
12911291
let x = ~RWlock();
1292-
let x2 = ~x.clone();
1292+
let x2 = (*x).clone();
12931293
12941294
let result: result::Result<(),()> = do task::try || {
1295-
do lock_rwlock_in_mode(x2, mode1) {
1295+
do lock_rwlock_in_mode(&x2, mode1) {
12961296
fail!();
12971297
}
12981298
};

src/libsyntax/ext/pipes/liveness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn analyze(proto: protocol, _cx: @ext_ctxt) {
6363
debug!("colive iteration %?", i);
6464
let mut new_colive = ~[];
6565
for colive.eachi |i, this_colive| {
66-
let mut result = ~this_colive.clone();
66+
let mut result = this_colive.clone();
6767
let this = proto.get_state_by_id(i);
6868
for this_colive.ones |j| {
6969
let next = proto.get_state_by_id(j);

0 commit comments

Comments
 (0)