Make IntoShared trait with blanket implementation

This commit is contained in:
bitfl0wer 2024-01-22 14:56:23 +01:00
parent c4bea069b7
commit b521928f81
3 changed files with 25 additions and 17 deletions

View File

@ -122,14 +122,18 @@ pub trait Composite<T: Updateable + Clone + Debug> {
} }
} }
/// Uses [`Shared`] to provide an ergonomic alternative to `Arc::new(RwLock::new(obj))`. pub trait IntoShared {
/// /// Uses [`Shared`] to provide an ergonomic alternative to `Arc::new(RwLock::new(obj))`.
/// [`Shared<Self>`] can then be observed using the [`Gateway`], turning the underlying ///
/// `dyn Composite<Self>` into a self-updating struct, which is a tracked variant of a chorus /// [`Shared<Self>`] can then be observed using the [`Gateway`], turning the underlying
/// entity struct, updating its' held information when new information concerning itself arrives /// `dyn Composite<Self>` into a self-updating struct, which is a tracked variant of a chorus
/// over the [`Gateway`] connection, reducing the need for expensive network-API calls. /// entity struct, updating its' held information when new information concerning itself arrives
pub fn into_shared<T: Composite<T> + Updateable + Clone + Debug + Sized>( /// over the [`Gateway`] connection, reducing the need for expensive network-API calls.
composite: T, fn into_shared(self) -> Shared<Self>;
) -> Shared<T> { }
Arc::new(RwLock::new(composite))
impl<T: Composite<T> + Updateable + Clone + Debug + ?Sized> IntoShared for T {
fn into_shared(self) -> Shared<Self> {
Arc::new(RwLock::new(self))
}
} }

View File

@ -1,5 +1,5 @@
use chorus::gateway::{Gateway, Shared}; use chorus::gateway::{Gateway, Shared};
use chorus::types::into_shared; use chorus::types::IntoShared;
use chorus::{ use chorus::{
instance::{ChorusUser, Instance}, instance::{ChorusUser, Instance},
types::{ types::{
@ -118,9 +118,9 @@ pub(crate) async fn setup() -> TestBundle {
urls, urls,
user, user,
instance, instance,
guild: into_shared(guild), guild: guild.into_shared(),
role: into_shared(role), role: role.into_shared(),
channel: into_shared(channel), channel: channel.into_shared(),
} }
} }

View File

@ -2,7 +2,7 @@ mod common;
use chorus::errors::GatewayError; use chorus::errors::GatewayError;
use chorus::gateway::*; use chorus::gateway::*;
use chorus::types::{self, into_shared, ChannelModifySchema, RoleCreateModifySchema, RoleObject}; use chorus::types::{self, ChannelModifySchema, IntoShared, RoleCreateModifySchema, RoleObject};
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*; use wasm_bindgen_test::*;
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
@ -95,7 +95,11 @@ async fn test_recursive_self_updating_structs() {
.await .await
.unwrap(); .unwrap();
// Watch role; // Watch role;
bundle.user.gateway.observe(into_shared(role.clone())).await; bundle
.user
.gateway
.observe(role.clone().into_shared())
.await;
// Update Guild and check for Guild // Update Guild and check for Guild
let inner_guild = guild.read().unwrap().clone(); let inner_guild = guild.read().unwrap().clone();
assert!(inner_guild.roles.is_some()); assert!(inner_guild.roles.is_some());
@ -107,7 +111,7 @@ async fn test_recursive_self_updating_structs() {
let role_inner = bundle let role_inner = bundle
.user .user
.gateway .gateway
.observe_and_into_inner(into_shared(role.clone())) .observe_and_into_inner(role.clone().into_shared())
.await; .await;
assert_eq!(role_inner.name, "yippieee"); assert_eq!(role_inner.name, "yippieee");
// Check if the change propagated // Check if the change propagated