Skip to content

Commit

Permalink
derive PartialEq for StreamState
Browse files Browse the repository at this point in the history
  • Loading branch information
vilgotf committed Jul 21, 2023
1 parent 41dee4d commit c865878
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,7 @@ impl Message {
}

/// The connection state of the stream.
#[derive(Debug)]
#[allow(unused)]
#[derive(Debug, PartialEq)]
enum StreamState {
/// The connection is fully active and no close has been initiated.
Active,
Expand Down Expand Up @@ -819,7 +818,7 @@ where
let (opcode, payload) = match ready!(self.as_mut().poll_read_next_message(cx)) {
Some(Ok((opcode, payload))) => (opcode, payload),
Some(Err(e)) => {
if matches!(self.inner.codec().state, StreamState::Active) {
if self.inner.codec().state == StreamState::Active {
if let Error::Protocol(protocol) = &e {
self.pending_message = Some(protocol.into());
}
Expand All @@ -833,7 +832,7 @@ where
let message = match Message::from_raw(opcode, payload) {
Ok(msg) => msg,
Err(e) => {
if matches!(self.inner.codec().state, StreamState::Active) {
if self.inner.codec().state == StreamState::Active {
self.pending_message = Some(Message::from(&e));
}

Expand All @@ -857,7 +856,7 @@ where
}
},
OpCode::Ping
if matches!(self.inner.codec().state, StreamState::Active)
if self.inner.codec().state == StreamState::Active
&& !self
.pending_message
.as_ref()
Expand Down Expand Up @@ -908,7 +907,7 @@ where
}

fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
if matches!(self.inner.codec().state, StreamState::Active)
if self.inner.codec().state == StreamState::Active
&& !self
.pending_message
.as_ref()
Expand Down Expand Up @@ -945,14 +944,14 @@ impl Encoder<Message> for WebsocketProtocol {

#[allow(clippy::cast_possible_truncation)]
fn encode(&mut self, item: Message, dst: &mut BytesMut) -> Result<(), Self::Error> {
if !matches!(self.state, StreamState::Active)
&& !matches!(self.state, StreamState::ClosedByPeer if item.is_close())
if !(self.state == StreamState::Active
|| matches!(self.state, StreamState::ClosedByPeer if item.is_close()))
{
return Err(Error::AlreadyClosed);
}

if item.is_close() {
if matches!(self.state, StreamState::ClosedByPeer) {
if self.state == StreamState::ClosedByPeer {
self.state = StreamState::CloseAcknowledged;
} else {
self.state = StreamState::ClosedByUs;
Expand Down

0 comments on commit c865878

Please sign in to comment.