Skip to content

Commit 3dd7c49

Browse files
committed
auto merge of #11251 : pcwalton/rust/remove-at-mut, r=pcwalton
r? @nikomatsakis for the borrow checker changes. Write guards are now eliminated.
2 parents 0ff6c12 + 8092153 commit 3dd7c49

File tree

277 files changed

+2431
-3477
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

277 files changed

+2431
-3477
lines changed

doc/tutorial.md

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,8 +1431,8 @@ For a more in-depth explanation of borrowed pointers, read the
14311431
## Freezing
14321432
14331433
Lending an immutable pointer to an object freezes it and prevents mutation.
1434-
`Freeze` objects have freezing enforced statically at compile-time. Examples
1435-
of non-`Freeze` types are `@mut` and [`RefCell<T>`][refcell].
1434+
`Freeze` objects have freezing enforced statically at compile-time. An example
1435+
of a non-`Freeze` type is [`RefCell<T>`][refcell].
14361436
14371437
~~~~
14381438
let mut x = 5;
@@ -1443,20 +1443,6 @@ let mut x = 5;
14431443
# x = 3;
14441444
~~~~
14451445
1446-
Mutable managed boxes handle freezing dynamically when any of their contents
1447-
are borrowed, and the task will fail if an attempt to modify them is made while
1448-
they are frozen:
1449-
1450-
~~~~
1451-
let x = @mut 5;
1452-
let y = x;
1453-
{
1454-
let z = &*y; // the managed box is now frozen
1455-
// modifying it through x or y will cause a task failure
1456-
}
1457-
// the box is now unfrozen again
1458-
~~~~
1459-
14601446
[refcell]: http://static.rust-lang.org/doc/master/std/cell/struct.RefCell.html
14611447
14621448
# Dereferencing pointers
@@ -1477,13 +1463,12 @@ assignments. Such an assignment modifies the value that the pointer
14771463
points to.
14781464
14791465
~~~
1480-
let managed = @mut 10;
1466+
let managed = @10;
14811467
let mut owned = ~20;
14821468

14831469
let mut value = 30;
14841470
let borrowed = &mut value;
14851471

1486-
*managed = *owned + 10;
14871472
*owned = *borrowed + 100;
14881473
*borrowed = *managed + 1000;
14891474
~~~
@@ -2113,8 +2098,7 @@ unless they contain managed boxes, managed closures, or borrowed pointers.
21132098
21142099
* `Freeze` - Constant (immutable) types.
21152100
These are types that do not contain anything intrinsically mutable.
2116-
Intrinsically mutable values include `@mut`
2117-
and `Cell` in the standard library.
2101+
Intrinsically mutable values include `Cell` in the standard library.
21182102
21192103
* `'static` - Non-borrowed types.
21202104
These are types that do not contain any data whose lifetime is bound to

src/libextra/arena.rs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@
3535
#[allow(missing_doc)];
3636

3737

38-
use list::{MutList, MutCons, MutNil};
38+
use list::{List, Cons, Nil};
39+
use list;
3940

4041
use std::at_vec;
4142
use std::cast::{transmute, transmute_mut, transmute_mut_region};
4243
use std::cast;
44+
use std::cell::{Cell, RefCell};
4345
use std::num;
4446
use std::ptr;
4547
use std::mem;
@@ -50,10 +52,11 @@ use std::unstable::intrinsics::{TyDesc, get_tydesc};
5052
// The way arena uses arrays is really deeply awful. The arrays are
5153
// allocated, and have capacities reserved, but the fill for the array
5254
// will always stay at 0.
55+
#[deriving(Clone)]
5356
struct Chunk {
54-
data: @[u8],
55-
fill: uint,
56-
is_pod: bool,
57+
data: RefCell<@[u8]>,
58+
fill: Cell<uint>,
59+
is_pod: Cell<bool>,
5760
}
5861

5962
#[no_freeze]
@@ -63,7 +66,7 @@ pub struct Arena {
6366
// access the head.
6467
priv head: Chunk,
6568
priv pod_head: Chunk,
66-
priv chunks: @mut MutList<Chunk>,
69+
priv chunks: RefCell<@List<Chunk>>,
6770
}
6871

6972
impl Arena {
@@ -75,7 +78,7 @@ impl Arena {
7578
Arena {
7679
head: chunk(initial_size, false),
7780
pod_head: chunk(initial_size, true),
78-
chunks: @mut MutNil,
81+
chunks: RefCell::new(@Nil),
7982
}
8083
}
8184
}
@@ -84,9 +87,9 @@ fn chunk(size: uint, is_pod: bool) -> Chunk {
8487
let mut v: @[u8] = @[];
8588
unsafe { at_vec::raw::reserve(&mut v, size); }
8689
Chunk {
87-
data: unsafe { cast::transmute(v) },
88-
fill: 0u,
89-
is_pod: is_pod,
90+
data: RefCell::new(unsafe { cast::transmute(v) }),
91+
fill: Cell::new(0u),
92+
is_pod: Cell::new(is_pod),
9093
}
9194
}
9295

@@ -95,8 +98,9 @@ impl Drop for Arena {
9598
fn drop(&mut self) {
9699
unsafe {
97100
destroy_chunk(&self.head);
98-
self.chunks.each(|chunk| {
99-
if !chunk.is_pod {
101+
102+
list::each(self.chunks.get(), |chunk| {
103+
if !chunk.is_pod.get() {
100104
destroy_chunk(chunk);
101105
}
102106
true
@@ -114,8 +118,11 @@ fn round_up_to(base: uint, align: uint) -> uint {
114118
// in it.
115119
unsafe fn destroy_chunk(chunk: &Chunk) {
116120
let mut idx = 0;
117-
let buf = chunk.data.as_ptr();
118-
let fill = chunk.fill;
121+
let buf = {
122+
let data = chunk.data.borrow();
123+
data.get().as_ptr()
124+
};
125+
let fill = chunk.fill.get();
119126

120127
while idx < fill {
121128
let tydesc_data: *uint = transmute(ptr::offset(buf, idx as int));
@@ -155,9 +162,9 @@ impl Arena {
155162
// Functions for the POD part of the arena
156163
fn alloc_pod_grow(&mut self, n_bytes: uint, align: uint) -> *u8 {
157164
// Allocate a new chunk.
158-
let chunk_size = at_vec::capacity(self.pod_head.data);
165+
let chunk_size = at_vec::capacity(self.pod_head.data.get());
159166
let new_min_chunk_size = num::max(n_bytes, chunk_size);
160-
self.chunks = @mut MutCons(self.pod_head, self.chunks);
167+
self.chunks.set(@Cons(self.pod_head.clone(), self.chunks.get()));
161168
self.pod_head =
162169
chunk(uint::next_power_of_two(new_min_chunk_size + 1u), true);
163170

@@ -168,17 +175,17 @@ impl Arena {
168175
fn alloc_pod_inner(&mut self, n_bytes: uint, align: uint) -> *u8 {
169176
unsafe {
170177
let this = transmute_mut_region(self);
171-
let start = round_up_to(this.pod_head.fill, align);
178+
let start = round_up_to(this.pod_head.fill.get(), align);
172179
let end = start + n_bytes;
173-
if end > at_vec::capacity(this.pod_head.data) {
180+
if end > at_vec::capacity(this.pod_head.data.get()) {
174181
return this.alloc_pod_grow(n_bytes, align);
175182
}
176-
this.pod_head.fill = end;
183+
this.pod_head.fill.set(end);
177184

178185
//debug!("idx = {}, size = {}, align = {}, fill = {}",
179-
// start, n_bytes, align, head.fill);
186+
// start, n_bytes, align, head.fill.get());
180187

181-
ptr::offset(this.pod_head.data.as_ptr(), start as int)
188+
ptr::offset(this.pod_head.data.get().as_ptr(), start as int)
182189
}
183190
}
184191

@@ -197,9 +204,9 @@ impl Arena {
197204
fn alloc_nonpod_grow(&mut self, n_bytes: uint, align: uint)
198205
-> (*u8, *u8) {
199206
// Allocate a new chunk.
200-
let chunk_size = at_vec::capacity(self.head.data);
207+
let chunk_size = at_vec::capacity(self.head.data.get());
201208
let new_min_chunk_size = num::max(n_bytes, chunk_size);
202-
self.chunks = @mut MutCons(self.head, self.chunks);
209+
self.chunks.set(@Cons(self.head.clone(), self.chunks.get()));
203210
self.head =
204211
chunk(uint::next_power_of_two(new_min_chunk_size + 1u), false);
205212

@@ -218,23 +225,23 @@ impl Arena {
218225
{
219226
let head = transmute_mut_region(&mut self.head);
220227

221-
tydesc_start = head.fill;
222-
after_tydesc = head.fill + mem::size_of::<*TyDesc>();
228+
tydesc_start = head.fill.get();
229+
after_tydesc = head.fill.get() + mem::size_of::<*TyDesc>();
223230
start = round_up_to(after_tydesc, align);
224231
end = start + n_bytes;
225232
}
226233

227-
if end > at_vec::capacity(self.head.data) {
234+
if end > at_vec::capacity(self.head.data.get()) {
228235
return self.alloc_nonpod_grow(n_bytes, align);
229236
}
230237

231238
let head = transmute_mut_region(&mut self.head);
232-
head.fill = round_up_to(end, mem::pref_align_of::<*TyDesc>());
239+
head.fill.set(round_up_to(end, mem::pref_align_of::<*TyDesc>()));
233240

234241
//debug!("idx = {}, size = {}, align = {}, fill = {}",
235242
// start, n_bytes, align, head.fill);
236243

237-
let buf = self.head.data.as_ptr();
244+
let buf = self.head.data.get().as_ptr();
238245
return (ptr::offset(buf, tydesc_start as int), ptr::offset(buf, start as int));
239246
}
240247
}

src/libextra/list.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@ pub enum List<T> {
1919
Nil,
2020
}
2121

22-
#[deriving(Eq)]
23-
#[allow(missing_doc)]
24-
pub enum MutList<T> {
25-
MutCons(T, @mut MutList<T>),
26-
MutNil,
27-
}
28-
2922
/// Create a list from a vector
3023
pub fn from_vec<T:Clone + 'static>(v: &[T]) -> @List<T> {
3124
v.rev_iter().fold(@Nil::<T>, |t, h| @Cons((*h).clone(), t))
@@ -158,26 +151,6 @@ pub fn each<T>(l: @List<T>, f: |&T| -> bool) -> bool {
158151
}
159152
}
160153

161-
impl<T> MutList<T> {
162-
/// Iterate over a mutable list
163-
pub fn each(@mut self, f: |&mut T| -> bool) -> bool {
164-
let mut cur = self;
165-
loop {
166-
let borrowed = &mut *cur;
167-
cur = match *borrowed {
168-
MutCons(ref mut hd, tl) => {
169-
if !f(hd) {
170-
return false;
171-
}
172-
tl
173-
}
174-
MutNil => break
175-
}
176-
}
177-
return true;
178-
}
179-
}
180-
181154
#[cfg(test)]
182155
mod tests {
183156
use list::*;

src/libextra/serialize.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -426,18 +426,6 @@ impl<D:Decoder,T:Decodable<D> + 'static> Decodable<D> for @T {
426426
}
427427
}
428428

429-
impl<S:Encoder,T:Encodable<S>> Encodable<S> for @mut T {
430-
fn encode(&self, s: &mut S) {
431-
(**self).encode(s)
432-
}
433-
}
434-
435-
impl<D:Decoder,T:Decodable<D> + 'static> Decodable<D> for @mut T {
436-
fn decode(d: &mut D) -> @mut T {
437-
@mut Decodable::decode(d)
438-
}
439-
}
440-
441429
impl<'a, S:Encoder,T:Encodable<S>> Encodable<S> for &'a [T] {
442430
fn encode(&self, s: &mut S) {
443431
s.emit_seq(self.len(), |s| {

src/libextra/term.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ impl<T: Writer> Terminal<T> {
113113
return Err(entry.unwrap_err());
114114
}
115115

116-
let ti = parse(entry.unwrap(), false);
116+
let mut file = entry.unwrap();
117+
let ti = parse(&mut file, false);
117118
if ti.is_err() {
118119
return Err(ti.unwrap_err());
119120
}

src/libextra/terminfo/searcher.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
/// Implement ncurses-compatible database discovery
1212
/// Does not support hashed database, only filesystem!
1313
14-
use std::{os, str};
15-
use std::os::getenv;
16-
use std::io;
1714
use std::io::File;
15+
use std::os::getenv;
16+
use std::{os, str};
1817

1918
/// Return path to database entry for `term`
2019
pub fn get_dbpath_for_term(term: &str) -> Option<~Path> {
@@ -74,9 +73,14 @@ pub fn get_dbpath_for_term(term: &str) -> Option<~Path> {
7473
}
7574

7675
/// Return open file for `term`
77-
pub fn open(term: &str) -> Result<@mut io::Reader, ~str> {
76+
pub fn open(term: &str) -> Result<File, ~str> {
7877
match get_dbpath_for_term(term) {
79-
Some(x) => Ok(@mut File::open(x) as @mut io::Reader),
78+
Some(x) => {
79+
match File::open(x) {
80+
Some(file) => Ok(file),
81+
None => Err(~"error opening file"),
82+
}
83+
}
8084
None => Err(format!("could not find terminfo entry for {}", term))
8185
}
8286
}

src/libextra/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,8 @@ impl MetricMap {
949949
950950
/// Write MetricDiff to a file.
951951
pub fn save(&self, p: &Path) {
952-
self.to_json().to_pretty_writer(@mut File::create(p) as @mut io::Writer);
952+
let mut file = File::create(p);
953+
self.to_json().to_pretty_writer(&mut file)
953954
}
954955
955956
/// Compare against another MetricMap. Optionally compare all

src/libextra/uuid.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,8 +796,8 @@ mod test {
796796
use serialize::{Encodable, Decodable};
797797

798798
let u = Uuid::new_v4();
799-
let wr = @mut MemWriter::new();
800-
u.encode(&mut ebml::writer::Encoder(wr));
799+
let mut wr = MemWriter::new();
800+
u.encode(&mut ebml::writer::Encoder(&mut wr));
801801
let doc = ebml::reader::Doc(wr.inner_ref().as_slice());
802802
let u2 = Decodable::decode(&mut ebml::reader::Decoder(doc));
803803
assert_eq!(u, u2);

src/libextra/workcache.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ impl Database {
173173

174174
// FIXME #4330: This should have &mut self and should set self.db_dirty to false.
175175
fn save(&self) {
176-
let f = @mut File::create(&self.db_filename);
177-
self.db_cache.to_json().to_pretty_writer(f as @mut io::Writer);
176+
let mut f = File::create(&self.db_filename);
177+
self.db_cache.to_json().to_pretty_writer(&mut f);
178178
}
179179

180180
fn load(&mut self) {
@@ -184,14 +184,16 @@ impl Database {
184184
Err(e) => fail!("Couldn't load workcache database {}: {}",
185185
self.db_filename.display(),
186186
e.desc),
187-
Ok(r) =>
188-
match json::from_reader(@mut r.unwrap() as @mut io::Reader) {
187+
Ok(r) => {
188+
let mut stream = r.unwrap();
189+
match json::from_reader(&mut stream) {
189190
Err(e) => fail!("Couldn't parse workcache database (from file {}): {}",
190191
self.db_filename.display(), e.to_str()),
191192
Ok(r) => {
192193
let mut decoder = json::Decoder::new(r);
193194
self.db_cache = Decodable::decode(&mut decoder);
194195
}
196+
}
195197
}
196198
}
197199
}

0 commit comments

Comments
 (0)