From d28f19d8ca50d14cdb79035e5f146786d7eb020e Mon Sep 17 00:00:00 2001 From: fowb Date: Sat, 12 Aug 2023 19:47:11 +0200 Subject: [PATCH] Change observe() to take Arc> --- src/gateway.rs | 18 ++++++++++++------ tests/gateway.rs | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/gateway.rs b/src/gateway.rs index eaac29f..f147584 100644 --- a/src/gateway.rs +++ b/src/gateway.rs @@ -6,7 +6,7 @@ use async_trait::async_trait; use std::any::Any; use std::collections::HashMap; use std::fmt::Debug; -use std::sync::Arc; +use std::sync::{Arc, RwLock}; use std::time::Duration; use tokio::sync::watch; use tokio::time::sleep_until; @@ -196,20 +196,26 @@ impl GatewayHandle { .unwrap(); } - pub async fn observe(&self, object: T) -> watch::Receiver { + pub async fn observe( + &self, + object: Arc>, + ) -> watch::Receiver>> { let mut store = self.store.lock().await; - if let Some(channel) = store.get(&object.id()) { + if let Some(channel) = store.get(&object.clone().read().unwrap().id()) { let (_, rx) = channel - .downcast_ref::<(watch::Sender, watch::Receiver)>() + .downcast_ref::<( + watch::Sender>>, + watch::Receiver>>, + )>() .unwrap_or_else(|| { panic!( "Snowflake {} already exists in the store, but it is not of type T.", - object.id() + object.read().unwrap().id() ) }); rx.clone() } else { - let id = object.id(); + let id = object.read().unwrap().id(); let channel = watch::channel(object); let receiver = channel.1.clone(); store.insert(id, Box::new(channel)); diff --git a/tests/gateway.rs b/tests/gateway.rs index 21a2018..15f58d8 100644 --- a/tests/gateway.rs +++ b/tests/gateway.rs @@ -29,7 +29,7 @@ async fn test_gateway_authenticate() { #[tokio::test] async fn test_self_updating_structs() { let mut bundle = common::setup().await; - let channel_updater = bundle.user.gateway.observe(bundle.channel.clone()).await; + let channel_updater = bundle.user.gateway.observe(bundle.channel).await; let received_channel = channel_updater.borrow().clone(); assert_eq!(received_channel, bundle.channel); let channel = &mut bundle.channel;