Skip to content

Commit

Permalink
Add AsRef impls for Payload & Utf8Payload
Browse files Browse the repository at this point in the history
  • Loading branch information
alexheretic authored and daniel-abramov committed Dec 16, 2024
1 parent 31dbb4f commit b0d74ae
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Unreleased
- Implement `AsRef<[u8]>` for `Payload`, `AsRef<[u8]>`, `AsRef<str>` for `Utf8Payload`.

# 0.25.0

- New `Payload` type for `Message` that allows sending messages with a payload that can be cheaply cloned (`Bytes`).
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/frame/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ mod tests {
let mut payload = Vec::new();
raw.read_to_end(&mut payload).unwrap();
let frame = Frame::from_payload(header, payload.into());
assert_eq!(frame.into_payload().as_slice(), &[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]);
assert_eq!(frame.into_payload(), &[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]);
}

#[test]
Expand Down
9 changes: 3 additions & 6 deletions src/protocol/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,10 @@ mod tests {
let mut sock = FrameSocket::new(raw);

assert_eq!(
sock.read(None).unwrap().unwrap().into_payload().as_slice(),
sock.read(None).unwrap().unwrap().into_payload(),
&[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]
);
assert_eq!(
sock.read(None).unwrap().unwrap().into_payload().as_slice(),
&[0x03, 0x02, 0x01]
);
assert_eq!(sock.read(None).unwrap().unwrap().into_payload(), &[0x03, 0x02, 0x01]);
assert!(sock.read(None).unwrap().is_none());

let (_, rest) = sock.into_inner();
Expand All @@ -301,7 +298,7 @@ mod tests {
let raw = Cursor::new(vec![0x02, 0x03, 0x04, 0x05, 0x06, 0x07]);
let mut sock = FrameSocket::from_partially_read(raw, vec![0x82, 0x07, 0x01]);
assert_eq!(
sock.read(None).unwrap().unwrap().into_payload().as_slice(),
sock.read(None).unwrap().unwrap().into_payload(),
&[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]
);
}
Expand Down
21 changes: 21 additions & 0 deletions src/protocol/frame/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ impl Display for Utf8Payload {
}
}

impl AsRef<str> for Utf8Payload {
#[inline]
fn as_ref(&self) -> &str {
self.as_str()
}
}

impl AsRef<[u8]> for Utf8Payload {
#[inline]
fn as_ref(&self) -> &[u8] {
self.as_slice()
}
}

/// A payload of a WebSocket frame.
#[derive(Debug, Clone)]
pub enum Payload {
Expand Down Expand Up @@ -265,3 +279,10 @@ impl<const N: usize> PartialEq<&[u8; N]> for Payload {
self.as_slice() == *other
}
}

impl AsRef<[u8]> for Payload {
#[inline]
fn as_ref(&self) -> &[u8] {
self.as_slice()
}
}
11 changes: 3 additions & 8 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,10 +664,7 @@ impl WebSocketContext {
match data {
OpData::Continue => {
if let Some(ref mut msg) = self.incomplete {
msg.extend(
frame.into_payload().as_slice(),
self.config.max_message_size,
)?;
msg.extend(frame.into_payload(), self.config.max_message_size)?;
} else {
return Err(Error::Protocol(
ProtocolError::UnexpectedContinueFrame,
Expand Down Expand Up @@ -697,10 +694,8 @@ impl WebSocketContext {
_ => panic!("Bug: message is not text nor binary"),
};
let mut incomplete = IncompleteMessage::new(message_type);
incomplete.extend(
frame.into_payload().as_slice(),
self.config.max_message_size,
)?;
incomplete
.extend(frame.into_payload(), self.config.max_message_size)?;
self.incomplete = Some(incomplete);
Ok(None)
}
Expand Down

0 comments on commit b0d74ae

Please sign in to comment.