From b0d74ae5f33b1aa8851ee4f7e7f204367a76693b Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Mon, 16 Dec 2024 11:34:43 +0000 Subject: [PATCH] Add AsRef impls for Payload & Utf8Payload --- CHANGELOG.md | 3 +++ src/protocol/frame/frame.rs | 2 +- src/protocol/frame/mod.rs | 9 +++------ src/protocol/frame/payload.rs | 21 +++++++++++++++++++++ src/protocol/mod.rs | 11 +++-------- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c93be19..86d1f46c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# Unreleased +- Implement `AsRef<[u8]>` for `Payload`, `AsRef<[u8]>`, `AsRef` for `Utf8Payload`. + # 0.25.0 - New `Payload` type for `Message` that allows sending messages with a payload that can be cheaply cloned (`Bytes`). diff --git a/src/protocol/frame/frame.rs b/src/protocol/frame/frame.rs index ef8dfe3c..11cfe7fd 100644 --- a/src/protocol/frame/frame.rs +++ b/src/protocol/frame/frame.rs @@ -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] diff --git a/src/protocol/frame/mod.rs b/src/protocol/frame/mod.rs index 6ae107f3..fd3b4524 100644 --- a/src/protocol/frame/mod.rs +++ b/src/protocol/frame/mod.rs @@ -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(); @@ -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] ); } diff --git a/src/protocol/frame/payload.rs b/src/protocol/frame/payload.rs index bac37182..98e0bc0c 100644 --- a/src/protocol/frame/payload.rs +++ b/src/protocol/frame/payload.rs @@ -116,6 +116,20 @@ impl Display for Utf8Payload { } } +impl AsRef 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 { @@ -265,3 +279,10 @@ impl PartialEq<&[u8; N]> for Payload { self.as_slice() == *other } } + +impl AsRef<[u8]> for Payload { + #[inline] + fn as_ref(&self) -> &[u8] { + self.as_slice() + } +} diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 202059bc..d057121e 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -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, @@ -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) }