1
- // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1
+ // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2
2
// file at the top-level directory of this distribution and at
3
3
// http://rust-lang.org/COPYRIGHT.
4
4
//
8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- /**
11
+ /*!
12
12
* Concurrency-enabled mechanisms for sharing mutable and/or immutable state
13
13
* 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
+ * ~~~
14
38
*/
15
39
16
40
use sync;
@@ -21,7 +45,7 @@ use core::unstable::sync::UnsafeAtomicRcBox;
21
45
use core:: ptr;
22
46
use core:: task;
23
47
24
- /// As sync::condvar, a mechanism for unlock-and-descheduling and signalling .
48
+ /// As sync::condvar, a mechanism for unlock-and-descheduling and signaling .
25
49
pub struct Condvar < ' self > {
26
50
is_mutex : bool ,
27
51
failed : & ' self mut bool ,
@@ -93,9 +117,14 @@ pub fn ARC<T:Const + Owned>(data: T) -> ARC<T> {
93
117
* wrapper.
94
118
*/
95
119
pub fn get < ' a , T : Const + Owned > ( rc : & ' a ARC < T > ) -> & ' a T {
96
- unsafe { & * rc. x . get_immut ( ) }
120
+ rc. get ( )
97
121
}
98
122
123
+ impl < T : Const +Owned > ARC < T > {
124
+ pub fn get < ' a > ( & ' a self ) -> & ' a T {
125
+ unsafe { & * self . x . get_immut ( ) }
126
+ }
127
+ }
99
128
/**
100
129
* Duplicate an atomically reference counted wrapper.
101
130
*
@@ -508,6 +537,7 @@ mod tests {
508
537
c. send ( arc:: clone ( & arc_v) ) ;
509
538
510
539
assert_eq ! ( ( * arc:: get( & arc_v) ) [ 2 ] , 3 ) ;
540
+ assert_eq ! ( arc_v. get( ) [ 4 ] , 5 ) ;
511
541
512
542
info ! ( arc_v) ;
513
543
}
0 commit comments