From 4d0c1c030d4287d77469585a2fb47c27ed3ae917 Mon Sep 17 00:00:00 2001 From: 0/0 Date: Wed, 12 Apr 2023 20:21:25 +0000 Subject: [PATCH] Gateway: Fix serenity breaking changes (#173) Fix issues caused by serenity-rs/serenity#2372 and serenity-rs/serenity#2380. Additionally, this Boxes the TrySendError from Serenity at Clippy's behest (causing a ~330B Result type). --------- Co-authored-by: Kyle Simpson --- src/error.rs | 8 ++++---- src/manager.rs | 5 ++--- src/shards.rs | 21 ++++++++++++--------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/error.rs b/src/error.rs index 54a8bc864..db4b3e17c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,7 +5,7 @@ use futures::channel::mpsc::TrySendError; #[cfg(not(feature = "simd-json"))] pub use serde_json::Error as JsonError; #[cfg(feature = "serenity")] -use serenity::gateway::InterMessage; +use serenity::gateway::ShardRunnerMessage; #[cfg(feature = "simd-json")] pub use simd_json::Error as JsonError; #[cfg(feature = "gateway")] @@ -48,7 +48,7 @@ pub enum JoinError { Driver(ConnectionError), #[cfg(feature = "serenity")] /// Serenity-specific WebSocket send error. - Serenity(TrySendError), + Serenity(Box>), #[cfg(feature = "twilight")] /// Twilight-specific WebSocket send error when a message fails to send over websocket. Twilight(SendError), @@ -117,8 +117,8 @@ impl Error for JoinError { } #[cfg(all(feature = "serenity", feature = "gateway"))] -impl From> for JoinError { - fn from(e: TrySendError) -> Self { +impl From>> for JoinError { + fn from(e: Box>) -> Self { JoinError::Serenity(e) } } diff --git a/src/manager.rs b/src/manager.rs index 77bc09523..b0032f8e7 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -17,8 +17,7 @@ use once_cell::sync::OnceCell; use parking_lot::RwLock as PRwLock; #[cfg(feature = "serenity")] use serenity::{ - client::bridge::voice::VoiceGatewayManager, - gateway::InterMessage, + gateway::{ShardRunnerMessage, VoiceGatewayManager}, model::{ id::{GuildId as SerenityGuild, UserId as SerenityUser}, voice::VoiceState, @@ -416,7 +415,7 @@ impl VoiceGatewayManager for Songbird { debug!("Songbird ({:?}) Initialised!", user_id); } - async fn register_shard(&self, shard_id: u32, sender: Sender) { + async fn register_shard(&self, shard_id: u32, sender: Sender) { debug!( "Registering Serenity shard handle {} with Songbird", shard_id diff --git a/src/shards.rs b/src/shards.rs index 6e071367c..d0b6e6438 100644 --- a/src/shards.rs +++ b/src/shards.rs @@ -12,7 +12,7 @@ use parking_lot::{lock_api::RwLockWriteGuard, Mutex as PMutex, RwLock as PRwLock #[cfg(feature = "serenity")] use serde_json::json; #[cfg(feature = "serenity")] -use serenity::gateway::InterMessage; +use serenity::gateway::ShardRunnerMessage; #[cfg(feature = "serenity")] use std::result::Result as StdResult; use std::sync::Arc; @@ -96,7 +96,7 @@ impl Sharder { #[cfg(feature = "serenity")] impl Sharder { #[allow(unreachable_patterns)] - pub(crate) fn register_shard_handle(&self, shard_id: u32, sender: Sender) { + pub(crate) fn register_shard_handle(&self, shard_id: u32, sender: Sender) { if let Sharder::Serenity(s) = self { s.register_shard_handle(shard_id, sender); } else { @@ -128,7 +128,7 @@ impl SerenitySharder { self.0.entry(shard_id).or_default().clone() } - fn register_shard_handle(&self, shard_id: u32, sender: Sender) { + fn register_shard_handle(&self, shard_id: u32, sender: Sender) { // Write locks are only used to add new entries to the map. let handle = self.get_or_insert_shard_handle(shard_id); @@ -180,7 +180,7 @@ impl VoiceUpdate for Shard { } }); - handle.send(InterMessage::json(map.to_string()))?; + handle.send(ShardRunnerMessage::Message(map.to_string().into()))?; Ok(()) }, #[cfg(feature = "twilight")] @@ -228,13 +228,13 @@ pub trait VoiceUpdate { /// a reconnect/rebalance is ongoing. #[derive(Debug, Default)] pub struct SerenityShardHandle { - sender: PRwLock>>, - queue: PMutex>, + sender: PRwLock>>, + queue: PMutex>, } #[cfg(feature = "serenity")] impl SerenityShardHandle { - fn register(&self, sender: Sender) { + fn register(&self, sender: Sender) { debug!("Adding shard handle send channel..."); let mut sender_lock = self.sender.write(); @@ -275,10 +275,13 @@ impl SerenityShardHandle { debug!("Removed shard handle send channel."); } - fn send(&self, message: InterMessage) -> StdResult<(), TrySendError> { + fn send( + &self, + message: ShardRunnerMessage, + ) -> StdResult<(), Box>> { let sender_lock = self.sender.read(); if let Some(sender) = &*sender_lock { - sender.unbounded_send(message) + sender.unbounded_send(message).map_err(Box::new) } else { debug!("Serenity shard temporarily disconnected: buffering message..."); let mut messages_lock = self.queue.lock();