From 33b44cb25602e6b78fc9ff4c3ac454c34f5c8aa4 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Tue, 9 Jul 2024 19:23:49 +0300 Subject: [PATCH] chore: Impl `AsRef` for `ServerEvent` Signed-off-by: Awiteb --- crates/oxidetalis/src/extensions.rs | 9 +++++---- crates/oxidetalis/src/websocket/events/server.rs | 6 ++++++ crates/oxidetalis/src/websocket/mod.rs | 16 ++++++++++------ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/crates/oxidetalis/src/extensions.rs b/crates/oxidetalis/src/extensions.rs index 3f4415e..eeee250 100644 --- a/crates/oxidetalis/src/extensions.rs +++ b/crates/oxidetalis/src/extensions.rs @@ -19,7 +19,7 @@ use std::sync::Arc; use chrono::Utc; use oxidetalis_config::Config; use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator}; -use salvo::{websocket::Message, Depot}; +use salvo::Depot; use sea_orm::DatabaseConnection; use uuid::Uuid; @@ -87,9 +87,10 @@ impl OnlineUsersExt for OnlineUsers { let now = Utc::now(); self.write().await.par_iter_mut().for_each(|(_, u)| { u.pinged_at = now; - let _ = u.sender.unbounded_send(Ok(Message::from( - &ServerEvent::ping().sign(&u.shared_secret), - ))); + let _ = u.sender.unbounded_send(Ok(ServerEvent::ping() + .sign(&u.shared_secret) + .as_ref() + .into())); }); } diff --git a/crates/oxidetalis/src/websocket/events/server.rs b/crates/oxidetalis/src/websocket/events/server.rs index 225b129..9bd990a 100644 --- a/crates/oxidetalis/src/websocket/events/server.rs +++ b/crates/oxidetalis/src/websocket/events/server.rs @@ -101,6 +101,12 @@ impl ServerEvent { } } +impl AsRef for ServerEvent { + fn as_ref(&self) -> &Self { + self + } +} + impl From<&ServerEvent> for Message { fn from(value: &ServerEvent) -> Self { Message::text(serde_json::to_string(value).expect("This can't fail")) diff --git a/crates/oxidetalis/src/websocket/mod.rs b/crates/oxidetalis/src/websocket/mod.rs index c91479e..ed4a901 100644 --- a/crates/oxidetalis/src/websocket/mod.rs +++ b/crates/oxidetalis/src/websocket/mod.rs @@ -132,18 +132,22 @@ async fn handle_socket( match handle_ws_msg(msg, &nonce_cache, &user_shared_secret).await { Ok(event) => { if let Some(server_event) = handle_events(event, &conn_id).await { - if let Err(err) = sender.unbounded_send(Ok(Message::from( - &server_event.sign(&user_shared_secret), - ))) { + if let Err(err) = sender.unbounded_send(Ok(server_event + .sign(&user_shared_secret) + .as_ref() + .into())) + { log::error!("Websocket Error: {err}"); break; } }; } Err(err) => { - if let Err(err) = sender.unbounded_send(Ok(Message::from( - &ServerEvent::from(err).sign(&user_shared_secret), - ))) { + if let Err(err) = sender.unbounded_send(Ok(ServerEvent::from(err) + .sign(&user_shared_secret) + .as_ref() + .into())) + { log::error!("Websocket Error: {err}"); break; };