about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/libstd/cell.rs6
-rw-r--r--src/libstd/rt/comm.rs13
2 files changed, 13 insertions, 6 deletions
diff --git a/src/libstd/cell.rs b/src/libstd/cell.rs
index 372effad61d..a3d06378ed4 100644
--- a/src/libstd/cell.rs
+++ b/src/libstd/cell.rs
@@ -50,6 +50,12 @@ impl<T> Cell<T> {
         this.value.take_unwrap()
     }
 
+    /// Yields the value if the cell is full, or `None` if it is empty.
+    pub fn take_opt(&self) -> Option<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) };
diff --git a/src/libstd/rt/comm.rs b/src/libstd/rt/comm.rs
index 49cf8c239b7..c6b87d88979 100644
--- a/src/libstd/rt/comm.rs
+++ b/src/libstd/rt/comm.rs
@@ -499,13 +499,14 @@ impl<T> GenericPort<T> for Port<T> {
     }
 
     fn try_recv(&self) -> Option<T> {
-        let pone = self.next.take();
-        match pone.try_recv() {
-            Some(StreamPayload { val, next }) => {
-                self.next.put_back(next);
-                Some(val)
+        do self.next.take_opt().map_move_default(None) |pone| {
+            match pone.try_recv() {
+                Some(StreamPayload { val, next }) => {
+                    self.next.put_back(next);
+                    Some(val)
+                }
+                None => None
             }
-            None => None
         }
     }
 }