From 8dedf3bf011640edf0834c8e931b8e5ca5b406aa Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Tue, 19 Oct 2021 17:28:46 +0100 Subject: [PATCH] Gateway: Add generics to `Call` methods. (#102) Adds generics to any `Id` types on `Call`. Also includes the overlooked `Songbird::get_or_insert`. Closes #94. Tested using `cargo make ready`. --- src/handler.rs | 69 ++++++++++++++++++++++++++++++++++++++++++-------- src/manager.rs | 17 ++++++++----- 2 files changed, 69 insertions(+), 17 deletions(-) diff --git a/src/handler.rs b/src/handler.rs index 74c3886e3..060b88f06 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -10,6 +10,7 @@ use crate::{ }; use flume::Sender; use serde_json::json; +use std::fmt::Debug; use tracing::instrument; #[cfg(feature = "driver-core")] @@ -68,15 +69,28 @@ impl Call { /// the given shard. #[inline] #[instrument] - pub fn new(guild_id: GuildId, ws: Shard, user_id: UserId) -> Self { - Self::new_raw_cfg(guild_id, Some(ws), user_id, Default::default()) + pub fn new(guild_id: G, ws: Shard, user_id: U) -> Self + where + G: Into + Debug, + U: Into + Debug, + { + Self::new_raw_cfg( + guild_id.into(), + Some(ws), + user_id.into(), + Default::default(), + ) } /// Creates a new Call, configuring the driver as specified. #[inline] #[instrument] - pub fn from_config(guild_id: GuildId, ws: Shard, user_id: UserId, config: Config) -> Self { - Self::new_raw_cfg(guild_id, Some(ws), user_id, config) + pub fn from_config(guild_id: G, ws: Shard, user_id: U, config: Config) -> Self + where + G: Into + Debug, + U: Into + Debug, + { + Self::new_raw_cfg(guild_id.into(), Some(ws), user_id.into(), config) } /// Creates a new, standalone Call which is not connected via @@ -89,15 +103,23 @@ impl Call { /// For most use cases you do not want this. #[inline] #[instrument] - pub fn standalone(guild_id: GuildId, user_id: UserId) -> Self { - Self::new_raw_cfg(guild_id, None, user_id, Default::default()) + pub fn standalone(guild_id: G, user_id: U) -> Self + where + G: Into + Debug, + U: Into + Debug, + { + Self::new_raw_cfg(guild_id.into(), None, user_id.into(), Default::default()) } /// Creates a new standalone Call from the given configuration file. #[inline] #[instrument] - pub fn standalone_from_config(guild_id: GuildId, user_id: UserId, config: Config) -> Self { - Self::new_raw_cfg(guild_id, None, user_id, config) + pub fn standalone_from_config(guild_id: G, user_id: U, config: Config) -> Self + where + G: Into + Debug, + U: Into + Debug, + { + Self::new_raw_cfg(guild_id.into(), None, user_id.into(), config) } fn new_raw_cfg(guild_id: GuildId, ws: Option, user_id: UserId, config: Config) -> Self { @@ -198,7 +220,16 @@ impl Call { /// /// [`Songbird::join`]: crate::Songbird::join #[instrument(skip(self))] - pub async fn join(&mut self, channel_id: ChannelId) -> JoinResult { + #[inline] + pub async fn join(&mut self, channel_id: C) -> JoinResult + where + C: Into + Debug, + { + self._join(channel_id.into()).await + } + + #[cfg(feature = "driver-core")] + async fn _join(&mut self, channel_id: ChannelId) -> JoinResult { let (tx, rx) = flume::unbounded(); let (gw_tx, gw_rx) = flume::unbounded(); @@ -250,7 +281,15 @@ impl Call { /// /// [`Songbird::join_gateway`]: crate::Songbird::join_gateway #[instrument(skip(self))] - pub async fn join_gateway(&mut self, channel_id: ChannelId) -> JoinResult { + #[inline] + pub async fn join_gateway(&mut self, channel_id: C) -> JoinResult + where + C: Into + Debug, + { + self._join_gateway(channel_id.into()).await + } + + async fn _join_gateway(&mut self, channel_id: ChannelId) -> JoinResult { let (tx, rx) = flume::unbounded(); let do_conn = self @@ -376,7 +415,15 @@ impl Call { /// /// [`standalone`]: Call::standalone #[instrument(skip(self))] - pub fn update_state(&mut self, session_id: String, channel_id: Option) { + #[inline] + pub fn update_state(&mut self, session_id: String, channel_id: Option) + where + C: Into + Debug, + { + self._update_state(session_id, channel_id.map(|c| c.into())) + } + + fn _update_state(&mut self, session_id: String, channel_id: Option) { if let Some(channel_id) = channel_id { let try_conn = if let Some((ref mut progress, _)) = self.connection.as_mut() { progress.apply_state_update(session_id, channel_id) diff --git a/src/manager.rs b/src/manager.rs index c133716e5..566102067 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -155,7 +155,15 @@ impl Songbird { /// This will not join any calls, or cause connection state to change. /// /// [`Call`]: Call - pub fn get_or_insert(&self, guild_id: GuildId) -> Arc> { + #[inline] + pub fn get_or_insert(&self, guild_id: G) -> Arc> + where + G: Into, + { + self._get_or_insert(guild_id.into()) + } + + fn _get_or_insert(&self, guild_id: GuildId) -> Arc> { self.get(guild_id).unwrap_or_else(|| { self.calls .entry(guild_id) @@ -378,7 +386,7 @@ impl Songbird { if let Some(call) = call { let mut handler = call.lock().await; - handler.update_state(v.0.session_id.clone(), v.0.channel_id.map(Into::into)); + handler.update_state(v.0.session_id.clone(), v.0.channel_id); } }, _ => {}, @@ -432,10 +440,7 @@ impl VoiceGatewayManager for Songbird { if let Some(call) = self.get(guild_id) { let mut handler = call.lock().await; - handler.update_state( - voice_state.session_id.clone(), - voice_state.channel_id.map(Into::into), - ); + handler.update_state(voice_state.session_id.clone(), voice_state.channel_id); } } }