Skip to content

Commit

Permalink
Reduce copies in ping/pong
Browse files Browse the repository at this point in the history
  • Loading branch information
asonix committed Nov 5, 2024
1 parent b3c5934 commit a66f20a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions actix-ws/examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
}
Expand Down
8 changes: 4 additions & 4 deletions actix-ws/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bytes>) -> 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 {
Expand All @@ -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<Bytes>) -> 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 {
Expand Down

0 comments on commit a66f20a

Please sign in to comment.