From ae2dd0c13a3c56ad2e4d005ae70b16e76834d783 Mon Sep 17 00:00:00 2001 From: Gnome! <45660393+GnomedDev@users.noreply.github.com> Date: Tue, 21 Jun 2022 13:39:26 +0100 Subject: [PATCH] Rework ClientData to wrap in Option instead of initialised bool (#2) --- src/info.rs | 6 ++++-- src/manager.rs | 46 ++++++++++++++++++++++++++-------------------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/info.rs b/src/info.rs index e5abda03d..e2f68afcf 100644 --- a/src/info.rs +++ b/src/info.rs @@ -13,7 +13,9 @@ impl ConnectionProgress { channel_id, guild_id, user_id, - ..Default::default() + token: None, + endpoint: None, + session_id: None, }) } @@ -133,7 +135,7 @@ impl fmt::Debug for ConnectionInfo { } } -#[derive(Clone, Default)] +#[derive(Clone)] pub(crate) struct Partial { pub channel_id: ChannelId, pub endpoint: Option, diff --git a/src/manager.rs b/src/manager.rs index 33891f3a9..4b9779360 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -31,10 +31,9 @@ use twilight_gateway::Cluster; #[cfg(feature = "twilight")] use twilight_model::gateway::event::Event as TwilightEvent; -#[derive(Clone, Copy, Debug, Default)] +#[derive(Clone, Copy, Debug)] struct ClientData { shard_count: u64, - initialised: bool, user_id: UserId, } @@ -46,7 +45,7 @@ struct ClientData { /// [`Call`]: Call #[derive(Debug)] pub struct Songbird { - client_data: PRwLock, + client_data: PRwLock>, calls: DashMap>>, sharder: Sharder, config: PRwLock>, @@ -73,7 +72,7 @@ impl Songbird { #[must_use] pub fn serenity_from_config(config: Config) -> Arc { Arc::new(Self { - client_data: PRwLock::new(ClientData::default()), + client_data: PRwLock::new(None), calls: DashMap::new(), sharder: Sharder::Serenity(SerenitySharder::default()), config: Some(config).into(), @@ -108,11 +107,10 @@ impl Songbird { U: Into, { Self { - client_data: PRwLock::new(ClientData { + client_data: PRwLock::new(Some(ClientData { shard_count: cluster.config().shard_scheme().total(), - initialised: true, user_id: user_id.into(), - }), + })), calls: DashMap::new(), sharder: Sharder::TwilightCluster(cluster), config: Some(config).into(), @@ -128,13 +126,14 @@ impl Songbird { pub fn initialise_client_data>(&self, shard_count: u64, user_id: U) { let mut client_data = self.client_data.write(); - if client_data.initialised { + if client_data.is_some() { return; } - client_data.shard_count = shard_count; - client_data.user_id = user_id.into(); - client_data.initialised = true; + *client_data = Some(ClientData { + shard_count, + user_id: user_id.into(), + }); } /// Retrieves a [`Call`] for the given guild, if one already exists. @@ -165,7 +164,11 @@ impl Songbird { self.calls .entry(guild_id) .or_insert_with(|| { - let info = self.manager_info(); + let info = self + .client_data + .read() + .expect("Manager has not been initialised"); + let shard = shard_id(guild_id.0, info.shard_count); let shard_handle = self .sharder @@ -196,12 +199,6 @@ impl Songbird { *config = Some(new_config); } - fn manager_info(&self) -> ClientData { - let client_data = self.client_data.write(); - - *client_data - } - #[cfg(feature = "driver")] /// Connects to a target by retrieving its relevant [`Call`] and /// connecting, or creating the handler if required. @@ -375,7 +372,12 @@ impl Songbird { } }, TwilightEvent::VoiceStateUpdate(v) => { - if v.0.user_id.get() != self.client_data.read().user_id.0 { + if self + .client_data + .read() + .as_ref() + .map_or(true, |data| v.0.user_id.get() != data.user_id.0) + { return; } @@ -431,7 +433,11 @@ impl VoiceGatewayManager for Songbird { } async fn state_update(&self, guild_id: SerenityGuild, voice_state: &VoiceState) { - if voice_state.user_id.0 != self.client_data.read().user_id.0 { + if self + .client_data + .read() + .map_or(true, |data| voice_state.user_id.0 != data.user_id.0) + { return; }