Skip to content

Commit 598072a

Browse files
committed
Don't fail in port.try_recv() the second time. Close rust-lang#7800.
1 parent b81f5c5 commit 598072a

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/libstd/cell.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ impl<T> Cell<T> {
5050
this.value.take_unwrap()
5151
}
5252

53+
/// Yields the value if the cell is full, or `None` if it is empty.
54+
pub fn take_opt(&self) -> Option<T> {
55+
let this = unsafe { transmute_mut(self) };
56+
this.value.take()
57+
}
58+
5359
/// Returns the value, failing if the cell is full.
5460
pub fn put_back(&self, value: T) {
5561
let this = unsafe { transmute_mut(self) };

src/libstd/rt/comm.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,14 @@ impl<T> GenericPort<T> for Port<T> {
499499
}
500500

501501
fn try_recv(&self) -> Option<T> {
502-
let pone = self.next.take();
503-
match pone.try_recv() {
504-
Some(StreamPayload { val, next }) => {
505-
self.next.put_back(next);
506-
Some(val)
502+
do self.next.take_opt().map_move_default(None) |pone| {
503+
match pone.try_recv() {
504+
Some(StreamPayload { val, next }) => {
505+
self.next.put_back(next);
506+
Some(val)
507+
}
508+
None => None
507509
}
508-
None => None
509510
}
510511
}
511512
}

0 commit comments

Comments
 (0)