diff options
| author | Ben Blum <bblum@andrew.cmu.edu> | 2013-08-21 18:43:50 -0400 |
|---|---|---|
| committer | Ben Blum <bblum@andrew.cmu.edu> | 2013-08-21 18:57:22 -0400 |
| commit | 598072afa46d3c4fca7659c49ef3c9dc9f85787e (patch) | |
| tree | f009a9d0492e66ddca9dd26923aba64ebdf21e43 /src/libstd | |
| parent | b81f5c547c41571be3814ad4b2802f3305e12152 (diff) | |
| download | rust-598072afa46d3c4fca7659c49ef3c9dc9f85787e.tar.gz rust-598072afa46d3c4fca7659c49ef3c9dc9f85787e.zip | |
Don't fail in port.try_recv() the second time. Close #7800.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/cell.rs | 6 | ||||
| -rw-r--r-- | src/libstd/rt/comm.rs | 13 |
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 } } } |
