-
-
Notifications
You must be signed in to change notification settings - Fork 136
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
refactor!(model, cache, gateway): Make Presence::guild_id
an Option
#2125
base: next
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ use twilight_model::{ | |
pub struct CachedPresence { | ||
pub(crate) activities: Vec<Activity>, | ||
pub(crate) client_status: ClientStatus, | ||
pub(crate) guild_id: Id<GuildMarker>, | ||
pub(crate) guild_id: Option<Id<GuildMarker>>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I do not see a reason for this change. Except that the |
||
pub(crate) status: Status, | ||
pub(crate) user_id: Id<UserMarker>, | ||
} | ||
|
@@ -31,7 +31,7 @@ impl CachedPresence { | |
} | ||
|
||
/// ID of the guild. | ||
pub const fn guild_id(&self) -> Id<GuildMarker> { | ||
pub const fn guild_id(&self) -> Option<Id<GuildMarker>> { | ||
self.guild_id | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,18 +1,15 @@ | ||||
use serde::{Deserialize, Serialize}; | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
use crate::{ | ||||
gateway::presence::{Presence, PresenceListDeserializer}, | ||||
gateway::presence::Presence, | ||||
guild::Member, | ||||
id::{ | ||||
marker::{GuildMarker, UserMarker}, | ||||
Id, | ||||
}, | ||||
}; | ||||
use serde::{ | ||||
de::{Deserializer, Error as DeError, IgnoredAny, MapAccess, Visitor}, | ||||
Deserialize, Serialize, | ||||
}; | ||||
use std::fmt::{Formatter, Result as FmtResult}; | ||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Serialize)] | ||||
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] | ||||
pub struct MemberChunk { | ||||
pub chunk_count: u32, | ||||
pub chunk_index: u32, | ||||
|
@@ -25,164 +22,6 @@ pub struct MemberChunk { | |||
pub presences: Vec<Presence>, | ||||
} | ||||
|
||||
#[derive(Debug, Deserialize)] | ||||
#[serde(field_identifier, rename_all = "snake_case")] | ||||
enum Field { | ||||
ChunkCount, | ||||
ChunkIndex, | ||||
GuildId, | ||||
Members, | ||||
Nonce, | ||||
NotFound, | ||||
Presences, | ||||
} | ||||
|
||||
struct MemberChunkVisitor; | ||||
|
||||
impl<'de> Visitor<'de> for MemberChunkVisitor { | ||||
type Value = MemberChunk; | ||||
|
||||
fn expecting(&self, f: &mut Formatter<'_>) -> FmtResult { | ||||
f.write_str("struct MemberChunk") | ||||
} | ||||
|
||||
#[allow(clippy::too_many_lines)] | ||||
fn visit_map<V: MapAccess<'de>>(self, mut map: V) -> Result<Self::Value, V::Error> { | ||||
let mut chunk_count = None; | ||||
let mut chunk_index = None; | ||||
let mut guild_id = None; | ||||
let mut members = None; | ||||
let mut nonce = None; | ||||
let mut not_found = None; | ||||
let mut presences = None; | ||||
|
||||
let span = tracing::trace_span!("deserializing member chunk"); | ||||
let _span_enter = span.enter(); | ||||
|
||||
loop { | ||||
let span_child = tracing::trace_span!("iterating over element"); | ||||
let _span_child_enter = span_child.enter(); | ||||
|
||||
let key = match map.next_key() { | ||||
Ok(Some(key)) => { | ||||
tracing::trace!(?key, "found key"); | ||||
|
||||
key | ||||
} | ||||
Ok(None) => break, | ||||
Err(why) => { | ||||
// Encountered when we run into an unknown key. | ||||
map.next_value::<IgnoredAny>()?; | ||||
|
||||
tracing::trace!("ran into an unknown key: {why:?}"); | ||||
|
||||
continue; | ||||
} | ||||
}; | ||||
|
||||
match key { | ||||
Field::ChunkCount => { | ||||
if chunk_count.is_some() { | ||||
return Err(DeError::duplicate_field("chunk_count")); | ||||
} | ||||
|
||||
chunk_count = Some(map.next_value()?); | ||||
} | ||||
Field::ChunkIndex => { | ||||
if chunk_index.is_some() { | ||||
return Err(DeError::duplicate_field("chunk_index")); | ||||
} | ||||
|
||||
chunk_index = Some(map.next_value()?); | ||||
} | ||||
Field::GuildId => { | ||||
if guild_id.is_some() { | ||||
return Err(DeError::duplicate_field("guild_id")); | ||||
} | ||||
|
||||
guild_id = Some(map.next_value()?); | ||||
} | ||||
Field::Members => { | ||||
if members.is_some() { | ||||
return Err(DeError::duplicate_field("members")); | ||||
} | ||||
|
||||
members = Some(map.next_value()?); | ||||
} | ||||
Field::Nonce => { | ||||
if nonce.is_some() { | ||||
return Err(DeError::duplicate_field("nonce")); | ||||
} | ||||
|
||||
nonce = Some(map.next_value()?); | ||||
} | ||||
Field::NotFound => { | ||||
if not_found.is_some() { | ||||
return Err(DeError::duplicate_field("not_found")); | ||||
} | ||||
|
||||
not_found = Some(map.next_value()?); | ||||
} | ||||
Field::Presences => { | ||||
if presences.is_some() { | ||||
return Err(DeError::duplicate_field("presences")); | ||||
} | ||||
|
||||
let deserializer = PresenceListDeserializer::new(Id::new(1)); | ||||
|
||||
presences = Some(map.next_value_seed(deserializer)?); | ||||
} | ||||
} | ||||
} | ||||
|
||||
let chunk_count = chunk_count.ok_or_else(|| DeError::missing_field("chunk_count"))?; | ||||
let chunk_index = chunk_index.ok_or_else(|| DeError::missing_field("chunk_index"))?; | ||||
let guild_id = guild_id.ok_or_else(|| DeError::missing_field("guild_id"))?; | ||||
let members = members.ok_or_else(|| DeError::missing_field("members"))?; | ||||
let not_found = not_found.unwrap_or_default(); | ||||
let mut presences = presences.unwrap_or_default(); | ||||
|
||||
tracing::trace!( | ||||
%chunk_count, | ||||
%chunk_index, | ||||
?guild_id, | ||||
?members, | ||||
?not_found, | ||||
?presences, | ||||
); | ||||
|
||||
for presence in &mut presences { | ||||
presence.guild_id = guild_id; | ||||
} | ||||
|
||||
Ok(MemberChunk { | ||||
chunk_count, | ||||
chunk_index, | ||||
guild_id, | ||||
members, | ||||
nonce, | ||||
not_found, | ||||
presences, | ||||
}) | ||||
} | ||||
} | ||||
|
||||
impl<'de> Deserialize<'de> for MemberChunk { | ||||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { | ||||
const FIELDS: &[&str] = &[ | ||||
"chunk_count", | ||||
"chunk_index", | ||||
"guild_id", | ||||
"members", | ||||
"nonce", | ||||
"not_found", | ||||
"presences", | ||||
]; | ||||
|
||||
deserializer.deserialize_struct("MemberChunk", FIELDS, MemberChunkVisitor) | ||||
} | ||||
} | ||||
|
||||
#[cfg(test)] | ||||
mod tests { | ||||
use super::super::MemberChunk; | ||||
|
@@ -439,7 +278,7 @@ mod tests { | |||
mobile: None, | ||||
web: Some(Status::Online), | ||||
}, | ||||
guild_id: Id::new(1), | ||||
guild_id: Some(Id::new(1)), | ||||
status: Status::Online, | ||||
user: UserOrId::UserId { id: Id::new(2) }, | ||||
}, | ||||
|
@@ -450,7 +289,7 @@ mod tests { | |||
mobile: None, | ||||
web: Some(Status::Online), | ||||
}, | ||||
guild_id: Id::new(1), | ||||
guild_id: Some(Id::new(1)), | ||||
status: Status::Online, | ||||
user: UserOrId::UserId { id: Id::new(3) }, | ||||
}, | ||||
|
@@ -461,7 +300,7 @@ mod tests { | |||
mobile: None, | ||||
web: None, | ||||
}, | ||||
guild_id: Id::new(1), | ||||
guild_id: Some(Id::new(1)), | ||||
status: Status::DoNotDisturb, | ||||
user: UserOrId::UserId { id: Id::new(5) }, | ||||
}, | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move this allocation inside of the
if
statement