Skip to content

Commit

Permalink
Use ? operator more
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e authored and cramertj committed May 14, 2019
1 parent 7b5c732 commit 7c84697
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 52 deletions.
5 changes: 1 addition & 4 deletions futures-util/src/future/catch_unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ impl<Fut> Future for CatchUnwind<Fut>
type Output = Result<Fut::Output, Box<dyn Any + Send>>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match catch_unwind(AssertUnwindSafe(|| self.future().poll(cx))) {
Ok(res) => res.map(Ok),
Err(e) => Poll::Ready(Err(e))
}
catch_unwind(AssertUnwindSafe(|| self.future().poll(cx)))?.map(Ok)
}
}
8 changes: 2 additions & 6 deletions futures-util/src/sink/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ impl<Si: Sink<Item>, Item> Buffer<Si, Item> {
) -> Poll<Result<(), Si::SinkError>> {
ready!(self.as_mut().sink().poll_ready(cx))?;
while let Some(item) = self.as_mut().buf().pop_front() {
if let Err(e) = self.as_mut().sink().start_send(item) {
return Poll::Ready(Err(e));
}
self.as_mut().sink().start_send(item)?;
if !self.buf.is_empty() {
ready!(self.as_mut().sink().poll_ready(cx))?;
}
Expand Down Expand Up @@ -91,9 +89,7 @@ impl<Si: Sink<Item>, Item> Sink<Item> for Buffer<Si, Item> {
return self.as_mut().sink().poll_ready(cx);
}

if let Poll::Ready(Err(e)) = self.as_mut().try_empty_buffer(cx) {
return Poll::Ready(Err(e));
}
let _ = self.as_mut().try_empty_buffer(cx)?;

if self.buf.len() >= self.capacity {
Poll::Pending
Expand Down
9 changes: 2 additions & 7 deletions futures-util/src/sink/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,8 @@ impl<Si: Sink<Item> + Unpin + ?Sized, Item> Future for Send<'_, Si, Item> {
let this = &mut *self;
if let Some(item) = this.item.take() {
let mut sink = Pin::new(&mut this.sink);
match sink.as_mut().poll_ready(cx) {
Poll::Ready(Ok(())) => {
if let Err(e) = sink.as_mut().start_send(item) {
return Poll::Ready(Err(e));
}
}
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
match sink.as_mut().poll_ready(cx)? {
Poll::Ready(()) => sink.as_mut().start_send(item)?,
Poll::Pending => {
this.item = Some(item);
return Poll::Pending;
Expand Down
5 changes: 2 additions & 3 deletions futures-util/src/sink/send_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ where
item: St::Item,
) -> Poll<Result<(), Si::SinkError>> {
debug_assert!(self.buffered.is_none());
match Pin::new(&mut self.sink).poll_ready(cx) {
Poll::Ready(Ok(())) => {
match Pin::new(&mut self.sink).poll_ready(cx)? {
Poll::Ready(()) => {
Poll::Ready(Pin::new(&mut self.sink).start_send(item))
}
Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
Poll::Pending => {
self.buffered = Some(item);
Poll::Pending
Expand Down
14 changes: 4 additions & 10 deletions futures-util/src/sink/with_flat_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,15 @@ where
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::SinkError>> {
match self.as_mut().try_empty_stream(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(Ok(())) => self.as_mut().sink().poll_flush(cx),
Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
}
ready!(self.as_mut().try_empty_stream(cx)?);
self.as_mut().sink().poll_flush(cx)
}

fn poll_close(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::SinkError>> {
match self.as_mut().try_empty_stream(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(Ok(())) => self.as_mut().sink().poll_close(cx),
Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
}
ready!(self.as_mut().try_empty_stream(cx)?);
self.as_mut().sink().poll_close(cx)
}
}
5 changes: 2 additions & 3 deletions futures-util/src/stream/forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ where
}

loop {
match self.as_mut().stream().poll_next(cx) {
Poll::Ready(Some(Ok(item))) =>
match self.as_mut().stream().poll_next(cx)? {
Poll::Ready(Some(item)) =>
ready!(self.as_mut().try_start_send(cx, item))?,
Poll::Ready(Some(Err(e))) => return Poll::Ready(Err(e)),
Poll::Ready(None) => {
ready!(self.as_mut().sink().as_pin_mut().expect(INVALID_POLL).poll_close(cx))?;
self.as_mut().sink().set(None);
Expand Down
5 changes: 2 additions & 3 deletions futures-util/src/try_stream/try_buffer_unordered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ impl<St> Stream for TryBufferUnordered<St>
// First up, try to spawn off as many futures as possible by filling up
// our slab of futures. Propagate errors from the stream immediately.
while self.in_progress_queue.len() < self.max {
match self.as_mut().stream().poll_next(cx) {
Poll::Ready(Some(Ok(fut))) => self.as_mut().in_progress_queue().push(fut.into_future()),
Poll::Ready(Some(Err(e))) => return Poll::Ready(Some(Err(e))),
match self.as_mut().stream().poll_next(cx)? {
Poll::Ready(Some(fut)) => self.as_mut().in_progress_queue().push(fut.into_future()),
Poll::Ready(None) | Poll::Pending => break,
}
}
Expand Down
5 changes: 2 additions & 3 deletions futures-util/src/try_stream/try_collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ impl<St, C> Future for TryCollect<St, C>
cx: &mut Context<'_>,
) -> Poll<Self::Output> {
loop {
match ready!(self.as_mut().stream().try_poll_next(cx)) {
Some(Ok(x)) => self.as_mut().items().extend(Some(x)),
Some(Err(e)) => return Poll::Ready(Err(e)),
match ready!(self.as_mut().stream().try_poll_next(cx)?) {
Some(x) => self.as_mut().items().extend(Some(x)),
None => return Poll::Ready(Ok(self.as_mut().finish())),
}
}
Expand Down
5 changes: 2 additions & 3 deletions futures-util/src/try_stream/try_concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,15 @@ where

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
loop {
match ready!(self.as_mut().stream().try_poll_next(cx)) {
Some(Ok(x)) => {
match ready!(self.as_mut().stream().try_poll_next(cx)?) {
Some(x) => {
let accum = self.as_mut().accum();
if let Some(a) = accum {
a.extend(x)
} else {
*accum = Some(x)
}
},
Some(Err(e)) => return Poll::Ready(Err(e)),
None => {
return Poll::Ready(Ok(self.as_mut().accum().take().unwrap_or_default()))
}
Expand Down
5 changes: 2 additions & 3 deletions futures-util/src/try_stream/try_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ impl<St, Fut, F> Stream for TryFilter<St, Fut, F>
) -> Poll<Option<Result<St::Ok, St::Error>>> {
loop {
if self.pending_fut.is_none() {
let item = match ready!(self.as_mut().stream().try_poll_next(cx)) {
Some(Ok(x)) => x,
Some(Err(e)) => return Poll::Ready(Some(Err(e))),
let item = match ready!(self.as_mut().stream().try_poll_next(cx)?) {
Some(x) => x,
None => return Poll::Ready(None),
};
let fut = (self.as_mut().f())(&item);
Expand Down
11 changes: 4 additions & 7 deletions futures-util/src/try_stream/try_filter_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ impl<St, Fut, F, T> Stream for TryFilterMap<St, Fut, F>
) -> Poll<Option<Result<T, St::Error>>> {
loop {
if self.pending.is_none() {
let item = match ready!(self.as_mut().stream().try_poll_next(cx)) {
Some(Ok(x)) => x,
Some(Err(e)) => return Poll::Ready(Some(Err(e))),
let item = match ready!(self.as_mut().stream().try_poll_next(cx)?) {
Some(x) => x,
None => return Poll::Ready(None),
};
let fut = (self.as_mut().f())(item);
Expand All @@ -95,10 +94,8 @@ impl<St, Fut, F, T> Stream for TryFilterMap<St, Fut, F>

let result = ready!(self.as_mut().pending().as_pin_mut().unwrap().try_poll(cx));
self.as_mut().pending().set(None);
match result {
Ok(Some(x)) => return Poll::Ready(Some(Ok(x))),
Err(e) => return Poll::Ready(Some(Err(e))),
Ok(None) => {},
if let Some(x) = result? {
return Poll::Ready(Some(Ok(x)));
}
}
}
Expand Down

0 comments on commit 7c84697

Please sign in to comment.