Skip to content

Commit 5759f2f

Browse files
committed
Strip down Cell functionality
1 parent 8a26266 commit 5759f2f

File tree

4 files changed

+24
-84
lines changed

4 files changed

+24
-84
lines changed

src/librustc/middle/typeck/infer/region_inference/mod.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use middle::graph::{Direction, NodeIndex};
2424
use util::common::indenter;
2525
use util::ppaux::{Repr};
2626

27-
use std::cell::Cell;
2827
use std::hashmap::{HashMap, HashSet};
2928
use std::uint;
3029
use std::vec;
@@ -106,16 +105,15 @@ pub struct RegionVarBindings {
106105
undo_log: ~[UndoLogEntry],
107106

108107
// This contains the results of inference. It begins as an empty
109-
// cell and only acquires a value after inference is complete.
110-
// We use a cell vs a mutable option to circumvent borrowck errors.
111-
values: Cell<~[VarValue]>,
108+
// option and only acquires a value after inference is complete.
109+
values: Option<~[VarValue]>,
112110
}
113111

114112
pub fn RegionVarBindings(tcx: ty::ctxt) -> RegionVarBindings {
115113
RegionVarBindings {
116114
tcx: tcx,
117115
var_origins: ~[],
118-
values: Cell::new_empty(),
116+
values: None,
119117
constraints: HashMap::new(),
120118
lubs: HashMap::new(),
121119
glbs: HashMap::new(),
@@ -226,7 +224,7 @@ impl RegionVarBindings {
226224
constraint: Constraint,
227225
origin: SubregionOrigin) {
228226
// cannot add constraints once regions are resolved
229-
assert!(self.values.is_empty());
227+
assert!(self.values.is_none());
230228

231229
debug!("RegionVarBindings: add_constraint({:?})", constraint);
232230

@@ -242,7 +240,7 @@ impl RegionVarBindings {
242240
sub: Region,
243241
sup: Region) {
244242
// cannot add constraints once regions are resolved
245-
assert!(self.values.is_empty());
243+
assert!(self.values.is_none());
246244

247245
debug!("RegionVarBindings: make_subregion({:?}, {:?})", sub, sup);
248246
match (sub, sup) {
@@ -277,7 +275,7 @@ impl RegionVarBindings {
277275
b: Region)
278276
-> Region {
279277
// cannot add constraints once regions are resolved
280-
assert!(self.values.is_empty());
278+
assert!(self.values.is_none());
281279

282280
debug!("RegionVarBindings: lub_regions({:?}, {:?})", a, b);
283281
match (a, b) {
@@ -300,7 +298,7 @@ impl RegionVarBindings {
300298
b: Region)
301299
-> Region {
302300
// cannot add constraints once regions are resolved
303-
assert!(self.values.is_empty());
301+
assert!(self.values.is_none());
304302

305303
debug!("RegionVarBindings: glb_regions({:?}, {:?})", a, b);
306304
match (a, b) {
@@ -319,14 +317,14 @@ impl RegionVarBindings {
319317
}
320318

321319
pub fn resolve_var(&mut self, rid: RegionVid) -> ty::Region {
322-
if self.values.is_empty() {
323-
self.tcx.sess.span_bug(
320+
let v = match self.values {
321+
None => self.tcx.sess.span_bug(
324322
self.var_origins[rid.to_uint()].span(),
325323
format!("Attempt to resolve region variable before values have \
326-
been computed!"));
327-
}
324+
been computed!")),
325+
Some(ref values) => values[rid.to_uint()]
326+
};
328327

329-
let v = self.values.with_ref(|values| values[rid.to_uint()]);
330328
debug!("RegionVarBindings: resolve_var({:?}={})={:?}",
331329
rid, rid.to_uint(), v);
332330
match v {
@@ -482,7 +480,7 @@ impl RegionVarBindings {
482480
debug!("RegionVarBindings: resolve_regions()");
483481
let mut errors = opt_vec::Empty;
484482
let v = self.infer_variable_values(&mut errors);
485-
self.values.put_back(v);
483+
self.values = Some(v);
486484
errors
487485
}
488486
}

src/libstd/cell.rs

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! A mutable, nullable memory location
11+
//! Runtime move semantics
1212
1313
#[missing_doc];
1414

1515
use cast::transmute_mut;
16-
use unstable::finally::Finally;
1716
use prelude::*;
1817

1918
/*
@@ -35,11 +34,6 @@ impl<T> Cell<T> {
3534
Cell { value: Some(value) }
3635
}
3736

38-
/// Creates a new empty cell with no value inside.
39-
pub fn new_empty() -> Cell<T> {
40-
Cell { value: None }
41-
}
42-
4337
/// Yields the value, failing if the cell is empty.
4438
pub fn take(&self) -> T {
4539
let this = unsafe { transmute_mut(self) };
@@ -56,34 +50,10 @@ impl<T> Cell<T> {
5650
this.value.take()
5751
}
5852

59-
/// Returns the value, failing if the cell is full.
60-
pub fn put_back(&self, value: T) {
61-
let this = unsafe { transmute_mut(self) };
62-
if !this.is_empty() {
63-
fail!("attempt to put a value back into a full cell");
64-
}
65-
this.value = Some(value);
66-
}
67-
6853
/// Returns true if the cell is empty and false if the cell is full.
6954
pub fn is_empty(&self) -> bool {
7055
self.value.is_none()
7156
}
72-
73-
/// Calls a closure with a reference to the value.
74-
pub fn with_ref<R>(&self, op: |v: &T| -> R) -> R {
75-
do self.with_mut_ref |ptr| { op(ptr) }
76-
}
77-
78-
/// Calls a closure with a mutable reference to the value.
79-
pub fn with_mut_ref<R>(&self, op: |v: &mut T| -> R) -> R {
80-
let mut v = Some(self.take());
81-
do (|| {
82-
op(v.get_mut_ref())
83-
}).finally {
84-
self.put_back(v.take_unwrap());
85-
}
86-
}
8757
}
8858

8959
#[test]
@@ -93,38 +63,12 @@ fn test_basic() {
9363
let value = value_cell.take();
9464
assert!(value == ~10);
9565
assert!(value_cell.is_empty());
96-
value_cell.put_back(value);
97-
assert!(!value_cell.is_empty());
9866
}
9967

10068
#[test]
10169
#[should_fail]
10270
fn test_take_empty() {
103-
let value_cell: Cell<~int> = Cell::new_empty();
71+
let value_cell: Cell<~int> = Cell::new(~0);
72+
value_cell.take();
10473
value_cell.take();
105-
}
106-
107-
#[test]
108-
#[should_fail]
109-
fn test_put_back_non_empty() {
110-
let value_cell = Cell::new(~10);
111-
value_cell.put_back(~20);
112-
}
113-
114-
#[test]
115-
fn test_with_ref() {
116-
let good = 6;
117-
let c = Cell::new(~[1, 2, 3, 4, 5, 6]);
118-
let l = do c.with_ref() |v| { v.len() };
119-
assert_eq!(l, good);
120-
}
121-
122-
#[test]
123-
fn test_with_mut_ref() {
124-
let good = ~[1, 2, 3];
125-
let v = ~[1, 2];
126-
let c = Cell::new(v);
127-
do c.with_mut_ref() |v| { v.push(3); }
128-
let v = c.take();
129-
assert_eq!(v, good);
13074
}

src/test/bench/msgsend-ring-mutex-arcs.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ fn main() {
8181
let num_tasks = from_str::<uint>(args[1]).unwrap();
8282
let msg_per_task = from_str::<uint>(args[2]).unwrap();
8383

84-
let (num_chan, num_port) = init();
85-
let num_chan = Cell::new(num_chan);
84+
let (mut num_chan, num_port) = init();
8685

8786
let start = time::precise_time_s();
8887

@@ -92,19 +91,19 @@ fn main() {
9291
for i in range(1u, num_tasks) {
9392
//error!("spawning %?", i);
9493
let (new_chan, num_port) = init();
95-
let num_chan2 = Cell::new(num_chan.take());
94+
let num_chan2 = Cell::new(num_chan);
9695
let num_port = Cell::new(num_port);
9796
let new_future = do Future::spawn() {
9897
let num_chan = num_chan2.take();
9998
let num_port1 = num_port.take();
10099
thread_ring(i, msg_per_task, num_chan, num_port1)
101100
};
102101
futures.push(new_future);
103-
num_chan.put_back(new_chan);
102+
num_chan = new_chan;
104103
};
105104

106105
// do our iteration
107-
thread_ring(0, msg_per_task, num_chan.take(), num_port);
106+
thread_ring(0, msg_per_task, num_chan, num_port);
108107

109108
// synchronize
110109
for f in futures.mut_iter() {

src/test/bench/msgsend-ring-rw-arcs.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ fn main() {
7777
let num_tasks = from_str::<uint>(args[1]).unwrap();
7878
let msg_per_task = from_str::<uint>(args[2]).unwrap();
7979

80-
let (num_chan, num_port) = init();
81-
let num_chan = Cell::new(num_chan);
80+
let (mut num_chan, num_port) = init();
8281

8382
let start = time::precise_time_s();
8483

@@ -88,19 +87,19 @@ fn main() {
8887
for i in range(1u, num_tasks) {
8988
//error!("spawning %?", i);
9089
let (new_chan, num_port) = init();
91-
let num_chan2 = Cell::new(num_chan.take());
90+
let num_chan2 = Cell::new(num_chan);
9291
let num_port = Cell::new(num_port);
9392
let new_future = do Future::spawn {
9493
let num_chan = num_chan2.take();
9594
let num_port1 = num_port.take();
9695
thread_ring(i, msg_per_task, num_chan, num_port1)
9796
};
9897
futures.push(new_future);
99-
num_chan.put_back(new_chan);
98+
num_chan = new_chan;
10099
};
101100

102101
// do our iteration
103-
thread_ring(0, msg_per_task, num_chan.take(), num_port);
102+
thread_ring(0, msg_per_task, num_chan, num_port);
104103

105104
// synchronize
106105
for f in futures.mut_iter() {

0 commit comments

Comments
 (0)