use util::common::indenter;
use util::ppaux::{Repr};
-use std::cell::Cell;
use std::hashmap::{HashMap, HashSet};
use std::uint;
use std::vec;
undo_log: ~[UndoLogEntry],
// This contains the results of inference. It begins as an empty
- // cell and only acquires a value after inference is complete.
- // We use a cell vs a mutable option to circumvent borrowck errors.
- values: Cell<~[VarValue]>,
+ // option and only acquires a value after inference is complete.
+ values: Option<~[VarValue]>,
}
pub fn RegionVarBindings(tcx: ty::ctxt) -> RegionVarBindings {
RegionVarBindings {
tcx: tcx,
var_origins: ~[],
- values: Cell::new_empty(),
+ values: None,
constraints: HashMap::new(),
lubs: HashMap::new(),
glbs: HashMap::new(),
constraint: Constraint,
origin: SubregionOrigin) {
// cannot add constraints once regions are resolved
- assert!(self.values.is_empty());
+ assert!(self.values.is_none());
debug!("RegionVarBindings: add_constraint({:?})", constraint);
sub: Region,
sup: Region) {
// cannot add constraints once regions are resolved
- assert!(self.values.is_empty());
+ assert!(self.values.is_none());
debug!("RegionVarBindings: make_subregion({:?}, {:?})", sub, sup);
match (sub, sup) {
b: Region)
-> Region {
// cannot add constraints once regions are resolved
- assert!(self.values.is_empty());
+ assert!(self.values.is_none());
debug!("RegionVarBindings: lub_regions({:?}, {:?})", a, b);
match (a, b) {
b: Region)
-> Region {
// cannot add constraints once regions are resolved
- assert!(self.values.is_empty());
+ assert!(self.values.is_none());
debug!("RegionVarBindings: glb_regions({:?}, {:?})", a, b);
match (a, b) {
}
pub fn resolve_var(&mut self, rid: RegionVid) -> ty::Region {
- if self.values.is_empty() {
- self.tcx.sess.span_bug(
+ let v = match self.values {
+ None => self.tcx.sess.span_bug(
self.var_origins[rid.to_uint()].span(),
format!("Attempt to resolve region variable before values have \
- been computed!"));
- }
+ been computed!")),
+ Some(ref values) => values[rid.to_uint()]
+ };
- let v = self.values.with_ref(|values| values[rid.to_uint()]);
debug!("RegionVarBindings: resolve_var({:?}={})={:?}",
rid, rid.to_uint(), v);
match v {
debug!("RegionVarBindings: resolve_regions()");
let mut errors = opt_vec::Empty;
let v = self.infer_variable_values(&mut errors);
- self.values.put_back(v);
+ self.values = Some(v);
errors
}
}
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-//! A mutable, nullable memory location
+//! Runtime move semantics
#[missing_doc];
use cast::transmute_mut;
-use unstable::finally::Finally;
use prelude::*;
/*
Cell { value: Some(value) }
}
- /// Creates a new empty cell with no value inside.
- pub fn new_empty() -> Cell<T> {
- Cell { value: None }
- }
-
/// Yields the value, failing if the cell is empty.
pub fn take(&self) -> T {
let this = unsafe { transmute_mut(self) };
this.value.take()
}
- /// Returns the value, failing if the cell is full.
- pub fn put_back(&self, value: T) {
- let this = unsafe { transmute_mut(self) };
- if !this.is_empty() {
- fail!("attempt to put a value back into a full cell");
- }
- this.value = Some(value);
- }
-
/// Returns true if the cell is empty and false if the cell is full.
pub fn is_empty(&self) -> bool {
self.value.is_none()
}
-
- /// Calls a closure with a reference to the value.
- pub fn with_ref<R>(&self, op: |v: &T| -> R) -> R {
- do self.with_mut_ref |ptr| { op(ptr) }
- }
-
- /// Calls a closure with a mutable reference to the value.
- pub fn with_mut_ref<R>(&self, op: |v: &mut T| -> R) -> R {
- let mut v = Some(self.take());
- do (|| {
- op(v.get_mut_ref())
- }).finally {
- self.put_back(v.take_unwrap());
- }
- }
}
#[test]
let value = value_cell.take();
assert!(value == ~10);
assert!(value_cell.is_empty());
- value_cell.put_back(value);
- assert!(!value_cell.is_empty());
}
#[test]
#[should_fail]
fn test_take_empty() {
- let value_cell: Cell<~int> = Cell::new_empty();
+ let value_cell: Cell<~int> = Cell::new(~0);
+ value_cell.take();
value_cell.take();
-}
-
-#[test]
-#[should_fail]
-fn test_put_back_non_empty() {
- let value_cell = Cell::new(~10);
- value_cell.put_back(~20);
-}
-
-#[test]
-fn test_with_ref() {
- let good = 6;
- let c = Cell::new(~[1, 2, 3, 4, 5, 6]);
- let l = do c.with_ref() |v| { v.len() };
- assert_eq!(l, good);
-}
-
-#[test]
-fn test_with_mut_ref() {
- let good = ~[1, 2, 3];
- let v = ~[1, 2];
- let c = Cell::new(v);
- do c.with_mut_ref() |v| { v.push(3); }
- let v = c.take();
- assert_eq!(v, good);
}
let num_tasks = from_str::<uint>(args[1]).unwrap();
let msg_per_task = from_str::<uint>(args[2]).unwrap();
- let (num_chan, num_port) = init();
- let num_chan = Cell::new(num_chan);
+ let (mut num_chan, num_port) = init();
let start = time::precise_time_s();
for i in range(1u, num_tasks) {
//error!("spawning %?", i);
let (new_chan, num_port) = init();
- let num_chan2 = Cell::new(num_chan.take());
+ let num_chan2 = Cell::new(num_chan);
let num_port = Cell::new(num_port);
let new_future = do Future::spawn() {
let num_chan = num_chan2.take();
thread_ring(i, msg_per_task, num_chan, num_port1)
};
futures.push(new_future);
- num_chan.put_back(new_chan);
+ num_chan = new_chan;
};
// do our iteration
- thread_ring(0, msg_per_task, num_chan.take(), num_port);
+ thread_ring(0, msg_per_task, num_chan, num_port);
// synchronize
for f in futures.mut_iter() {
let num_tasks = from_str::<uint>(args[1]).unwrap();
let msg_per_task = from_str::<uint>(args[2]).unwrap();
- let (num_chan, num_port) = init();
- let num_chan = Cell::new(num_chan);
+ let (mut num_chan, num_port) = init();
let start = time::precise_time_s();
for i in range(1u, num_tasks) {
//error!("spawning %?", i);
let (new_chan, num_port) = init();
- let num_chan2 = Cell::new(num_chan.take());
+ let num_chan2 = Cell::new(num_chan);
let num_port = Cell::new(num_port);
let new_future = do Future::spawn {
let num_chan = num_chan2.take();
thread_ring(i, msg_per_task, num_chan, num_port1)
};
futures.push(new_future);
- num_chan.put_back(new_chan);
+ num_chan = new_chan;
};
// do our iteration
- thread_ring(0, msg_per_task, num_chan.take(), num_port);
+ thread_ring(0, msg_per_task, num_chan, num_port);
// synchronize
for f in futures.mut_iter() {