Skip to content

Commit

Permalink
core: Add Clone trait
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Nov 27, 2012
1 parent 1c348e6 commit b21e9d5
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 26 deletions.
10 changes: 10 additions & 0 deletions src/libcore/clone.rs
@@ -0,0 +1,10 @@
/**
Clonable types are copied with the clone method
*/
pub trait Clone {
fn clone(&self) -> self;
}

impl (): Clone {
fn clone(&self) -> () { () }
}
1 change: 1 addition & 0 deletions src/libcore/core.rc
Expand Up @@ -156,6 +156,7 @@ pub mod to_str;
pub mod to_bytes;
pub mod from_str;
pub mod util;
pub mod clone;

// Data structure modules

Expand Down
4 changes: 4 additions & 0 deletions src/libcore/core.rs
Expand Up @@ -48,6 +48,10 @@ pub use coreops::ops::{BitXor};
#[cfg(test)]
pub use coreops::ops::{Shl, Shr, Index};

#[cfg(notest)]
pub use clone::Clone;
#[cfg(test)]
pub use coreops::clone::Clone;

// Export the log levels as global constants. Higher levels mean
// more-verbosity. Error is the bottom level, default logging level is
Expand Down
6 changes: 4 additions & 2 deletions src/libcore/private.rs
Expand Up @@ -509,12 +509,14 @@ pub fn exclusive<T:Send >(user_data: T) -> Exclusive<T> {
Exclusive { x: unsafe { shared_mutable_state(move data) } }
}

impl<T: Send> Exclusive<T> {
impl<T: Send> Exclusive<T>: Clone {
// Duplicate an exclusive ARC, as std::arc::clone.
fn clone() -> Exclusive<T> {
fn clone(&self) -> Exclusive<T> {
Exclusive { x: unsafe { clone_shared_mutable_state(&self.x) } }
}
}

impl<T: Send> Exclusive<T> {
// Exactly like std::arc::mutex_arc,access(), but with the little_lock
// instead of a proper mutex. Same reason for being unsafe.
//
Expand Down
20 changes: 16 additions & 4 deletions src/libstd/arc.rs
Expand Up @@ -103,6 +103,12 @@ fn unwrap<T: Const Send>(rc: ARC<T>) -> T {
unsafe { unwrap_shared_mutable_state(move x) }
}

impl<T: Const Send> ARC<T>: Clone {
fn clone(&self) -> ARC<T> {
clone(self)
}
}

/****************************************************************************
* Mutex protected ARC (unsafe)
****************************************************************************/
Expand All @@ -128,13 +134,16 @@ pub fn mutex_arc_with_condvars<T: Send>(user_data: T,
MutexARC { x: unsafe { shared_mutable_state(move data) } }
}

impl<T: Send> &MutexARC<T> {
impl<T: Send> MutexARC<T>: Clone {
/// Duplicate a mutex-protected ARC, as arc::clone.
fn clone() -> MutexARC<T> {
fn clone(&self) -> MutexARC<T> {
// NB: Cloning the underlying mutex is not necessary. Its reference
// count would be exactly the same as the shared state's.
MutexARC { x: unsafe { clone_shared_mutable_state(&self.x) } }
}
}

impl<T: Send> &MutexARC<T> {

/**
* Access the underlying mutable data with mutual exclusion from other
Expand Down Expand Up @@ -265,13 +274,16 @@ pub fn rw_arc_with_condvars<T: Const Send>(user_data: T,
RWARC { x: unsafe { shared_mutable_state(move data) }, cant_nest: () }
}

impl<T: Const Send> &RWARC<T> {
impl<T: Const Send> RWARC<T> {
/// Duplicate a rwlock-protected ARC, as arc::clone.
fn clone() -> RWARC<T> {
fn clone(&self) -> RWARC<T> {
RWARC { x: unsafe { clone_shared_mutable_state(&self.x) },
cant_nest: () }
}

}

impl<T: Const Send> &RWARC<T> {
/**
* Access the underlying data mutably. Locks the rwlock in write mode;
* other readers and writers will block.
Expand Down
35 changes: 19 additions & 16 deletions src/libstd/bitv.rs
Expand Up @@ -290,22 +290,6 @@ impl Bitv {
#[inline(always)]
fn assign(v: &Bitv) -> bool { self.do_op(Assign, v) }

/// Makes a copy of a bitvector
#[inline(always)]
fn clone() -> ~Bitv {
~match self.rep {
Small(ref b) => {
Bitv{nbits: self.nbits, rep: Small(~SmallBitv{bits: b.bits})}
}
Big(ref b) => {
let st = to_mut(from_elem(self.nbits / uint_bits + 1, 0));
let len = st.len();
for uint::range(0, len) |i| { st[i] = b.storage[i]; };
Bitv{nbits: self.nbits, rep: Big(~BigBitv{storage: move st})}
}
}
}

/// Retrieve the value at index `i`
#[inline(always)]
pure fn get(i: uint) -> bool {
Expand Down Expand Up @@ -512,6 +496,25 @@ impl Bitv {

}

impl Bitv: Clone {
/// Makes a copy of a bitvector
#[inline(always)]
fn clone(&self) -> Bitv {
match self.rep {
Small(ref b) => {
Bitv{nbits: self.nbits, rep: Small(~SmallBitv{bits: b.bits})}
}
Big(ref b) => {
let st = to_mut(from_elem(self.nbits / uint_bits + 1, 0));
let len = st.len();
for uint::range(0, len) |i| { st[i] = b.storage[i]; };
Bitv{nbits: self.nbits, rep: Big(~BigBitv{storage: move st})}
}
}
}

}

/**
* Transform a byte-vector into a bitv. Each byte becomes 8 bits,
* with the most significant bits of each byte coming first. Each
Expand Down
14 changes: 10 additions & 4 deletions src/libstd/sync.rs
Expand Up @@ -358,10 +358,14 @@ fn semaphore(count: int) -> Semaphore {
Semaphore { sem: new_sem(count, ()) }
}

impl &Semaphore {
impl Semaphore: Clone {
/// Create a new handle to the semaphore.
fn clone() -> Semaphore { Semaphore { sem: Sem((*self.sem).clone()) } }
fn clone(&self) -> Semaphore {
Semaphore { sem: Sem((*self.sem).clone()) }
}
}

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

impl &Mutex {
impl Mutex: Clone {
/// Create a new handle to the mutex.
fn clone() -> Mutex { Mutex { sem: Sem((*self.sem).clone()) } }
fn clone(&self) -> Mutex { Mutex { sem: Sem((*self.sem).clone()) } }
}

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

Expand Down

0 comments on commit b21e9d5

Please sign in to comment.