Skip to content

Commit b21e9d5

Browse files
committed
core: Add Clone trait
1 parent 1c348e6 commit b21e9d5

File tree

7 files changed

+64
-26
lines changed

7 files changed

+64
-26
lines changed

src/libcore/clone.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
Clonable types are copied with the clone method
3+
*/
4+
pub trait Clone {
5+
fn clone(&self) -> self;
6+
}
7+
8+
impl (): Clone {
9+
fn clone(&self) -> () { () }
10+
}

src/libcore/core.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ pub mod to_str;
156156
pub mod to_bytes;
157157
pub mod from_str;
158158
pub mod util;
159+
pub mod clone;
159160

160161
// Data structure modules
161162

src/libcore/core.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ pub use coreops::ops::{BitXor};
4848
#[cfg(test)]
4949
pub use coreops::ops::{Shl, Shr, Index};
5050

51+
#[cfg(notest)]
52+
pub use clone::Clone;
53+
#[cfg(test)]
54+
pub use coreops::clone::Clone;
5155

5256
// Export the log levels as global constants. Higher levels mean
5357
// more-verbosity. Error is the bottom level, default logging level is

src/libcore/private.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,14 @@ pub fn exclusive<T:Send >(user_data: T) -> Exclusive<T> {
509509
Exclusive { x: unsafe { shared_mutable_state(move data) } }
510510
}
511511

512-
impl<T: Send> Exclusive<T> {
512+
impl<T: Send> Exclusive<T>: Clone {
513513
// Duplicate an exclusive ARC, as std::arc::clone.
514-
fn clone() -> Exclusive<T> {
514+
fn clone(&self) -> Exclusive<T> {
515515
Exclusive { x: unsafe { clone_shared_mutable_state(&self.x) } }
516516
}
517+
}
517518

519+
impl<T: Send> Exclusive<T> {
518520
// Exactly like std::arc::mutex_arc,access(), but with the little_lock
519521
// instead of a proper mutex. Same reason for being unsafe.
520522
//

src/libstd/arc.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ fn unwrap<T: Const Send>(rc: ARC<T>) -> T {
103103
unsafe { unwrap_shared_mutable_state(move x) }
104104
}
105105

106+
impl<T: Const Send> ARC<T>: Clone {
107+
fn clone(&self) -> ARC<T> {
108+
clone(self)
109+
}
110+
}
111+
106112
/****************************************************************************
107113
* Mutex protected ARC (unsafe)
108114
****************************************************************************/
@@ -128,13 +134,16 @@ pub fn mutex_arc_with_condvars<T: Send>(user_data: T,
128134
MutexARC { x: unsafe { shared_mutable_state(move data) } }
129135
}
130136

131-
impl<T: Send> &MutexARC<T> {
137+
impl<T: Send> MutexARC<T>: Clone {
132138
/// Duplicate a mutex-protected ARC, as arc::clone.
133-
fn clone() -> MutexARC<T> {
139+
fn clone(&self) -> MutexARC<T> {
134140
// NB: Cloning the underlying mutex is not necessary. Its reference
135141
// count would be exactly the same as the shared state's.
136142
MutexARC { x: unsafe { clone_shared_mutable_state(&self.x) } }
137143
}
144+
}
145+
146+
impl<T: Send> &MutexARC<T> {
138147

139148
/**
140149
* Access the underlying mutable data with mutual exclusion from other
@@ -265,13 +274,16 @@ pub fn rw_arc_with_condvars<T: Const Send>(user_data: T,
265274
RWARC { x: unsafe { shared_mutable_state(move data) }, cant_nest: () }
266275
}
267276
268-
impl<T: Const Send> &RWARC<T> {
277+
impl<T: Const Send> RWARC<T> {
269278
/// Duplicate a rwlock-protected ARC, as arc::clone.
270-
fn clone() -> RWARC<T> {
279+
fn clone(&self) -> RWARC<T> {
271280
RWARC { x: unsafe { clone_shared_mutable_state(&self.x) },
272281
cant_nest: () }
273282
}
274283
284+
}
285+
286+
impl<T: Const Send> &RWARC<T> {
275287
/**
276288
* Access the underlying data mutably. Locks the rwlock in write mode;
277289
* other readers and writers will block.

src/libstd/bitv.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -290,22 +290,6 @@ impl Bitv {
290290
#[inline(always)]
291291
fn assign(v: &Bitv) -> bool { self.do_op(Assign, v) }
292292

293-
/// Makes a copy of a bitvector
294-
#[inline(always)]
295-
fn clone() -> ~Bitv {
296-
~match self.rep {
297-
Small(ref b) => {
298-
Bitv{nbits: self.nbits, rep: Small(~SmallBitv{bits: b.bits})}
299-
}
300-
Big(ref b) => {
301-
let st = to_mut(from_elem(self.nbits / uint_bits + 1, 0));
302-
let len = st.len();
303-
for uint::range(0, len) |i| { st[i] = b.storage[i]; };
304-
Bitv{nbits: self.nbits, rep: Big(~BigBitv{storage: move st})}
305-
}
306-
}
307-
}
308-
309293
/// Retrieve the value at index `i`
310294
#[inline(always)]
311295
pure fn get(i: uint) -> bool {
@@ -512,6 +496,25 @@ impl Bitv {
512496

513497
}
514498

499+
impl Bitv: Clone {
500+
/// Makes a copy of a bitvector
501+
#[inline(always)]
502+
fn clone(&self) -> Bitv {
503+
match self.rep {
504+
Small(ref b) => {
505+
Bitv{nbits: self.nbits, rep: Small(~SmallBitv{bits: b.bits})}
506+
}
507+
Big(ref b) => {
508+
let st = to_mut(from_elem(self.nbits / uint_bits + 1, 0));
509+
let len = st.len();
510+
for uint::range(0, len) |i| { st[i] = b.storage[i]; };
511+
Bitv{nbits: self.nbits, rep: Big(~BigBitv{storage: move st})}
512+
}
513+
}
514+
}
515+
516+
}
517+
515518
/**
516519
* Transform a byte-vector into a bitv. Each byte becomes 8 bits,
517520
* with the most significant bits of each byte coming first. Each

src/libstd/sync.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,14 @@ fn semaphore(count: int) -> Semaphore {
358358
Semaphore { sem: new_sem(count, ()) }
359359
}
360360

361-
impl &Semaphore {
361+
impl Semaphore: Clone {
362362
/// Create a new handle to the semaphore.
363-
fn clone() -> Semaphore { Semaphore { sem: Sem((*self.sem).clone()) } }
363+
fn clone(&self) -> Semaphore {
364+
Semaphore { sem: Sem((*self.sem).clone()) }
365+
}
366+
}
364367

368+
impl &Semaphore {
365369
/**
366370
* Acquire a resource represented by the semaphore. Blocks if necessary
367371
* until resource(s) become available.
@@ -404,10 +408,12 @@ pub fn mutex_with_condvars(num_condvars: uint) -> Mutex {
404408
Mutex { sem: new_sem_and_signal(1, num_condvars) }
405409
}
406410

407-
impl &Mutex {
411+
impl Mutex: Clone {
408412
/// Create a new handle to the mutex.
409-
fn clone() -> Mutex { Mutex { sem: Sem((*self.sem).clone()) } }
413+
fn clone(&self) -> Mutex { Mutex { sem: Sem((*self.sem).clone()) } }
414+
}
410415

416+
impl &Mutex {
411417
/// Run a function with ownership of the mutex.
412418
fn lock<U>(blk: fn() -> U) -> U { (&self.sem).access(blk) }
413419

0 commit comments

Comments
 (0)