forked from rust-lang/rfcs
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rust-lang#151 from stepancheg/catch-unwind
Stream::catch_unwind
- Loading branch information
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::prelude::v1::*; | ||
use std::any::Any; | ||
use std::panic::{catch_unwind, UnwindSafe, AssertUnwindSafe}; | ||
use std::mem; | ||
|
||
use super::super::{Poll, Async}; | ||
use super::Stream; | ||
|
||
/// Stream for the `catch_unwind` combinator. | ||
/// | ||
/// This is created by this `Stream::catch_unwind` method. | ||
#[must_use = "streams do nothing unless polled"] | ||
pub struct CatchUnwind<S> where S: Stream { | ||
state: CatchUnwindState<S>, | ||
} | ||
|
||
pub fn new<S>(stream: S) -> CatchUnwind<S> | ||
where S: Stream + UnwindSafe, | ||
{ | ||
CatchUnwind { | ||
state: CatchUnwindState::Stream(stream), | ||
} | ||
} | ||
|
||
enum CatchUnwindState<S> { | ||
Stream(S), | ||
Eof, | ||
Done, | ||
} | ||
|
||
impl<S> Stream for CatchUnwind<S> | ||
where S: Stream + UnwindSafe, | ||
{ | ||
type Item = Result<S::Item, S::Error>; | ||
type Error = Box<Any + Send>; | ||
|
||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> { | ||
let mut stream = match mem::replace(&mut self.state, CatchUnwindState::Eof) { | ||
CatchUnwindState::Done => panic!("cannot poll after eof"), | ||
CatchUnwindState::Eof => { | ||
self.state = CatchUnwindState::Done; | ||
return Ok(Async::Ready(None)); | ||
} | ||
CatchUnwindState::Stream(stream) => stream, | ||
}; | ||
let res = catch_unwind(|| (stream.poll(), stream)); | ||
match res { | ||
Err(e) => Err(e), // and state is already Eof | ||
Ok((poll, stream)) => { | ||
self.state = CatchUnwindState::Stream(stream); | ||
match poll { | ||
Err(e) => Ok(Async::Ready(Some(Err(e)))), | ||
Ok(Async::NotReady) => Ok(Async::NotReady), | ||
Ok(Async::Ready(Some(r))) => Ok(Async::Ready(Some(Ok(r)))), | ||
Ok(Async::Ready(None)) => Ok(Async::Ready(None)), | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<S: Stream> Stream for AssertUnwindSafe<S> { | ||
type Item = S::Item; | ||
type Error = S::Error; | ||
|
||
fn poll(&mut self) -> Poll<Option<S::Item>, S::Error> { | ||
self.0.poll() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
extern crate futures; | ||
|
||
use futures::stream; | ||
use futures::stream::Stream; | ||
|
||
#[test] | ||
fn panic_in_the_middle_of_the_stream() { | ||
let stream = stream::iter::<_, Option<i32>, bool>(vec![ | ||
Some(10), None, Some(11)].into_iter().map(Ok)); | ||
|
||
// panic on second element | ||
let stream_panicking = stream.map(|o| o.unwrap()); | ||
let mut iter = stream_panicking.catch_unwind().wait(); | ||
|
||
assert_eq!(Ok(10), iter.next().unwrap().ok().unwrap()); | ||
assert!(iter.next().unwrap().is_err()); | ||
assert!(iter.next().is_none()); | ||
} | ||
|
||
#[test] | ||
fn no_panic() { | ||
let stream = stream::iter::<_, _, bool>(vec![ | ||
10, 11, 12].into_iter().map(Ok)); | ||
|
||
let mut iter = stream.catch_unwind().wait(); | ||
|
||
assert_eq!(Ok(10), iter.next().unwrap().ok().unwrap()); | ||
assert_eq!(Ok(11), iter.next().unwrap().ok().unwrap()); | ||
assert_eq!(Ok(12), iter.next().unwrap().ok().unwrap()); | ||
assert!(iter.next().is_none()); | ||
} |