Replace usage of Arc<RwLock<...>> in public APIs with Shared<...>

This commit is contained in:
bitfl0wer 2024-01-23 19:07:23 +01:00
parent 9d543fcd27
commit 5e13f12cbd
22 changed files with 88 additions and 69 deletions

View File

@ -9,7 +9,7 @@ use reqwest::Client;
use serde::{Deserialize, Serialize};
use crate::errors::ChorusResult;
use crate::gateway::{Gateway, GatewayHandle};
use crate::gateway::{Gateway, GatewayHandle, Shared};
use crate::ratelimiter::ChorusRequest;
use crate::types::types::subconfigs::limits::rates::RateLimits;
use crate::types::{
@ -153,11 +153,11 @@ impl fmt::Display for Token {
/// It is used for most authenticated actions on a Spacebar server.
/// It also has its own [Gateway] connection.
pub struct ChorusUser {
pub belongs_to: Arc<RwLock<Instance>>,
pub belongs_to: Shared<Instance>,
pub token: String,
pub limits: Option<HashMap<LimitType, Limit>>,
pub settings: Arc<RwLock<UserSettings>>,
pub object: Arc<RwLock<User>>,
pub settings: Shared<UserSettings>,
pub object: Shared<User>,
pub gateway: GatewayHandle,
}
@ -184,11 +184,11 @@ impl ChorusUser {
/// This isn't the prefered way to create a ChorusUser.
/// See [Instance::login_account] and [Instance::register_account] instead.
pub fn new(
belongs_to: Arc<RwLock<Instance>>,
belongs_to: Shared<Instance>,
token: String,
limits: Option<HashMap<LimitType, Limit>>,
settings: Arc<RwLock<UserSettings>>,
object: Arc<RwLock<User>>,
settings: Shared<UserSettings>,
object: Shared<User>,
gateway: GatewayHandle,
) -> ChorusUser {
ChorusUser {
@ -206,7 +206,7 @@ impl ChorusUser {
/// registering or logging in to the Instance, where you do not yet have a User object, but still
/// need to make a RateLimited request. To use the [`GatewayHandle`], you will have to identify
/// first.
pub(crate) async fn shell(instance: Arc<RwLock<Instance>>, token: String) -> ChorusUser {
pub(crate) async fn shell(instance: Shared<Instance>, token: String) -> ChorusUser {
let settings = Arc::new(RwLock::new(UserSettings::default()));
let object = Arc::new(RwLock::new(User::default()));
let wss_url = instance.read().unwrap().urls.wss.clone();

View File

@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_repr::{Deserialize_repr, Serialize_repr};
use crate::gateway::Shared;
use crate::types::utils::Snowflake;
use crate::types::{Team, User};
@ -27,7 +28,7 @@ pub struct Application {
pub bot_require_code_grant: bool,
pub verify_key: String,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub owner: Arc<RwLock<User>>,
pub owner: Shared<User>,
pub flags: u64,
#[cfg(feature = "sqlx")]
pub redirect_uris: Option<sqlx::types::Json<Vec<String>>>,
@ -49,7 +50,7 @@ pub struct Application {
#[cfg(feature = "sqlx")]
pub install_params: Option<sqlx::types::Json<InstallParams>>,
#[cfg(not(feature = "sqlx"))]
pub install_params: Option<Arc<RwLock<InstallParams>>>,
pub install_params: Option<Shared<InstallParams>>,
pub terms_of_service_url: Option<String>,
pub privacy_policy_url: Option<String>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
@ -142,7 +143,7 @@ pub struct ApplicationCommand {
pub application_id: Snowflake,
pub name: String,
pub description: String,
pub options: Vec<Arc<RwLock<ApplicationCommandOption>>>,
pub options: Vec<Shared<ApplicationCommandOption>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -154,7 +155,7 @@ pub struct ApplicationCommandOption {
pub description: String,
pub required: bool,
pub choices: Vec<ApplicationCommandOptionChoice>,
pub options: Arc<RwLock<Vec<ApplicationCommandOption>>>,
pub options: Shared<Vec<ApplicationCommandOption>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@ -190,14 +191,14 @@ pub enum ApplicationCommandOptionType {
pub struct ApplicationCommandInteractionData {
pub id: Snowflake,
pub name: String,
pub options: Vec<Arc<RwLock<ApplicationCommandInteractionDataOption>>>,
pub options: Vec<Shared<ApplicationCommandInteractionDataOption>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApplicationCommandInteractionDataOption {
pub name: String,
pub value: Value,
pub options: Vec<Arc<RwLock<ApplicationCommandInteractionDataOption>>>,
pub options: Vec<Shared<ApplicationCommandInteractionDataOption>>,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
@ -206,7 +207,7 @@ pub struct GuildApplicationCommandPermissions {
pub id: Snowflake,
pub application_id: Snowflake,
pub guild_id: Snowflake,
pub permissions: Vec<Arc<RwLock<ApplicationCommandPermission>>>,
pub permissions: Vec<Shared<ApplicationCommandPermission>>,
}
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]

View File

@ -2,13 +2,14 @@ use std::sync::{Arc, RwLock};
use serde::{Deserialize, Serialize};
use crate::gateway::Shared;
use crate::types::utils::Snowflake;
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
/// See <https://discord.com/developers/docs/resources/audit-log#audit-log-entry-object>
pub struct AuditLogEntry {
pub target_id: Option<String>,
pub changes: Option<Vec<Arc<RwLock<AuditLogChange>>>>,
pub changes: Option<Vec<Shared<AuditLogChange>>>,
pub user_id: Option<Snowflake>,
pub id: Snowflake,
// to:do implement an enum for these types

View File

@ -1,5 +1,6 @@
use std::sync::{Arc, RwLock};
use crate::gateway::Shared;
#[cfg(feature = "client")]
use crate::gateway::Updateable;
@ -21,8 +22,8 @@ pub struct AutoModerationRule {
pub creator_id: Snowflake,
pub event_type: AutoModerationRuleEventType,
pub trigger_type: AutoModerationRuleTriggerType,
pub trigger_metadata: Arc<RwLock<AutoModerationRuleTriggerMetadata>>,
pub actions: Vec<Arc<RwLock<AutoModerationAction>>>,
pub trigger_metadata: Shared<AutoModerationRuleTriggerMetadata>,
pub actions: Vec<Shared<AutoModerationAction>>,
pub enabled: bool,
pub exempt_roles: Vec<Snowflake>,
pub exempt_channels: Vec<Snowflake>,
@ -99,7 +100,7 @@ pub enum AutoModerationRuleKeywordPresetType {
pub struct AutoModerationAction {
#[serde(rename = "type")]
pub action_type: AutoModerationActionType,
pub metadata: Option<Arc<RwLock<AutoModerationActionMetadata>>>,
pub metadata: Option<Shared<AutoModerationActionMetadata>>,
}
#[derive(Serialize_repr, Deserialize_repr, Debug, Clone, Default)]

View File

@ -6,6 +6,7 @@ use serde_aux::prelude::deserialize_string_from_number;
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::fmt::Debug;
use crate::gateway::Shared;
use crate::types::{
entities::{GuildMember, User},
utils::Snowflake,
@ -71,13 +72,13 @@ pub struct Channel {
pub permission_overwrites: Option<sqlx::types::Json<Vec<PermissionOverwrite>>>,
#[cfg(not(feature = "sqlx"))]
#[cfg_attr(feature = "client", observe_option_vec)]
pub permission_overwrites: Option<Vec<Arc<RwLock<PermissionOverwrite>>>>,
pub permission_overwrites: Option<Vec<Shared<PermissionOverwrite>>>,
pub permissions: Option<String>,
pub position: Option<i32>,
pub rate_limit_per_user: Option<i32>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
#[cfg_attr(feature = "client", observe_option_vec)]
pub recipients: Option<Vec<Arc<RwLock<User>>>>,
pub recipients: Option<Vec<Shared<User>>>,
pub rtc_region: Option<String>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub thread_metadata: Option<ThreadMetadata>,
@ -171,7 +172,7 @@ pub struct ThreadMember {
pub user_id: Option<Snowflake>,
pub join_timestamp: Option<String>,
pub flags: Option<u64>,
pub member: Option<Arc<RwLock<GuildMember>>>,
pub member: Option<Shared<GuildMember>>,
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]

View File

@ -3,6 +3,7 @@ use std::sync::{Arc, RwLock};
use serde::{Deserialize, Serialize};
use crate::gateway::Shared;
use crate::types::entities::User;
use crate::types::Snowflake;
@ -31,7 +32,7 @@ pub struct Emoji {
#[cfg(not(feature = "sqlx"))]
pub roles: Option<Vec<Snowflake>>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub user: Option<Arc<RwLock<User>>>,
pub user: Option<Shared<User>>,
pub require_colons: Option<bool>,
pub managed: Option<bool>,
pub animated: Option<bool>,

View File

@ -6,6 +6,7 @@ use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use crate::gateway::Shared;
use crate::types::types::guild_configuration::GuildFeaturesList;
use crate::types::{
entities::{Channel, Emoji, RoleObject, Sticker, User, VoiceState, Webhook},
@ -45,14 +46,14 @@ pub struct Guild {
pub bans: Option<Vec<GuildBan>>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
#[cfg_attr(feature = "client", observe_option_vec)]
pub channels: Option<Vec<Arc<RwLock<Channel>>>>,
pub channels: Option<Vec<Shared<Channel>>>,
pub default_message_notifications: Option<MessageNotificationLevel>,
pub description: Option<String>,
pub discovery_splash: Option<String>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
#[cfg_attr(feature = "client", observe_vec)]
#[serde(default)]
pub emojis: Vec<Arc<RwLock<Emoji>>>,
pub emojis: Vec<Shared<Emoji>>,
pub explicit_content_filter: Option<i32>,
//#[cfg_attr(feature = "sqlx", sqlx(try_from = "String"))]
pub features: Option<GuildFeaturesList>,
@ -88,7 +89,7 @@ pub struct Guild {
pub region: Option<String>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
#[cfg_attr(feature = "client", observe_option_vec)]
pub roles: Option<Vec<Arc<RwLock<RoleObject>>>>,
pub roles: Option<Vec<Shared<RoleObject>>>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub rules_channel: Option<String>,
pub rules_channel_id: Option<Snowflake>,
@ -102,10 +103,10 @@ pub struct Guild {
pub verification_level: Option<VerificationLevel>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
#[cfg_attr(feature = "client", observe_option_vec)]
pub voice_states: Option<Vec<Arc<RwLock<VoiceState>>>>,
pub voice_states: Option<Vec<Shared<VoiceState>>>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
#[cfg_attr(feature = "client", observe_option_vec)]
pub webhooks: Option<Vec<Arc<RwLock<Webhook>>>>,
pub webhooks: Option<Vec<Shared<Webhook>>>,
#[cfg(feature = "sqlx")]
pub welcome_screen: Option<sqlx::types::Json<WelcomeScreenObject>>,
#[cfg(not(feature = "sqlx"))]
@ -239,11 +240,11 @@ pub struct GuildInvite {
pub created_at: DateTime<Utc>,
pub expires_at: Option<DateTime<Utc>>,
pub guild_id: Snowflake,
pub guild: Option<Arc<RwLock<Guild>>>,
pub guild: Option<Shared<Guild>>,
pub channel_id: Snowflake,
pub channel: Option<Arc<RwLock<Channel>>>,
pub channel: Option<Shared<Channel>>,
pub inviter_id: Option<Snowflake>,
pub inviter: Option<Arc<RwLock<User>>>,
pub inviter: Option<Shared<User>>,
pub target_user_id: Option<Snowflake>,
pub target_user: Option<String>,
pub target_user_type: Option<i32>,
@ -296,7 +297,7 @@ pub struct GuildScheduledEvent {
pub entity_type: GuildScheduledEventEntityType,
pub entity_id: Option<Snowflake>,
pub entity_metadata: Option<GuildScheduledEventEntityMetadata>,
pub creator: Option<Arc<RwLock<User>>>,
pub creator: Option<Shared<User>>,
pub user_count: Option<u64>,
pub image: Option<String>,
}

View File

@ -2,6 +2,7 @@ use std::sync::{Arc, RwLock};
use serde::{Deserialize, Serialize};
use crate::gateway::Shared;
use crate::types::{entities::PublicUser, Snowflake};
#[derive(Debug, Deserialize, Default, Serialize, Clone)]
@ -10,7 +11,7 @@ use crate::types::{entities::PublicUser, Snowflake};
/// # Reference
/// See <https://discord-userdoccers.vercel.app/resources/guild#guild-member-object>
pub struct GuildMember {
pub user: Option<Arc<RwLock<PublicUser>>>,
pub user: Option<Shared<PublicUser>>,
pub nick: Option<String>,
pub avatar: Option<String>,
pub roles: Vec<Snowflake>,

View File

@ -3,6 +3,7 @@ use std::sync::{Arc, RwLock};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::gateway::Shared;
use crate::types::{
entities::{Application, User},
utils::Snowflake,
@ -23,14 +24,14 @@ pub struct Integration {
pub expire_behaviour: Option<u8>,
pub expire_grace_period: Option<u16>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub user: Option<Arc<RwLock<User>>>,
pub user: Option<Shared<User>>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub account: IntegrationAccount,
pub synced_at: Option<DateTime<Utc>>,
pub subscriber_count: Option<f64>,
pub revoked: Option<bool>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub application: Option<Arc<RwLock<Application>>>,
pub application: Option<Shared<Application>>,
pub scopes: Option<Vec<String>>,
}

View File

@ -3,6 +3,7 @@ use std::sync::{Arc, RwLock};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::gateway::Shared;
use crate::types::{Snowflake, WelcomeScreenObject};
use super::guild::GuildScheduledEvent;
@ -21,7 +22,7 @@ pub struct Invite {
pub flags: Option<i32>,
pub guild: Option<InviteGuild>,
pub guild_id: Option<Snowflake>,
pub guild_scheduled_event: Option<Arc<RwLock<GuildScheduledEvent>>>,
pub guild_scheduled_event: Option<Shared<GuildScheduledEvent>>,
#[serde(rename = "type")]
pub invite_type: Option<i32>,
pub inviter: Option<User>,
@ -59,7 +60,7 @@ pub struct InviteGuild {
/// See <https://discord-userdoccers.vercel.app/resources/invite#invite-stage-instance-object>
#[derive(Debug, Serialize, Deserialize)]
pub struct InviteStageInstance {
pub members: Vec<Arc<RwLock<GuildMember>>>,
pub members: Vec<Shared<GuildMember>>,
pub participant_count: i32,
pub speaker_count: i32,
pub topic: String,

View File

@ -2,6 +2,7 @@ use std::sync::{Arc, RwLock};
use serde::{Deserialize, Serialize};
use crate::gateway::Shared;
use crate::types::{
entities::{
Application, Attachment, Channel, Emoji, GuildMember, PublicUser, RoleSubscriptionData,
@ -121,7 +122,7 @@ pub struct MessageInteraction {
pub interaction_type: u8,
pub name: String,
pub user: User,
pub member: Option<Arc<RwLock<GuildMember>>>,
pub member: Option<Shared<GuildMember>>,
}
#[derive(Debug, Default, PartialEq, Clone, Serialize, Deserialize, Eq, PartialOrd, Ord)]

View File

@ -24,7 +24,6 @@ pub use voice_state::*;
pub use webhook::*;
use crate::gateway::Shared;
#[cfg(feature = "client")]
use crate::gateway::Updateable;
@ -36,7 +35,6 @@ use async_trait::async_trait;
#[cfg(feature = "client")]
use std::fmt::Debug;
#[cfg(feature = "client")]
use std::sync::{Arc, RwLock};

View File

@ -4,6 +4,7 @@ use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use crate::gateway::Shared;
use crate::types::Snowflake;
use super::PublicUser;
@ -15,7 +16,7 @@ pub struct Relationship {
#[serde(rename = "type")]
pub relationship_type: RelationshipType,
pub nickname: Option<String>,
pub user: Arc<RwLock<PublicUser>>,
pub user: Shared<PublicUser>,
pub since: Option<DateTime<Utc>>,
}

View File

@ -2,6 +2,7 @@ use std::sync::{Arc, RwLock};
use serde::{Deserialize, Serialize};
use crate::gateway::Shared;
use crate::types::{entities::User, utils::Snowflake};
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -24,7 +25,7 @@ pub struct Sticker {
pub available: Option<bool>,
pub guild_id: Option<Snowflake>,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub user: Option<Arc<RwLock<User>>>,
pub user: Option<Shared<User>>,
pub sort_value: Option<u8>,
}

View File

@ -2,6 +2,7 @@ use std::sync::{Arc, RwLock};
use serde::{Deserialize, Serialize};
use crate::gateway::Shared;
use crate::types::entities::User;
use crate::types::Snowflake;
@ -21,5 +22,5 @@ pub struct TeamMember {
pub membership_state: u8,
pub permissions: Vec<String>,
pub team_id: Snowflake,
pub user: Arc<RwLock<User>>,
pub user: Shared<User>,
}

View File

@ -3,6 +3,7 @@ use std::sync::{Arc, RwLock};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::gateway::Shared;
use crate::types::{
entities::{Guild, User},
utils::Snowflake,
@ -18,13 +19,13 @@ pub struct GuildTemplate {
pub usage_count: Option<u64>,
pub creator_id: Snowflake,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub creator: Arc<RwLock<User>>,
pub creator: Shared<User>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub source_guild_id: Snowflake,
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub source_guild: Vec<Arc<RwLock<Guild>>>,
pub source_guild: Vec<Shared<Guild>>,
// Unsure how a {recursive: Guild} looks like, might be a Vec?
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub serialized_source_guild: Vec<Arc<RwLock<Guild>>>,
pub serialized_source_guild: Vec<Shared<Guild>>,
}

View File

@ -3,6 +3,8 @@ use std::sync::{Arc, RwLock};
use chrono::{serde::ts_milliseconds_option, Utc};
use serde::{Deserialize, Serialize};
use crate::gateway::Shared;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
#[serde(rename_all = "lowercase")]
@ -77,7 +79,7 @@ pub struct UserSettings {
#[cfg(not(feature = "sqlx"))]
pub restricted_guilds: Vec<String>,
pub show_current_game: bool,
pub status: Arc<RwLock<UserStatus>>,
pub status: Shared<UserStatus>,
pub stream_notifications_enabled: bool,
pub theme: UserTheme,
pub timezone_offset: i16,
@ -153,5 +155,5 @@ pub struct GuildFolder {
#[derive(Debug, Serialize, Deserialize)]
pub struct LoginResult {
pub token: String,
pub settings: Arc<RwLock<UserSettings>>,
pub settings: Shared<UserSettings>,
}

View File

@ -3,6 +3,7 @@ use std::sync::{Arc, RwLock};
#[cfg(feature = "client")]
use chorus_macros::Composite;
use crate::gateway::Shared;
#[cfg(feature = "client")]
use crate::types::Composite;
@ -33,7 +34,7 @@ pub struct VoiceState {
pub guild: Option<Guild>,
pub channel_id: Option<Snowflake>,
pub user_id: Snowflake,
pub member: Option<Arc<RwLock<GuildMember>>>,
pub member: Option<Shared<GuildMember>>,
pub session_id: String,
pub token: Option<String>,
pub deaf: bool,

View File

@ -3,6 +3,7 @@ use std::sync::{Arc, RwLock};
use serde::{Deserialize, Serialize};
use crate::gateway::Shared;
#[cfg(feature = "client")]
use crate::gateway::Updateable;
@ -36,10 +37,10 @@ pub struct Webhook {
pub application_id: Snowflake,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub user: Option<Arc<RwLock<User>>>,
pub user: Option<Shared<User>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "sqlx", sqlx(skip))]
pub source_guild: Option<Arc<RwLock<Guild>>>,
pub source_guild: Option<Shared<Guild>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
}

View File

@ -1,4 +1,5 @@
use crate::types::events::WebSocketEvent;
use crate::types::IntoShared;
use crate::types::{entities::Channel, JsonField, Snowflake, SourceUrlField};
use chorus_macros::{JsonField, SourceUrlField};
use chrono::{DateTime, Utc};
@ -8,7 +9,7 @@ use serde::{Deserialize, Serialize};
use super::UpdateMessage;
#[cfg(feature = "client")]
use std::sync::{Arc, RwLock};
use crate::gateway::Shared;
#[cfg(feature = "client")]
use crate::types::Guild;
@ -42,9 +43,9 @@ impl UpdateMessage<Guild> for ChannelCreate {
self.channel.guild_id
}
fn update(&mut self, object_to_update: Arc<RwLock<Guild>>) {
fn update(&mut self, object_to_update: Shared<Guild>) {
let mut write = object_to_update.write().unwrap();
let update = Arc::new(RwLock::new(self.channel.clone()));
let update = self.channel.clone().into_shared();
if write.channels.is_some() {
write.channels.as_mut().unwrap().push(update);
} else {
@ -68,7 +69,7 @@ impl WebSocketEvent for ChannelUpdate {}
#[cfg(feature = "client")]
impl UpdateMessage<Channel> for ChannelUpdate {
fn update(&mut self, object_to_update: Arc<RwLock<Channel>>) {
fn update(&mut self, object_to_update: Shared<Channel>) {
let mut write = object_to_update.write().unwrap();
*write = self.channel.clone();
}
@ -114,7 +115,7 @@ impl UpdateMessage<Guild> for ChannelDelete {
self.channel.guild_id
}
fn update(&mut self, object_to_update: Arc<RwLock<Guild>>) {
fn update(&mut self, object_to_update: Shared<Guild>) {
if self.id().is_none() {
return;
}

View File

@ -5,8 +5,8 @@ use serde::{Deserialize, Serialize};
use crate::types::entities::{Guild, PublicUser, UnavailableGuild};
use crate::types::events::WebSocketEvent;
use crate::types::{
AuditLogEntry, Emoji, GuildMember, GuildScheduledEvent, JsonField, RoleObject, Snowflake,
SourceUrlField, Sticker,
AuditLogEntry, Emoji, GuildMember, GuildScheduledEvent, IntoShared, JsonField, RoleObject,
Snowflake, SourceUrlField, Sticker,
};
use super::PresenceUpdate;
@ -14,7 +14,7 @@ use super::PresenceUpdate;
#[cfg(feature = "client")]
use super::UpdateMessage;
#[cfg(feature = "client")]
use std::sync::{Arc, RwLock};
use crate::gateway::Shared;
#[derive(Debug, Deserialize, Serialize, Default, Clone, SourceUrlField, JsonField)]
/// See <https://discord.com/developers/docs/topics/gateway-events#guild-create>;
@ -38,7 +38,7 @@ impl UpdateMessage<Guild> for GuildCreate {
}
}
fn update(&mut self, _: Arc<RwLock<Guild>>) {}
fn update(&mut self, _: Shared<Guild>) {}
}
#[derive(Debug, Deserialize, Serialize, Clone)]
@ -114,7 +114,7 @@ impl UpdateMessage<Guild> for GuildDelete {
fn id(&self) -> Option<Snowflake> {
Some(self.guild.id)
}
fn update(&mut self, _: Arc<RwLock<Guild>>) {}
fn update(&mut self, _: Shared<Guild>) {}
}
impl WebSocketEvent for GuildDelete {}
@ -229,16 +229,16 @@ impl UpdateMessage<Guild> for GuildRoleCreate {
Some(self.guild_id)
}
fn update(&mut self, object_to_update: Arc<RwLock<Guild>>) {
fn update(&mut self, object_to_update: Shared<Guild>) {
let mut object_to_update = object_to_update.write().unwrap();
if object_to_update.roles.is_some() {
object_to_update
.roles
.as_mut()
.unwrap()
.push(Arc::new(RwLock::new(self.role.clone())));
.push(self.role.clone().into_shared());
} else {
object_to_update.roles = Some(Vec::from([Arc::new(RwLock::new(self.role.clone()))]));
object_to_update.roles = Some(Vec::from([self.role.clone().into_shared()]));
}
}
}
@ -262,7 +262,7 @@ impl UpdateMessage<RoleObject> for GuildRoleUpdate {
Some(self.role.id)
}
fn update(&mut self, object_to_update: Arc<RwLock<RoleObject>>) {
fn update(&mut self, object_to_update: Shared<RoleObject>) {
let mut write = object_to_update.write().unwrap();
*write = self.role.clone();
}

View File

@ -39,9 +39,9 @@ use serde_json::{from_str, from_value, to_value, Value};
#[cfg(feature = "client")]
use std::collections::HashMap;
use std::fmt::Debug;
#[cfg(feature = "client")]
use std::sync::{Arc, RwLock};
use crate::gateway::Shared;
use std::fmt::Debug;
#[cfg(feature = "client")]
use serde::de::DeserializeOwned;
@ -132,7 +132,7 @@ pub(crate) trait UpdateMessage<T>: Clone + JsonField + SourceUrlField
where
T: Updateable + Serialize + DeserializeOwned + Clone,
{
fn update(&mut self, object_to_update: Arc<RwLock<T>>) {
fn update(&mut self, object_to_update: Shared<T>) {
update_object(self.get_json(), object_to_update)
}
fn id(&self) -> Option<Snowflake>;
@ -152,8 +152,10 @@ pub trait SourceUrlField: Clone {
/// Only applicable for events where the Update struct is the same as the Entity struct
pub(crate) fn update_object(
value: String,
object: Arc<RwLock<(impl Updateable + Serialize + DeserializeOwned + Clone)>>,
object: Shared<(impl Updateable + Serialize + DeserializeOwned + Clone)>,
) {
use crate::gateway::Shared;
let data_from_event: HashMap<String, Value> = from_str(&value).unwrap();
let mut original_data: HashMap<String, Value> =
from_value(to_value(object.clone()).unwrap()).unwrap();