Skip to content

Commit 3b9724f

Browse files
committed
---
yaml --- r: 89966 b: refs/heads/master c: 80dff18 h: refs/heads/master v: v3
1 parent b195c08 commit 3b9724f

File tree

9 files changed

+151
-151
lines changed

9 files changed

+151
-151
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 747213a280ac5505e2537952f1d28efceda0bfcc
2+
refs/heads/master: 80dff186416180423fe8117cbd19930a6c813ed9
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a6d3e57dca68fde4effdda3e4ae2887aa535fcd6
55
refs/heads/try: b160761e35efcd1207112b3b782c06633cf441a8

trunk/doc/tutorial.md

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,9 @@ calling the destructor, and the owner determines whether the object is mutable.
890890

891891
Ownership is recursive, so mutability is inherited recursively and a destructor
892892
destroys the contained tree of owned objects. Variables are top-level owners
893-
and destroy the contained object when they go out of scope.
893+
and destroy the contained object when they go out of scope. A box managed by
894+
the garbage collector starts a new ownership tree, and the destructor is called
895+
when it is collected.
894896

895897
~~~~
896898
// the struct owns the objects contained in the `x` and `y` fields
@@ -1007,6 +1009,51 @@ let mut s = r; // box becomes mutable
10071009
let t = s; // box becomes immutable
10081010
~~~~
10091011

1012+
# Managed boxes
1013+
1014+
A managed box (`@`) is a heap allocation with the lifetime managed by a
1015+
task-local garbage collector. It will be destroyed at some point after there
1016+
are no references left to the box, no later than the end of the task. Managed
1017+
boxes lack an owner, so they start a new ownership tree and don't inherit
1018+
mutability. They do own the contained object, and mutability is defined by the
1019+
type of the managed box (`@` or `@mut`). An object containing a managed box is
1020+
not `Owned`, and can't be sent between tasks.
1021+
1022+
~~~~
1023+
let a = @5; // immutable
1024+
1025+
let mut b = @5; // mutable variable, immutable box
1026+
b = @10;
1027+
1028+
let c = @mut 5; // immutable variable, mutable box
1029+
*c = 10;
1030+
1031+
let mut d = @mut 5; // mutable variable, mutable box
1032+
*d += 5;
1033+
d = @mut 15;
1034+
~~~~
1035+
1036+
A mutable variable and an immutable variable can refer to the same box, given
1037+
that their types are compatible. Mutability of a box is a property of its type,
1038+
however, so for example a mutable handle to an immutable box cannot be
1039+
assigned a reference to a mutable box.
1040+
1041+
~~~~
1042+
let a = @1; // immutable box
1043+
let b = @mut 2; // mutable box
1044+
1045+
let mut c : @int; // declare a variable with type managed immutable int
1046+
let mut d : @mut int; // and one of type managed mutable int
1047+
1048+
c = a; // box type is the same, okay
1049+
d = b; // box type is the same, okay
1050+
~~~~
1051+
1052+
~~~~ {.xfail-test}
1053+
// but b cannot be assigned to c, or a to d
1054+
c = b; // error
1055+
~~~~
1056+
10101057
# Borrowed pointers
10111058

10121059
Rust's borrowed pointers are a general purpose reference type. In contrast with
@@ -1300,52 +1347,6 @@ defined in [`std::vec`] and [`std::str`].
13001347
[`std::vec`]: std/vec/index.html
13011348
[`std::str`]: std/str/index.html
13021349
1303-
# Ownership escape hatches
1304-
1305-
Ownership can cleanly describe tree-like data structures, and borrowed pointers provide non-owning
1306-
references. However, more flexibility is often desired and Rust provides ways to escape from strict
1307-
single parent ownership.
1308-
1309-
The standard library provides the `std::rc::Rc` pointer type to express *shared ownership* over a
1310-
reference counted box. As soon as all of the `Rc` pointers go out of scope, the box and the
1311-
contained value are destroyed.
1312-
1313-
~~~
1314-
use std::rc::Rc;
1315-
1316-
// A fixed-size array allocated in a reference-counted box
1317-
let x = Rc::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
1318-
let y = x.clone(); // a new owner
1319-
let z = x; // this moves `x` into `z`, rather than creating a new owner
1320-
1321-
assert_eq!(*z.borrow(), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
1322-
1323-
let mut a = Rc::new([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); // the variable is mutable, but not the box
1324-
a = z;
1325-
~~~
1326-
1327-
A garbage collected pointer is provided via `std::gc::Gc`, with a task-local garbage collector
1328-
having ownership of the box. It allows the creation of cycles, and the individual `Gc` pointers do
1329-
not have a destructor.
1330-
1331-
~~~
1332-
use std::gc::Gc;
1333-
1334-
// A fixed-size array allocated in a garbage-collected box
1335-
let x = Gc::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
1336-
let y = x; // does not perform a move, unlike with `Rc`
1337-
let z = x;
1338-
1339-
assert_eq!(*z.borrow(), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
1340-
~~~
1341-
1342-
With shared ownership, mutability cannot be inherited so the boxes are always immutable. However,
1343-
it's possible to use *dynamic* mutability via types like `std::cell::Cell` where freezing is handled
1344-
via dynamic checks and can fail at runtime.
1345-
1346-
The `Rc` and `Gc` types are not sendable, so they cannot be used to share memory between tasks. Safe
1347-
immutable and mutable shared memory is provided by the `extra::arc` module.
1348-
13491350
# Closures
13501351
13511352
Named functions, like those we've seen so far, may not refer to local

trunk/src/librustc/middle/lint.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ pub enum lint {
7575
type_limits,
7676
type_overflow,
7777
unused_unsafe,
78-
unsafe_block,
7978

8079
managed_heap_memory,
8180
owned_heap_memory,
@@ -237,13 +236,6 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
237236
default: warn
238237
}),
239238

240-
("unsafe_block",
241-
LintSpec {
242-
lint: unsafe_block,
243-
desc: "usage of an `unsafe` block",
244-
default: allow
245-
}),
246-
247239
("unused_variable",
248240
LintSpec {
249241
lint: unused_variable,
@@ -878,7 +870,8 @@ fn check_pat_non_uppercase_statics(cx: &Context, p: &ast::Pat) {
878870

879871
fn check_unused_unsafe(cx: &Context, e: &ast::Expr) {
880872
match e.node {
881-
// Don't warn about generated blocks, that'll just pollute the output.
873+
// Don't warn about generated blocks, that'll just pollute the
874+
// output.
882875
ast::ExprBlock(ref blk) => {
883876
if blk.rules == ast::UnsafeBlock(ast::UserProvided) &&
884877
!cx.tcx.used_unsafe.contains(&blk.id) {
@@ -890,16 +883,6 @@ fn check_unused_unsafe(cx: &Context, e: &ast::Expr) {
890883
}
891884
}
892885

893-
fn check_unsafe_block(cx: &Context, e: &ast::Expr) {
894-
match e.node {
895-
// Don't warn about generated blocks, that'll just pollute the output.
896-
ast::ExprBlock(ref blk) if blk.rules == ast::UnsafeBlock(ast::UserProvided) => {
897-
cx.span_lint(unsafe_block, blk.span, "usage of an `unsafe` block");
898-
}
899-
_ => ()
900-
}
901-
}
902-
903886
fn check_unused_mut_pat(cx: &Context, p: @ast::Pat) {
904887
match p.node {
905888
ast::PatIdent(ast::BindByValue(ast::MutMutable),
@@ -1143,7 +1126,6 @@ impl<'self> Visitor<()> for Context<'self> {
11431126
check_while_true_expr(self, e);
11441127
check_stability(self, e);
11451128
check_unused_unsafe(self, e);
1146-
check_unsafe_block(self, e);
11471129
check_unnecessary_allocation(self, e);
11481130
check_heap_expr(self, e);
11491131

trunk/src/libstd/rand/distributions/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -444,17 +444,17 @@ mod tests {
444444
fn test_rand_sample() {
445445
let mut rand_sample = RandSample::<ConstRand>;
446446

447-
assert_eq!(*rand_sample.sample(&mut task_rng()), 0);
448-
assert_eq!(*rand_sample.ind_sample(&mut task_rng()), 0);
447+
assert_eq!(*rand_sample.sample(task_rng()), 0);
448+
assert_eq!(*rand_sample.ind_sample(task_rng()), 0);
449449
}
450450

451451
#[test]
452452
fn test_normal() {
453453
let mut norm = Normal::new(10.0, 10.0);
454-
let mut rng = task_rng();
454+
let rng = task_rng();
455455
for _ in range(0, 1000) {
456-
norm.sample(&mut rng);
457-
norm.ind_sample(&mut rng);
456+
norm.sample(rng);
457+
norm.ind_sample(rng);
458458
}
459459
}
460460
#[test]
@@ -466,10 +466,10 @@ mod tests {
466466
#[test]
467467
fn test_exp() {
468468
let mut exp = Exp::new(10.0);
469-
let mut rng = task_rng();
469+
let rng = task_rng();
470470
for _ in range(0, 1000) {
471-
assert!(exp.sample(&mut rng) >= 0.0);
472-
assert!(exp.ind_sample(&mut rng) >= 0.0);
471+
assert!(exp.sample(rng) >= 0.0);
472+
assert!(exp.ind_sample(rng) >= 0.0);
473473
}
474474
}
475475
#[test]

trunk/src/libstd/rand/distributions/range.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ mod tests {
183183

184184
#[test]
185185
fn test_integers() {
186-
let mut rng = task_rng();
186+
let rng = task_rng();
187187
macro_rules! t (
188188
($($ty:ty),*) => {{
189189
$(
@@ -193,9 +193,9 @@ mod tests {
193193
for &(low, high) in v.iter() {
194194
let mut sampler: Range<$ty> = Range::new(low, high);
195195
for _ in range(0, 1000) {
196-
let v = sampler.sample(&mut rng);
196+
let v = sampler.sample(rng);
197197
assert!(low <= v && v < high);
198-
let v = sampler.ind_sample(&mut rng);
198+
let v = sampler.ind_sample(rng);
199199
assert!(low <= v && v < high);
200200
}
201201
}
@@ -208,7 +208,7 @@ mod tests {
208208

209209
#[test]
210210
fn test_floats() {
211-
let mut rng = task_rng();
211+
let rng = task_rng();
212212
macro_rules! t (
213213
($($ty:ty),*) => {{
214214
$(
@@ -219,9 +219,9 @@ mod tests {
219219
for &(low, high) in v.iter() {
220220
let mut sampler: Range<$ty> = Range::new(low, high);
221221
for _ in range(0, 1000) {
222-
let v = sampler.sample(&mut rng);
222+
let v = sampler.sample(rng);
223223
assert!(low <= v && v < high);
224-
let v = sampler.ind_sample(&mut rng);
224+
let v = sampler.ind_sample(rng);
225225
assert!(low <= v && v < high);
226226
}
227227
}

trunk/src/libstd/rand/mod.rs

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -577,24 +577,11 @@ impl reseeding::Reseeder<StdRng> for TaskRngReseeder {
577577
}
578578
}
579579
static TASK_RNG_RESEED_THRESHOLD: uint = 32_768;
580-
type TaskRngInner = reseeding::ReseedingRng<StdRng, TaskRngReseeder>;
581580
/// The task-local RNG.
582-
#[no_send]
583-
pub struct TaskRng {
584-
// This points into TLS (specifically, it points to the endpoint
585-
// of a ~ stored in TLS, to make it robust against TLS moving
586-
// things internally) and so this struct cannot be legally
587-
// transferred between tasks *and* it's unsafe to deallocate the
588-
// RNG other than when a task is finished.
589-
//
590-
// The use of unsafe code here is OK if the invariants above are
591-
// satisfied; and it allows us to avoid (unnecessarily) using a
592-
// GC'd or RC'd pointer.
593-
priv rng: *mut TaskRngInner
594-
}
581+
pub type TaskRng = reseeding::ReseedingRng<StdRng, TaskRngReseeder>;
595582

596583
// used to make space in TLS for a random number generator
597-
local_data_key!(TASK_RNG_KEY: ~TaskRngInner)
584+
local_data_key!(TASK_RNG_KEY: @mut TaskRng)
598585

599586
/// Retrieve the lazily-initialized task-local random number
600587
/// generator, seeded by the system. Intended to be used in method
@@ -607,34 +594,34 @@ local_data_key!(TASK_RNG_KEY: ~TaskRngInner)
607594
/// if the operating system random number generator is rigged to give
608595
/// the same sequence always. If absolute consistency is required,
609596
/// explicitly select an RNG, e.g. `IsaacRng` or `Isaac64Rng`.
610-
pub fn task_rng() -> TaskRng {
611-
local_data::get_mut(TASK_RNG_KEY, |rng| match rng {
597+
pub fn task_rng() -> @mut TaskRng {
598+
let r = local_data::get(TASK_RNG_KEY, |k| k.map(|k| *k));
599+
match r {
612600
None => {
613-
let mut rng = ~reseeding::ReseedingRng::new(StdRng::new(),
601+
let rng = @mut reseeding::ReseedingRng::new(StdRng::new(),
614602
TASK_RNG_RESEED_THRESHOLD,
615603
TaskRngReseeder);
616-
let ptr = &mut *rng as *mut TaskRngInner;
617-
618604
local_data::set(TASK_RNG_KEY, rng);
619-
620-
TaskRng { rng: ptr }
605+
rng
621606
}
622-
Some(rng) => TaskRng { rng: &mut **rng }
623-
})
607+
Some(rng) => rng
608+
}
624609
}
625610

626-
impl Rng for TaskRng {
611+
// Allow direct chaining with `task_rng`
612+
impl<R: Rng> Rng for @mut R {
613+
#[inline]
627614
fn next_u32(&mut self) -> u32 {
628-
unsafe { (*self.rng).next_u32() }
615+
(**self).next_u32()
629616
}
630-
617+
#[inline]
631618
fn next_u64(&mut self) -> u64 {
632-
unsafe { (*self.rng).next_u64() }
619+
(**self).next_u64()
633620
}
634621

635622
#[inline]
636623
fn fill_bytes(&mut self, bytes: &mut [u8]) {
637-
unsafe { (*self.rng).fill_bytes(bytes) }
624+
(**self).fill_bytes(bytes);
638625
}
639626
}
640627

0 commit comments

Comments
 (0)