Revert "Give `GatewayHandle` and `Gateway` common trait to call `watch_whole()` or observe() from a `Gateway`"

This reverts commit 8e2e0d137d.
This commit is contained in:
bitfl0wer 2023-08-16 00:21:18 +02:00
parent 10c3936514
commit 497fd56ab0
No known key found for this signature in database
GPG Key ID: 0ACD574FCF5226CF
12 changed files with 43 additions and 99 deletions

View File

@ -105,7 +105,7 @@ pub fn composite_derive(input: TokenStream) -> TokenStream {
let expanded = quote! {
#[async_trait::async_trait(?Send)]
impl<T: Updateable + Clone> Composite<T> for #ident {
async fn watch_whole(self, gateway: &(impl GatewayObject + ?Sized)) -> Self {
async fn watch_whole(self, gateway: &GatewayHandle) -> Self {
Self {
#(#field_exprs,)*
}

View File

@ -180,16 +180,32 @@ pub trait Updateable: 'static + Send + Sync {
fn id(&self) -> Snowflake;
}
#[async_trait(?Send)]
pub trait GatewayObject {
fn store(&self) -> Arc<Mutex<HashMap<Snowflake, Box<dyn Send + Any>>>>;
fn events(&self) -> Arc<Mutex<Events>>;
async fn observe<T: Updateable + Clone + Composite<T>>(
impl GatewayHandle {
/// Sends json to the gateway with an opcode
async fn send_json_event(&self, op_code: u8, to_send: serde_json::Value) {
let gateway_payload = types::GatewaySendPayload {
op_code,
event_data: Some(to_send),
sequence_number: None,
};
let payload_json = serde_json::to_string(&gateway_payload).unwrap();
let message = tokio_tungstenite::tungstenite::Message::text(payload_json);
self.websocket_send
.lock()
.await
.send(message)
.await
.unwrap();
}
pub async fn observe<T: Updateable + Clone + Composite<T>>(
&self,
object: Arc<RwLock<T>>,
) -> watch::Receiver<Arc<RwLock<T>>> {
let store = self.store();
let mut store = store.lock().await;
let mut store = self.store.lock().await;
let id = object.read().unwrap().id();
if let Some(channel) = store.get(&id) {
let (_, rx) = channel
@ -214,7 +230,8 @@ pub trait GatewayObject {
receiver
}
}
async fn observe_and_get<T: Updateable + Clone + Composite<T>>(
pub async fn observe_and_get<T: Updateable + Clone + Composite<T>>(
&self,
object: Arc<RwLock<T>>,
) -> Arc<RwLock<T>> {
@ -222,50 +239,6 @@ pub trait GatewayObject {
let object = channel.borrow().clone();
object
}
}
#[async_trait(?Send)]
impl GatewayObject for GatewayHandle {
fn store(&self) -> Arc<Mutex<HashMap<Snowflake, Box<dyn Send + Any>>>> {
self.store.clone()
}
fn events(&self) -> Arc<Mutex<Events>> {
self.events.clone()
}
}
#[async_trait(?Send)]
impl GatewayObject for Gateway {
fn store(&self) -> Arc<Mutex<HashMap<Snowflake, Box<dyn Send + Any>>>> {
self.store.clone()
}
fn events(&self) -> Arc<Mutex<Events>> {
self.events.clone()
}
}
impl GatewayHandle {
/// Sends json to the gateway with an opcode
async fn send_json_event(&self, op_code: u8, to_send: serde_json::Value) {
let gateway_payload = types::GatewaySendPayload {
op_code,
event_data: Some(to_send),
sequence_number: None,
};
let payload_json = serde_json::to_string(&gateway_payload).unwrap();
let message = tokio_tungstenite::tungstenite::Message::text(payload_json);
self.websocket_send
.lock()
.await
.send(message)
.await
.unwrap();
}
/// Sends an identify event to the gateway
pub async fn send_identify(&self, to_send: types::GatewayIdentifyPayload) {

View File

@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use serde_aux::prelude::deserialize_string_from_number;
use serde_repr::{Deserialize_repr, Serialize_repr};
use crate::gateway::{GatewayObject, Updateable};
use crate::gateway::{GatewayHandle, Updateable};
use crate::types::{
entities::{GuildMember, User},
utils::Snowflake,

View File

@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock};
use chorus_macros::{Composite, Updateable};
use serde::{Deserialize, Serialize};
use crate::gateway::{GatewayObject, Updateable};
use crate::gateway::{GatewayHandle, Updateable};
use crate::types::entities::User;
use crate::types::{Composite, Snowflake};

View File

@ -5,7 +5,7 @@ use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use crate::gateway::{GatewayObject, Updateable};
use crate::gateway::{GatewayHandle, Updateable};
use crate::types::types::guild_configuration::GuildFeaturesList;
use crate::types::{
entities::{Channel, Emoji, RoleObject, Sticker, User, VoiceState, Webhook},

View File

@ -22,7 +22,7 @@ pub use user_settings::*;
pub use voice_state::*;
pub use webhook::*;
use crate::gateway::{GatewayObject, Updateable};
use crate::gateway::{GatewayHandle, Updateable};
use async_trait::async_trait;
use std::sync::{Arc, RwLock};
@ -52,11 +52,11 @@ mod webhook;
#[async_trait(?Send)]
pub trait Composite<T: Updateable + Clone> {
async fn watch_whole(self, gateway: &(impl GatewayObject + ?Sized)) -> Self;
async fn watch_whole(self, gateway: &GatewayHandle) -> Self;
async fn option_observe_fn(
value: Option<Arc<RwLock<T>>>,
gateway: &(impl GatewayObject + ?Sized),
gateway: &GatewayHandle,
) -> Option<Arc<RwLock<T>>>
where
T: Composite<T>,
@ -71,7 +71,7 @@ pub trait Composite<T: Updateable + Clone> {
async fn option_vec_observe_fn(
value: Option<Vec<Arc<RwLock<T>>>>,
gateway: &(impl GatewayObject + ?Sized),
gateway: &GatewayHandle,
) -> Option<Vec<Arc<RwLock<T>>>>
where
T: Composite<T>,
@ -87,10 +87,7 @@ pub trait Composite<T: Updateable + Clone> {
}
}
async fn value_observe_fn(
value: Arc<RwLock<T>>,
gateway: &(impl GatewayObject + ?Sized),
) -> Arc<RwLock<T>>
async fn value_observe_fn(value: Arc<RwLock<T>>, gateway: &GatewayHandle) -> Arc<RwLock<T>>
where
T: Composite<T>,
{
@ -99,7 +96,7 @@ pub trait Composite<T: Updateable + Clone> {
async fn vec_observe_fn(
value: Vec<Arc<RwLock<T>>>,
gateway: &(impl GatewayObject + ?Sized),
gateway: &GatewayHandle,
) -> Vec<Arc<RwLock<T>>>
where
T: Composite<T>,

View File

@ -3,7 +3,7 @@ use chorus_macros::{Composite, Updateable};
use serde::{Deserialize, Serialize};
use serde_aux::prelude::{deserialize_option_number_from_string, deserialize_string_from_number};
use crate::gateway::{GatewayObject, Updateable};
use crate::gateway::{GatewayHandle, Updateable};
use crate::types::{utils::Snowflake, Composite};
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Updateable, Composite)]

View File

@ -3,7 +3,7 @@ use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_aux::prelude::deserialize_option_number_from_string;
use crate::gateway::{GatewayObject, Updateable};
use crate::gateway::{GatewayHandle, Updateable};
use crate::types::{utils::Snowflake, Composite};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]

View File

@ -4,7 +4,7 @@ use chorus_macros::{Composite, Updateable};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::gateway::{GatewayObject, Updateable};
use crate::gateway::{GatewayHandle, Updateable};
use crate::types::{
entities::{Guild, GuildMember},
utils::Snowflake,

View File

@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock};
use chorus_macros::{Composite, Updateable};
use serde::{Deserialize, Serialize};
use crate::gateway::{GatewayObject, Updateable};
use crate::gateway::{GatewayHandle, Updateable};
use crate::types::{
entities::{Guild, User},
utils::Snowflake,

View File

@ -1,17 +1,13 @@
use std::sync::{Arc, RwLock};
use chorus_macros::JsonField;
use chrono::{DateTime, Utc};
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,
Sticker,
AuditLogEntry, Emoji, GuildMember, GuildScheduledEvent, RoleObject, Snowflake, Sticker,
};
use super::{PresenceUpdate, UpdateMessage};
use super::PresenceUpdate;
#[derive(Debug, Deserialize, Serialize, Default, Clone)]
/// See <https://discord.com/developers/docs/topics/gateway-events#guild-create>;
@ -168,34 +164,15 @@ pub struct GuildMembersChunk {
impl WebSocketEvent for GuildMembersChunk {}
#[derive(Debug, Default, Deserialize, Serialize, Clone, JsonField)]
#[derive(Debug, Default, Deserialize, Serialize, Clone)]
/// See <https://discord.com/developers/docs/topics/gateway-events#guild-role-create>
pub struct GuildRoleCreate {
pub guild_id: Snowflake,
pub role: RoleObject,
#[serde(skip)]
pub json: String,
}
impl WebSocketEvent for GuildRoleCreate {}
impl UpdateMessage<Guild> for GuildRoleCreate {
fn id(&self) -> Snowflake {
self.role.id
}
fn update(&mut self, object_to_update: std::sync::Arc<std::sync::RwLock<Guild>>) {
let mut write = object_to_update.write().unwrap();
if write.roles.is_some() {
write
.roles
.as_mut()
.unwrap()
.push(Arc::new(RwLock::new(self.role.clone())));
}
}
}
#[derive(Debug, Default, Deserialize, Serialize, Clone)]
/// See <https://discord.com/developers/docs/topics/gateway-events#guild-role-update>
pub struct GuildRoleUpdate {

View File

@ -1,7 +1,7 @@
mod common;
use chorus::gateway::*;
use chorus::types::{self, Channel, ChannelModifySchema, Guild};
use chorus::types::{self, Channel, ChannelModifySchema};
#[tokio::test]
/// Tests establishing a connection (hello and heartbeats) on the local gateway;
@ -63,6 +63,3 @@ async fn test_self_updating_structs() {
common::teardown(bundle).await
}
#[tokio::test]
async fn test_recursive_self_updating_structs() {}