Skip to content

Commit

Permalink
chore(codec): Flatten nested structure
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto committed Nov 26, 2024
1 parent bdccf58 commit 938c317
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions tonic/src/codec/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ impl StreamingInner {

// Returns Some(()) if data was found or None if the loop in `poll_next` should break
fn poll_frame(&mut self, cx: &mut Context<'_>) -> Poll<Result<Option<()>, Status>> {
let chunk = match ready!(Pin::new(&mut self.body).poll_frame(cx)) {
Some(Ok(d)) => Some(d),
let frame = match ready!(Pin::new(&mut self.body).poll_frame(cx)) {
Some(Ok(frame)) => frame,
Some(Err(status)) => {
if self.direction == Direction::Request && status.code() == Code::Cancelled {
return Poll::Ready(Ok(None));
Expand All @@ -257,37 +257,30 @@ impl StreamingInner {
debug!("decoder inner stream error: {:?}", status);
return Poll::Ready(Err(status));
}
None => None,
};

Poll::Ready(if let Some(frame) = chunk {
match frame {
frame if frame.is_data() => {
self.buf.put(frame.into_data().unwrap());
Ok(Some(()))
}
frame if frame.is_trailers() => {
match &mut self.trailers {
Some(trailers) => {
trailers.extend(frame.into_trailers().unwrap());
}
None => {
self.trailers = Some(frame.into_trailers().unwrap());
}
}

None => {
// FIXME: improve buf usage.
return Poll::Ready(if self.buf.has_remaining() {
trace!("unexpected EOF decoding stream, state: {:?}", self.state);
Err(Status::internal("Unexpected EOF decoding stream."))
} else {
Ok(None)
}
frame => panic!("unexpected frame: {:?}", frame),
});
}
} else {
// FIXME: improve buf usage.
if self.buf.has_remaining() {
trace!("unexpected EOF decoding stream, state: {:?}", self.state);
Err(Status::internal("Unexpected EOF decoding stream."))
};

Poll::Ready(if frame.is_data() {
self.buf.put(frame.into_data().unwrap());
Ok(Some(()))
} else if frame.is_trailers() {
if let Some(trailers) = &mut self.trailers {
trailers.extend(frame.into_trailers().unwrap());
} else {
Ok(None)
self.trailers = Some(frame.into_trailers().unwrap());
}

Ok(None)
} else {
panic!("unexpected frame: {:?}", frame);
})
}

Expand Down

0 comments on commit 938c317

Please sign in to comment.