Skip to content

Commit 29003c7

Browse files
committed
Rename the poorly named Managed<T> type to Mut<T>.
The Mut<T> type is intended to allow freezable data stuctures to be stored in `@mut` boxes. Currently this causes borrowck to be very conserivative since it cannot prove that you are not modifying such a structure while iterating over it, for example. But if you do `@Mut<T>` instead of `@mut T`, you will effectively convert borrowck's static checks into dynamic ones. This lets you use the e.g. send_map just like a Java Map or something else.
1 parent 2026359 commit 29003c7

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

src/libcore/core.rc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export hash;
5757
export cmp;
5858
export num;
5959
export path;
60-
export managed;
60+
export mutable;
6161
export flate;
6262
export unit;
6363
export uniq;
@@ -225,7 +225,7 @@ mod run;
225225
mod sys;
226226
mod unsafe;
227227

228-
mod managed;
228+
mod mutable;
229229

230230
mod flate;
231231

src/libcore/managed.rs renamed to src/libcore/mutable.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mutation when the data structure should be immutable.
1515
use util::with;
1616
use unsafe::transmute_immut;
1717

18-
export Managed;
18+
export Mut;
1919

2020
enum Mode { ReadOnly, Mutable, Immutable }
2121

@@ -24,18 +24,26 @@ struct Data<T> {
2424
priv mut mode: Mode
2525
}
2626

27-
type Managed<T> = @Data<T>;
27+
type Mut<T> = Data<T>;
2828

29-
fn Managed<T>(+t: T) -> Managed<T> {
30-
@Data {value: t, mode: ReadOnly}
29+
fn Mut<T>(+t: T) -> Mut<T> {
30+
Data {value: t, mode: ReadOnly}
31+
}
32+
33+
fn unwrap<T>(+m: Mut<T>) -> T {
34+
// Borrowck should prevent us from calling unwrap while the value
35+
// is in use, as that would be a move from a borrowed value.
36+
assert (m.mode as uint) == (ReadOnly as uint);
37+
let Data {value, mode: _} = m;
38+
return move value;
3139
}
3240

3341
impl<T> Data<T> {
3442
fn borrow_mut<R>(op: &fn(t: &mut T) -> R) -> R {
3543
match self.mode {
36-
Immutable => fail fmt!("%? currently immutable",
37-
self.value),
38-
ReadOnly | Mutable => {}
44+
Immutable => fail fmt!("%? currently immutable",
45+
self.value),
46+
ReadOnly | Mutable => {}
3947
}
4048

4149
do with(&mut self.mode, Mutable) {
@@ -64,7 +72,7 @@ impl<T> Data<T> {
6472
#[ignore(cfg(windows))]
6573
#[should_fail]
6674
fn test_mut_in_imm() {
67-
let m = Managed(1);
75+
let m = @Mut(1);
6876
do m.borrow_imm |_p| {
6977
do m.borrow_mut |_q| {
7078
// should not be permitted
@@ -76,7 +84,7 @@ fn test_mut_in_imm() {
7684
#[ignore(cfg(windows))]
7785
#[should_fail]
7886
fn test_imm_in_mut() {
79-
let m = Managed(1);
87+
let m = @Mut(1);
8088
do m.borrow_mut |_p| {
8189
do m.borrow_imm |_q| {
8290
// should not be permitted
@@ -86,7 +94,7 @@ fn test_imm_in_mut() {
8694

8795
#[test]
8896
fn test_const_in_mut() {
89-
let m = Managed(1);
97+
let m = @Mut(1);
9098
do m.borrow_mut |p| {
9199
do m.borrow_const |q| {
92100
assert *p == *q;
@@ -98,7 +106,7 @@ fn test_const_in_mut() {
98106

99107
#[test]
100108
fn test_mut_in_const() {
101-
let m = Managed(1);
109+
let m = @Mut(1);
102110
do m.borrow_const |p| {
103111
do m.borrow_mut |q| {
104112
assert *p == *q;
@@ -110,7 +118,7 @@ fn test_mut_in_const() {
110118

111119
#[test]
112120
fn test_imm_in_const() {
113-
let m = Managed(1);
121+
let m = @Mut(1);
114122
do m.borrow_const |p| {
115123
do m.borrow_imm |q| {
116124
assert *p == *q;
@@ -120,7 +128,7 @@ fn test_imm_in_const() {
120128

121129
#[test]
122130
fn test_const_in_imm() {
123-
let m = Managed(1);
131+
let m = @Mut(1);
124132
do m.borrow_imm |p| {
125133
do m.borrow_const |q| {
126134
assert *p == *q;
@@ -133,7 +141,7 @@ fn test_const_in_imm() {
133141
#[ignore(cfg(windows))]
134142
#[should_fail]
135143
fn test_mut_in_imm_in_const() {
136-
let m = Managed(1);
144+
let m = @Mut(1);
137145
do m.borrow_const |_p| {
138146
do m.borrow_imm |_q| {
139147
do m.borrow_mut |_r| {

src/libstd/map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use io::WriterUtil;
77
use to_str::ToStr;
8-
use managed::Managed;
8+
use mutable::Mut;
99
use send_map::linear::LinearMap;
1010

1111
use core::cmp::Eq;
@@ -463,7 +463,7 @@ fn hash_from_uints<V: Copy>(items: &[(uint, V)]) -> hashmap<uint, V> {
463463
}
464464

465465
// XXX Transitional
466-
impl<K: Eq IterBytes Hash Copy, V: Copy> Managed<LinearMap<K, V>>:
466+
impl<K: Eq IterBytes Hash Copy, V: Copy> @Mut<LinearMap<K, V>>:
467467
map<K, V> {
468468
pure fn size() -> uint {
469469
unchecked {

src/test/bench/core-map.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use std;
88
use std::map;
9-
use managed::Managed;
9+
use mutable::Mut;
1010
use send_map::linear::*;
1111
use io::WriterUtil;
1212

@@ -166,11 +166,11 @@ fn main(args: ~[~str]) {
166166
{
167167
let rng = rand::seeded_rng(copy seed);
168168
let mut results = empty_results();
169-
int_benchmarks::<Managed<LinearMap<uint, uint>>>(
170-
|| Managed(LinearMap()),
169+
int_benchmarks::<@Mut<LinearMap<uint, uint>>>(
170+
|| @Mut(LinearMap()),
171171
rng, num_keys, &mut results);
172-
str_benchmarks::<Managed<LinearMap<~str, uint>>>(
173-
|| Managed(LinearMap()),
172+
str_benchmarks::<@Mut<LinearMap<~str, uint>>>(
173+
|| @Mut(LinearMap()),
174174
rng, num_keys, &mut results);
175175
write_results("libstd::map::hashmap", &results);
176176
}

0 commit comments

Comments
 (0)