diff options
| author | Olivier Saut <osaut@airpost.net> | 2013-05-20 12:07:14 +0200 |
|---|---|---|
| committer | Olivier Saut <osaut@airpost.net> | 2013-05-20 12:07:14 +0200 |
| commit | 3f232bc4a01d102599de0565ff769acdc06bac53 (patch) | |
| tree | 03ee103dc94a935c50544285600d0ef04a064d82 | |
| parent | a9c7d3f7757ee67fd5d068b0ebfb5a95d8e4d2cc (diff) | |
| download | rust-3f232bc4a01d102599de0565ff769acdc06bac53.tar.gz rust-3f232bc4a01d102599de0565ff769acdc06bac53.zip | |
Add ARC::get method and implements the function from it. Add an example showing a simple use of ARC.
| -rw-r--r-- | src/libstd/arc.rs | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/src/libstd/arc.rs b/src/libstd/arc.rs index 8d50c306878..9c7db35f6f2 100644 --- a/src/libstd/arc.rs +++ b/src/libstd/arc.rs @@ -8,9 +8,33 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -/** +/*! * Concurrency-enabled mechanisms for sharing mutable and/or immutable state * between tasks. + * + * # Example + * + * In this example, a large vector of floats is shared between several tasks. + * With simple pipes, without ARC, a copy would have to be made for each task. + * + * ~~~ + * extern mod std; + * use std::arc; + * let numbers=vec::from_fn(100, |ind| (ind as float)*rand::random()); + * let shared_numbers=arc::ARC(numbers); + * + * for 10.times { + * let (port, chan) = stream(); + * chan.send(shared_numbers.clone()); + * + * do spawn { + * let shared_numbers=port.recv(); + * let local_numbers=shared_numbers.get(); + * + * // Work with the local numbers + * } + * } + * ~~~ */ use sync; @@ -93,9 +117,14 @@ pub fn ARC<T:Const + Owned>(data: T) -> ARC<T> { * wrapper. */ pub fn get<'a, T:Const + Owned>(rc: &'a ARC<T>) -> &'a T { - unsafe { &*rc.x.get_immut() } + rc.get() } +impl<T:Const+Owned> ARC<T> { + pub fn get<'a>(&'a self) -> &'a T { + unsafe { &*self.x.get_immut() } + } +} /** * Duplicate an atomically reference counted wrapper. * @@ -508,6 +537,7 @@ mod tests { c.send(arc::clone(&arc_v)); assert_eq!((*arc::get(&arc_v))[2], 3); + assert_eq!(arc_v.get()[4], 5); info!(arc_v); } |
