Skip to content

Commit 3f232bc

Browse files
author
Olivier Saut
committed
Add ARC::get method and implements the function from it. Add an example showing a simple use of ARC.
1 parent a9c7d3f commit 3f232bc

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/libstd/arc.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,33 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
/**
11+
/*!
1212
* Concurrency-enabled mechanisms for sharing mutable and/or immutable state
1313
* between tasks.
14+
*
15+
* # Example
16+
*
17+
* In this example, a large vector of floats is shared between several tasks.
18+
* With simple pipes, without ARC, a copy would have to be made for each task.
19+
*
20+
* ~~~
21+
* extern mod std;
22+
* use std::arc;
23+
* let numbers=vec::from_fn(100, |ind| (ind as float)*rand::random());
24+
* let shared_numbers=arc::ARC(numbers);
25+
*
26+
* for 10.times {
27+
* let (port, chan) = stream();
28+
* chan.send(shared_numbers.clone());
29+
*
30+
* do spawn {
31+
* let shared_numbers=port.recv();
32+
* let local_numbers=shared_numbers.get();
33+
*
34+
* // Work with the local numbers
35+
* }
36+
* }
37+
* ~~~
1438
*/
1539

1640
use sync;
@@ -93,9 +117,14 @@ pub fn ARC<T:Const + Owned>(data: T) -> ARC<T> {
93117
* wrapper.
94118
*/
95119
pub fn get<'a, T:Const + Owned>(rc: &'a ARC<T>) -> &'a T {
96-
unsafe { &*rc.x.get_immut() }
120+
rc.get()
97121
}
98122

123+
impl<T:Const+Owned> ARC<T> {
124+
pub fn get<'a>(&'a self) -> &'a T {
125+
unsafe { &*self.x.get_immut() }
126+
}
127+
}
99128
/**
100129
* Duplicate an atomically reference counted wrapper.
101130
*
@@ -508,6 +537,7 @@ mod tests {
508537
c.send(arc::clone(&arc_v));
509538

510539
assert_eq!((*arc::get(&arc_v))[2], 3);
540+
assert_eq!(arc_v.get()[4], 5);
511541

512542
info!(arc_v);
513543
}

0 commit comments

Comments
 (0)