From 5372d2c475b41dc04342ed8316df47a93725e01d Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Mon, 22 Jan 2024 14:56:23 +0100 Subject: [PATCH] Make IntoShared trait with blanket implementation --- src/types/entities/mod.rs | 24 ++++++++++++++---------- tests/common/mod.rs | 8 ++++---- tests/gateway.rs | 10 +++++++--- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/types/entities/mod.rs b/src/types/entities/mod.rs index ee31bf4..d314d82 100644 --- a/src/types/entities/mod.rs +++ b/src/types/entities/mod.rs @@ -122,14 +122,18 @@ pub trait Composite { } } -/// Uses [`Shared`] to provide an ergonomic alternative to `Arc::new(RwLock::new(obj))`. -/// -/// [`Shared`] can then be observed using the [`Gateway`], turning the underlying -/// `dyn Composite` into a self-updating struct, which is a tracked variant of a chorus -/// entity struct, updating its' held information when new information concerning itself arrives -/// over the [`Gateway`] connection, reducing the need for expensive network-API calls. -pub fn into_shared + Updateable + Clone + Debug + Sized>( - composite: T, -) -> Shared { - Arc::new(RwLock::new(composite)) +pub trait IntoShared { + /// Uses [`Shared`] to provide an ergonomic alternative to `Arc::new(RwLock::new(obj))`. + /// + /// [`Shared`] can then be observed using the [`Gateway`], turning the underlying + /// `dyn Composite` into a self-updating struct, which is a tracked variant of a chorus + /// entity struct, updating its' held information when new information concerning itself arrives + /// over the [`Gateway`] connection, reducing the need for expensive network-API calls. + fn into_shared(self) -> Shared; +} + +impl + Updateable + Clone + Debug + ?Sized> IntoShared for T { + fn into_shared(self) -> Shared { + Arc::new(RwLock::new(self)) + } } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index fe064fb..e18f92b 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,5 +1,5 @@ use chorus::gateway::{Gateway, Shared}; -use chorus::types::into_shared; +use chorus::types::IntoShared; use chorus::{ instance::{ChorusUser, Instance}, types::{ @@ -118,9 +118,9 @@ pub(crate) async fn setup() -> TestBundle { urls, user, instance, - guild: into_shared(guild), - role: into_shared(role), - channel: into_shared(channel), + guild: guild.into_shared(), + role: role.into_shared(), + channel: channel.into_shared(), } } diff --git a/tests/gateway.rs b/tests/gateway.rs index 84361fb..66259f7 100644 --- a/tests/gateway.rs +++ b/tests/gateway.rs @@ -2,7 +2,7 @@ mod common; use chorus::errors::GatewayError; use chorus::gateway::*; -use chorus::types::{self, into_shared, ChannelModifySchema, RoleCreateModifySchema, RoleObject}; +use chorus::types::{self, ChannelModifySchema, IntoShared, RoleCreateModifySchema, RoleObject}; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; #[cfg(target_arch = "wasm32")] @@ -95,7 +95,11 @@ async fn test_recursive_self_updating_structs() { .await .unwrap(); // 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 let inner_guild = guild.read().unwrap().clone(); assert!(inner_guild.roles.is_some()); @@ -107,7 +111,7 @@ async fn test_recursive_self_updating_structs() { let role_inner = bundle .user .gateway - .observe_and_into_inner(into_shared(role.clone())) + .observe_and_into_inner(role.clone().into_shared()) .await; assert_eq!(role_inner.name, "yippieee"); // Check if the change propagated