From 7fb5fb65d876a07924429c70e705ec5d717fbe68 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Fri, 24 Jan 2025 11:38:18 +0100 Subject: [PATCH] Add `Message::len` method Signed-off-by: Yuki Kishimoto --- src/message.rs | 49 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/src/message.rs b/src/message.rs index 0c9b093..cc34000 100644 --- a/src/message.rs +++ b/src/message.rs @@ -39,34 +39,27 @@ pub enum Message { Close(Option), } -// impl Message { -// /// Get the length of the WebSocket message. -// #[inline] -// pub fn len(&self) -> usize { -// match self { -// Self::Text(string) => string.len(), -// Self::Binary(data) => data.len(), -// Self::Ping(data) => data.len(), -// Self::Pong(data) => data.len(), -// Self::Close(frame) => frame.map(|f| f.reason.len()).unwrap_or(0), -// } -// } -// -// /// Returns true if the WebSocket message has no content. -// /// For example, if the other side of the connection sent an empty string. -// #[inline] -// pub fn is_empty(&self) -> bool { -// self.len() == 0 -// } -// -// /// Consume the message and return it as binary data. -// pub fn into_data(self) -> Vec { -// match self { -// Self::Text(string) => string.into_bytes(), -// Self::Binary(data) => data, -// } -// } -// } +impl Message { + /// Get the length of the WebSocket message. + #[inline] + pub fn len(&self) -> usize { + match self { + Self::Text(string) => string.len(), + Self::Binary(data) => data.len(), + #[cfg(not(target_arch = "wasm32"))] + Self::Ping(data) => data.len(), + #[cfg(not(target_arch = "wasm32"))] + Self::Pong(data) => data.len(), + #[cfg(not(target_arch = "wasm32"))] + Self::Close(data) => data.as_ref().map(|d| d.reason.len()).unwrap_or(0), + } + } + + #[inline] + pub fn is_empty(&self) -> bool { + self.len() == 0 + } +} #[cfg(not(target_arch = "wasm32"))] impl From for TungsteniteCloseFrame {