core: Add Clone trait
This commit is contained in:
parent
1c348e6e38
commit
b21e9d52de
7 changed files with 64 additions and 26 deletions
10
src/libcore/clone.rs
Normal file
10
src/libcore/clone.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
/**
|
||||
Clonable types are copied with the clone method
|
||||
*/
|
||||
pub trait Clone {
|
||||
fn clone(&self) -> self;
|
||||
}
|
||||
|
||||
impl (): Clone {
|
||||
fn clone(&self) -> () { () }
|
||||
}
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
//
|
||||
|
|
|
@ -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)
|
||||
****************************************************************************/
|
||||
|
@ -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
|
||||
|
@ -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.
|
||||
|
|
|
@ -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 {
|
||||
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
@ -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) }
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue