]> git.lizzy.rs Git - rust.git/commitdiff
Strip down Cell functionality
authorSteven Fackler <sfackler@gmail.com>
Sat, 16 Nov 2013 21:26:15 +0000 (13:26 -0800)
committerSteven Fackler <sfackler@gmail.com>
Sat, 23 Nov 2013 05:19:53 +0000 (21:19 -0800)
src/librustc/middle/typeck/infer/region_inference/mod.rs
src/libstd/cell.rs
src/test/bench/msgsend-ring-mutex-arcs.rs
src/test/bench/msgsend-ring-rw-arcs.rs

index ff4b20d369b6f5bea46cfb44bcd96ec45f568ded..8af370792aee158f121c280ee56d4c64216e95da 100644 (file)
@@ -24,7 +24,6 @@
 use util::common::indenter;
 use util::ppaux::{Repr};
 
-use std::cell::Cell;
 use std::hashmap::{HashMap, HashSet};
 use std::uint;
 use std::vec;
@@ -106,16 +105,15 @@ pub struct RegionVarBindings {
     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(),
@@ -226,7 +224,7 @@ pub fn add_constraint(&mut self,
                           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);
 
@@ -242,7 +240,7 @@ pub fn make_subregion(&mut self,
                           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) {
@@ -277,7 +275,7 @@ pub fn lub_regions(&mut self,
                        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) {
@@ -300,7 +298,7 @@ pub fn glb_regions(&mut self,
                        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) {
@@ -319,14 +317,14 @@ pub fn glb_regions(&mut self,
     }
 
     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 {
@@ -482,7 +480,7 @@ pub fn resolve_regions(&mut self) -> OptVec<RegionResolutionError> {
         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
     }
 }
index 54849a44f6d3b96cdbb3249e8bd47a5b4f7e0570..b33a18ed95dd350a57ab3cbc023a9d80c02e8802 100644 (file)
@@ -8,12 +8,11 @@
 // 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::*;
 
 /*
@@ -35,11 +34,6 @@ pub fn new(value: T) -> Cell<T> {
         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) };
@@ -56,34 +50,10 @@ pub fn take_opt(&self) -> Option<T> {
         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]
@@ -93,38 +63,12 @@ fn test_basic() {
     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);
 }
index e4d45c6b74a9a05a74e61c3193c34f798ccb2c0a..f3b78ecd30c676423317d46182a516008171a78a 100644 (file)
@@ -81,8 +81,7 @@ fn main() {
     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();
 
@@ -92,7 +91,7 @@ fn main() {
     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();
@@ -100,11 +99,11 @@ fn main() {
             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() {
index d326fd81518eef2db11af6ac1f2c840685acf27a..47b1e5903280ee22324fb32e38ddf57bf5acc396 100644 (file)
@@ -77,8 +77,7 @@ fn main() {
     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();
 
@@ -88,7 +87,7 @@ fn main() {
     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();
@@ -96,11 +95,11 @@ fn main() {
             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() {