From a66f20a1060b7d6b88e36f008f6527afb462522f Mon Sep 17 00:00:00 2001 From: asonix Date: Mon, 4 Nov 2024 18:02:41 -0600 Subject: [PATCH] Reduce copies in ping/pong --- actix-ws/examples/chat.rs | 4 ++-- actix-ws/src/session.rs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/actix-ws/examples/chat.rs b/actix-ws/examples/chat.rs index 9d5c823df..440212832 100644 --- a/actix-ws/examples/chat.rs +++ b/actix-ws/examples/chat.rs @@ -82,7 +82,7 @@ async fn ws( loop { interval.tick().await; - if session2.ping(b"").await.is_err() { + if session2.ping(&b""[..]).await.is_err() { break; } @@ -97,7 +97,7 @@ async fn ws( while let Some(Ok(msg)) = stream.recv().await { match msg { AggregatedMessage::Ping(bytes) => { - if session.pong(&bytes).await.is_err() { + if session.pong(bytes).await.is_err() { return; } } diff --git a/actix-ws/src/session.rs b/actix-ws/src/session.rs index ad210809e..57ee712ec 100644 --- a/actix-ws/src/session.rs +++ b/actix-ws/src/session.rs @@ -103,11 +103,11 @@ impl Session { /// } /// # } /// ``` - pub async fn ping(&mut self, msg: &[u8]) -> Result<(), Closed> { + pub async fn ping(&mut self, msg: impl Into) -> Result<(), Closed> { self.pre_check(); if let Some(inner) = self.inner.as_mut() { inner - .send(Message::Ping(Bytes::copy_from_slice(msg))) + .send(Message::Ping(msg.into())) .await .map_err(|_| Closed) } else { @@ -127,11 +127,11 @@ impl Session { /// _ => (), /// } /// # } - pub async fn pong(&mut self, msg: &[u8]) -> Result<(), Closed> { + pub async fn pong(&mut self, msg: impl Into) -> Result<(), Closed> { self.pre_check(); if let Some(inner) = self.inner.as_mut() { inner - .send(Message::Pong(Bytes::copy_from_slice(msg))) + .send(Message::Pong(msg.into())) .await .map_err(|_| Closed) } else {