Skip to content

Commit

Permalink
Gateway: Fix serenity breaking changes (#173)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
tazz4843 and FelixMcFelix committed Nov 20, 2023
1 parent 7f519d0 commit 4d0c1c0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -48,7 +48,7 @@ pub enum JoinError {
Driver(ConnectionError),
#[cfg(feature = "serenity")]
/// Serenity-specific WebSocket send error.
Serenity(TrySendError<InterMessage>),
Serenity(Box<TrySendError<ShardRunnerMessage>>),
#[cfg(feature = "twilight")]
/// Twilight-specific WebSocket send error when a message fails to send over websocket.
Twilight(SendError),
Expand Down Expand Up @@ -117,8 +117,8 @@ impl Error for JoinError {
}

#[cfg(all(feature = "serenity", feature = "gateway"))]
impl From<TrySendError<InterMessage>> for JoinError {
fn from(e: TrySendError<InterMessage>) -> Self {
impl From<Box<TrySendError<ShardRunnerMessage>>> for JoinError {
fn from(e: Box<TrySendError<ShardRunnerMessage>>) -> Self {
JoinError::Serenity(e)
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -416,7 +415,7 @@ impl VoiceGatewayManager for Songbird {
debug!("Songbird ({:?}) Initialised!", user_id);
}

async fn register_shard(&self, shard_id: u32, sender: Sender<InterMessage>) {
async fn register_shard(&self, shard_id: u32, sender: Sender<ShardRunnerMessage>) {
debug!(
"Registering Serenity shard handle {} with Songbird",
shard_id
Expand Down
21 changes: 12 additions & 9 deletions src/shards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<InterMessage>) {
pub(crate) fn register_shard_handle(&self, shard_id: u32, sender: Sender<ShardRunnerMessage>) {
if let Sharder::Serenity(s) = self {
s.register_shard_handle(shard_id, sender);
} else {
Expand Down Expand Up @@ -128,7 +128,7 @@ impl SerenitySharder {
self.0.entry(shard_id).or_default().clone()
}

fn register_shard_handle(&self, shard_id: u32, sender: Sender<InterMessage>) {
fn register_shard_handle(&self, shard_id: u32, sender: Sender<ShardRunnerMessage>) {
// Write locks are only used to add new entries to the map.
let handle = self.get_or_insert_shard_handle(shard_id);

Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -228,13 +228,13 @@ pub trait VoiceUpdate {
/// a reconnect/rebalance is ongoing.
#[derive(Debug, Default)]
pub struct SerenityShardHandle {
sender: PRwLock<Option<Sender<InterMessage>>>,
queue: PMutex<Vec<InterMessage>>,
sender: PRwLock<Option<Sender<ShardRunnerMessage>>>,
queue: PMutex<Vec<ShardRunnerMessage>>,
}

#[cfg(feature = "serenity")]
impl SerenityShardHandle {
fn register(&self, sender: Sender<InterMessage>) {
fn register(&self, sender: Sender<ShardRunnerMessage>) {
debug!("Adding shard handle send channel...");

let mut sender_lock = self.sender.write();
Expand Down Expand Up @@ -275,10 +275,13 @@ impl SerenityShardHandle {
debug!("Removed shard handle send channel.");
}

fn send(&self, message: InterMessage) -> StdResult<(), TrySendError<InterMessage>> {
fn send(
&self,
message: ShardRunnerMessage,
) -> StdResult<(), Box<TrySendError<ShardRunnerMessage>>> {
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();
Expand Down

0 comments on commit 4d0c1c0

Please sign in to comment.