Strip down Cell functionality
This commit is contained in:
parent
8a26266f65
commit
5759f2fc57
4 changed files with 24 additions and 84 deletions
|
@ -24,7 +24,6 @@ use middle::graph::{Direction, NodeIndex};
|
||||||
use util::common::indenter;
|
use util::common::indenter;
|
||||||
use util::ppaux::{Repr};
|
use util::ppaux::{Repr};
|
||||||
|
|
||||||
use std::cell::Cell;
|
|
||||||
use std::hashmap::{HashMap, HashSet};
|
use std::hashmap::{HashMap, HashSet};
|
||||||
use std::uint;
|
use std::uint;
|
||||||
use std::vec;
|
use std::vec;
|
||||||
|
@ -106,16 +105,15 @@ pub struct RegionVarBindings {
|
||||||
undo_log: ~[UndoLogEntry],
|
undo_log: ~[UndoLogEntry],
|
||||||
|
|
||||||
// This contains the results of inference. It begins as an empty
|
// This contains the results of inference. It begins as an empty
|
||||||
// cell and only acquires a value after inference is complete.
|
// option and only acquires a value after inference is complete.
|
||||||
// We use a cell vs a mutable option to circumvent borrowck errors.
|
values: Option<~[VarValue]>,
|
||||||
values: Cell<~[VarValue]>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn RegionVarBindings(tcx: ty::ctxt) -> RegionVarBindings {
|
pub fn RegionVarBindings(tcx: ty::ctxt) -> RegionVarBindings {
|
||||||
RegionVarBindings {
|
RegionVarBindings {
|
||||||
tcx: tcx,
|
tcx: tcx,
|
||||||
var_origins: ~[],
|
var_origins: ~[],
|
||||||
values: Cell::new_empty(),
|
values: None,
|
||||||
constraints: HashMap::new(),
|
constraints: HashMap::new(),
|
||||||
lubs: HashMap::new(),
|
lubs: HashMap::new(),
|
||||||
glbs: HashMap::new(),
|
glbs: HashMap::new(),
|
||||||
|
@ -226,7 +224,7 @@ impl RegionVarBindings {
|
||||||
constraint: Constraint,
|
constraint: Constraint,
|
||||||
origin: SubregionOrigin) {
|
origin: SubregionOrigin) {
|
||||||
// cannot add constraints once regions are resolved
|
// cannot add constraints once regions are resolved
|
||||||
assert!(self.values.is_empty());
|
assert!(self.values.is_none());
|
||||||
|
|
||||||
debug!("RegionVarBindings: add_constraint({:?})", constraint);
|
debug!("RegionVarBindings: add_constraint({:?})", constraint);
|
||||||
|
|
||||||
|
@ -242,7 +240,7 @@ impl RegionVarBindings {
|
||||||
sub: Region,
|
sub: Region,
|
||||||
sup: Region) {
|
sup: Region) {
|
||||||
// cannot add constraints once regions are resolved
|
// cannot add constraints once regions are resolved
|
||||||
assert!(self.values.is_empty());
|
assert!(self.values.is_none());
|
||||||
|
|
||||||
debug!("RegionVarBindings: make_subregion({:?}, {:?})", sub, sup);
|
debug!("RegionVarBindings: make_subregion({:?}, {:?})", sub, sup);
|
||||||
match (sub, sup) {
|
match (sub, sup) {
|
||||||
|
@ -277,7 +275,7 @@ impl RegionVarBindings {
|
||||||
b: Region)
|
b: Region)
|
||||||
-> Region {
|
-> Region {
|
||||||
// cannot add constraints once regions are resolved
|
// cannot add constraints once regions are resolved
|
||||||
assert!(self.values.is_empty());
|
assert!(self.values.is_none());
|
||||||
|
|
||||||
debug!("RegionVarBindings: lub_regions({:?}, {:?})", a, b);
|
debug!("RegionVarBindings: lub_regions({:?}, {:?})", a, b);
|
||||||
match (a, b) {
|
match (a, b) {
|
||||||
|
@ -300,7 +298,7 @@ impl RegionVarBindings {
|
||||||
b: Region)
|
b: Region)
|
||||||
-> Region {
|
-> Region {
|
||||||
// cannot add constraints once regions are resolved
|
// cannot add constraints once regions are resolved
|
||||||
assert!(self.values.is_empty());
|
assert!(self.values.is_none());
|
||||||
|
|
||||||
debug!("RegionVarBindings: glb_regions({:?}, {:?})", a, b);
|
debug!("RegionVarBindings: glb_regions({:?}, {:?})", a, b);
|
||||||
match (a, b) {
|
match (a, b) {
|
||||||
|
@ -319,14 +317,14 @@ impl RegionVarBindings {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resolve_var(&mut self, rid: RegionVid) -> ty::Region {
|
pub fn resolve_var(&mut self, rid: RegionVid) -> ty::Region {
|
||||||
if self.values.is_empty() {
|
let v = match self.values {
|
||||||
self.tcx.sess.span_bug(
|
None => self.tcx.sess.span_bug(
|
||||||
self.var_origins[rid.to_uint()].span(),
|
self.var_origins[rid.to_uint()].span(),
|
||||||
format!("Attempt to resolve region variable before values have \
|
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({:?}={})={:?}",
|
debug!("RegionVarBindings: resolve_var({:?}={})={:?}",
|
||||||
rid, rid.to_uint(), v);
|
rid, rid.to_uint(), v);
|
||||||
match v {
|
match v {
|
||||||
|
@ -482,7 +480,7 @@ impl RegionVarBindings {
|
||||||
debug!("RegionVarBindings: resolve_regions()");
|
debug!("RegionVarBindings: resolve_regions()");
|
||||||
let mut errors = opt_vec::Empty;
|
let mut errors = opt_vec::Empty;
|
||||||
let v = self.infer_variable_values(&mut errors);
|
let v = self.infer_variable_values(&mut errors);
|
||||||
self.values.put_back(v);
|
self.values = Some(v);
|
||||||
errors
|
errors
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,12 +8,11 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
//! A mutable, nullable memory location
|
//! Runtime move semantics
|
||||||
|
|
||||||
#[missing_doc];
|
#[missing_doc];
|
||||||
|
|
||||||
use cast::transmute_mut;
|
use cast::transmute_mut;
|
||||||
use unstable::finally::Finally;
|
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -35,11 +34,6 @@ impl<T> Cell<T> {
|
||||||
Cell { value: Some(value) }
|
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.
|
/// Yields the value, failing if the cell is empty.
|
||||||
pub fn take(&self) -> T {
|
pub fn take(&self) -> T {
|
||||||
let this = unsafe { transmute_mut(self) };
|
let this = unsafe { transmute_mut(self) };
|
||||||
|
@ -56,34 +50,10 @@ impl<T> Cell<T> {
|
||||||
this.value.take()
|
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.
|
/// Returns true if the cell is empty and false if the cell is full.
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.value.is_none()
|
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]
|
#[test]
|
||||||
|
@ -93,38 +63,12 @@ fn test_basic() {
|
||||||
let value = value_cell.take();
|
let value = value_cell.take();
|
||||||
assert!(value == ~10);
|
assert!(value == ~10);
|
||||||
assert!(value_cell.is_empty());
|
assert!(value_cell.is_empty());
|
||||||
value_cell.put_back(value);
|
|
||||||
assert!(!value_cell.is_empty());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_fail]
|
#[should_fail]
|
||||||
fn test_take_empty() {
|
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();
|
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);
|
|
||||||
}
|
|
||||||
|
|
|
@ -81,8 +81,7 @@ fn main() {
|
||||||
let num_tasks = from_str::<uint>(args[1]).unwrap();
|
let num_tasks = from_str::<uint>(args[1]).unwrap();
|
||||||
let msg_per_task = from_str::<uint>(args[2]).unwrap();
|
let msg_per_task = from_str::<uint>(args[2]).unwrap();
|
||||||
|
|
||||||
let (num_chan, num_port) = init();
|
let (mut num_chan, num_port) = init();
|
||||||
let num_chan = Cell::new(num_chan);
|
|
||||||
|
|
||||||
let start = time::precise_time_s();
|
let start = time::precise_time_s();
|
||||||
|
|
||||||
|
@ -92,7 +91,7 @@ fn main() {
|
||||||
for i in range(1u, num_tasks) {
|
for i in range(1u, num_tasks) {
|
||||||
//error!("spawning %?", i);
|
//error!("spawning %?", i);
|
||||||
let (new_chan, num_port) = init();
|
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 num_port = Cell::new(num_port);
|
||||||
let new_future = do Future::spawn() {
|
let new_future = do Future::spawn() {
|
||||||
let num_chan = num_chan2.take();
|
let num_chan = num_chan2.take();
|
||||||
|
@ -100,11 +99,11 @@ fn main() {
|
||||||
thread_ring(i, msg_per_task, num_chan, num_port1)
|
thread_ring(i, msg_per_task, num_chan, num_port1)
|
||||||
};
|
};
|
||||||
futures.push(new_future);
|
futures.push(new_future);
|
||||||
num_chan.put_back(new_chan);
|
num_chan = new_chan;
|
||||||
};
|
};
|
||||||
|
|
||||||
// do our iteration
|
// 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
|
// synchronize
|
||||||
for f in futures.mut_iter() {
|
for f in futures.mut_iter() {
|
||||||
|
|
|
@ -77,8 +77,7 @@ fn main() {
|
||||||
let num_tasks = from_str::<uint>(args[1]).unwrap();
|
let num_tasks = from_str::<uint>(args[1]).unwrap();
|
||||||
let msg_per_task = from_str::<uint>(args[2]).unwrap();
|
let msg_per_task = from_str::<uint>(args[2]).unwrap();
|
||||||
|
|
||||||
let (num_chan, num_port) = init();
|
let (mut num_chan, num_port) = init();
|
||||||
let num_chan = Cell::new(num_chan);
|
|
||||||
|
|
||||||
let start = time::precise_time_s();
|
let start = time::precise_time_s();
|
||||||
|
|
||||||
|
@ -88,7 +87,7 @@ fn main() {
|
||||||
for i in range(1u, num_tasks) {
|
for i in range(1u, num_tasks) {
|
||||||
//error!("spawning %?", i);
|
//error!("spawning %?", i);
|
||||||
let (new_chan, num_port) = init();
|
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 num_port = Cell::new(num_port);
|
||||||
let new_future = do Future::spawn {
|
let new_future = do Future::spawn {
|
||||||
let num_chan = num_chan2.take();
|
let num_chan = num_chan2.take();
|
||||||
|
@ -96,11 +95,11 @@ fn main() {
|
||||||
thread_ring(i, msg_per_task, num_chan, num_port1)
|
thread_ring(i, msg_per_task, num_chan, num_port1)
|
||||||
};
|
};
|
||||||
futures.push(new_future);
|
futures.push(new_future);
|
||||||
num_chan.put_back(new_chan);
|
num_chan = new_chan;
|
||||||
};
|
};
|
||||||
|
|
||||||
// do our iteration
|
// 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
|
// synchronize
|
||||||
for f in futures.mut_iter() {
|
for f in futures.mut_iter() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue