Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework ClientData to wrap in Option instead of initialised bool #2

Merged
merged 2 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ impl ConnectionProgress {
channel_id,
guild_id,
user_id,
..Default::default()
token: None,
endpoint: None,
session_id: None,
})
}

Expand Down Expand Up @@ -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<String>,
Expand Down
46 changes: 26 additions & 20 deletions src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -46,7 +45,7 @@ struct ClientData {
/// [`Call`]: Call
#[derive(Debug)]
pub struct Songbird {
client_data: PRwLock<ClientData>,
client_data: PRwLock<Option<ClientData>>,
calls: DashMap<GuildId, Arc<Mutex<Call>>>,
sharder: Sharder,
config: PRwLock<Option<Config>>,
Expand All @@ -73,7 +72,7 @@ impl Songbird {
#[must_use]
pub fn serenity_from_config(config: Config) -> Arc<Self> {
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(),
Expand Down Expand Up @@ -108,11 +107,10 @@ impl Songbird {
U: Into<UserId>,
{
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(),
Expand All @@ -128,13 +126,14 @@ impl Songbird {
pub fn initialise_client_data<U: Into<UserId>>(&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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down