diff options
| author | ilotterytea <iltsu@alright.party> | 2025-04-09 02:36:05 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-04-09 02:36:05 +0500 |
| commit | a4ce687ddd119ff97ac1efd3b23d0c27013845f4 (patch) | |
| tree | beb652ec216f425ab18bddb0efcfa2fd088c115e | |
| parent | 93e26506c0211a3ebefa958e1df0d8cfbfc12b03 (diff) | |
upd: function signatures for RetrieveEmoteWS (attempt 2)
| -rw-r--r-- | src/betterttv.rs | 46 | ||||
| -rw-r--r-- | src/emotes.rs | 15 | ||||
| -rw-r--r-- | src/seventv.rs | 46 |
3 files changed, 54 insertions, 53 deletions
diff --git a/src/betterttv.rs b/src/betterttv.rs index 2d8134b..0dbd4a3 100644 --- a/src/betterttv.rs +++ b/src/betterttv.rs @@ -1,4 +1,4 @@ -use std::collections::HashSet; +use std::{collections::HashSet, sync::Arc}; use futures::SinkExt; use reqwest::{Client, Error}; @@ -8,7 +8,7 @@ use tokio::net::TcpStream; use tokio_tungstenite::{MaybeTlsStream, WebSocketStream, connect_async, tungstenite::Result}; use tungstenite::Message; -use crate::emotes::{Emote, EmoteBase, RetrieveEmoteAPI, RetrieveEmoteWS}; +use crate::emotes::{Emote, EmoteBase, RetrieveEmoteAPI, RetrieveEmoteHandler, RetrieveEmoteWS}; #[derive(Debug, Deserialize, Clone)] pub struct BetterTTVEmote { @@ -94,40 +94,40 @@ impl RetrieveEmoteAPI<BetterTTVEmote> for BetterTTVAPIClient { } } -pub struct BetterTTVWSClient<F> -where - F: Fn(String, Option<String>, Emote), -{ +pub struct BetterTTVWSClient { url: String, - on_emote_create: Option<F>, - on_emote_update: Option<F>, - on_emote_delete: Option<F>, + on_emote_create: Option<RetrieveEmoteHandler<Emote>>, + on_emote_update: Option<RetrieveEmoteHandler<Emote>>, + on_emote_delete: Option<RetrieveEmoteHandler<Emote>>, joined_channels: HashSet<usize>, awaiting_channels: HashSet<usize>, } -impl<F> RetrieveEmoteWS<Emote, F> for BetterTTVWSClient<F> -where - F: Fn(String, Option<String>, Emote), -{ - fn on_emote_create(&mut self, func: F) { - self.on_emote_create = Some(func); +impl RetrieveEmoteWS<Emote> for BetterTTVWSClient { + fn on_emote_create( + &mut self, + func: impl Fn(String, Option<String>, Emote) + Send + Sync + 'static, + ) { + self.on_emote_create = Some(Arc::new(func)); } - fn on_emote_update(&mut self, func: F) { - self.on_emote_update = Some(func); + fn on_emote_update( + &mut self, + func: impl Fn(String, Option<String>, Emote) + Send + Sync + 'static, + ) { + self.on_emote_update = Some(Arc::new(func)); } - fn on_emote_delete(&mut self, func: F) { - self.on_emote_delete = Some(func); + fn on_emote_delete( + &mut self, + func: impl Fn(String, Option<String>, Emote) + Send + Sync + 'static, + ) { + self.on_emote_delete = Some(Arc::new(func)); } } -impl<F> BetterTTVWSClient<F> -where - F: Fn(String, Option<String>, Emote), -{ +impl BetterTTVWSClient { pub async fn new() -> Result<(WebSocketStream<MaybeTlsStream<TcpStream>>, Self)> { let url = "wss://sockets.betterttv.net/ws"; diff --git a/src/emotes.rs b/src/emotes.rs index 53fd5ad..b1beb9d 100644 --- a/src/emotes.rs +++ b/src/emotes.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use reqwest::Error; use serde::{Deserialize, Serialize}; @@ -33,11 +35,10 @@ pub trait RetrieveEmoteAPI<T> { async fn get_global_emotes(&self) -> Result<Vec<T>, Error>; } -pub trait RetrieveEmoteWS<T, F> -where - F: Fn(String, Option<String>, T), -{ - fn on_emote_create(&mut self, func: F); - fn on_emote_update(&mut self, func: F); - fn on_emote_delete(&mut self, func: F); +pub type RetrieveEmoteHandler<T> = Arc<dyn Fn(String, Option<String>, T) + Send + Sync>; + +pub trait RetrieveEmoteWS<T> { + fn on_emote_create(&mut self, func: impl Fn(String, Option<String>, T) + Send + Sync + 'static); + fn on_emote_update(&mut self, func: impl Fn(String, Option<String>, T) + Send + Sync + 'static); + fn on_emote_delete(&mut self, func: impl Fn(String, Option<String>, T) + Send + Sync + 'static); } diff --git a/src/seventv.rs b/src/seventv.rs index fd44aeb..e2b42ab 100644 --- a/src/seventv.rs +++ b/src/seventv.rs @@ -1,4 +1,4 @@ -use std::collections::HashSet; +use std::{collections::HashSet, sync::Arc}; use futures::SinkExt; use reqwest::{Client, Error}; @@ -10,7 +10,7 @@ use tokio_tungstenite::{ }; use tungstenite::{Message, Result, protocol::WebSocketConfig}; -use crate::emotes::{Emote, RetrieveEmoteAPI, RetrieveEmoteWS}; +use crate::emotes::{Emote, RetrieveEmoteAPI, RetrieveEmoteHandler, RetrieveEmoteWS}; #[derive(Deserialize, Serialize, Clone, Debug)] pub struct User { @@ -193,14 +193,11 @@ impl RetrieveEmoteAPI<Emote> for SevenTVAPIClient { } } -pub struct SevenTVWSClient<F> -where - F: Fn(String, Option<String>, Emote), -{ +pub struct SevenTVWSClient { url: String, - on_emote_create: Option<F>, - on_emote_update: Option<F>, - on_emote_delete: Option<F>, + on_emote_create: Option<RetrieveEmoteHandler<Emote>>, + on_emote_update: Option<RetrieveEmoteHandler<Emote>>, + on_emote_delete: Option<RetrieveEmoteHandler<Emote>>, joined_channels: HashSet<String>, awaiting_channels: HashSet<String>, @@ -208,27 +205,30 @@ where identified: bool, } -impl<F> RetrieveEmoteWS<Emote, F> for SevenTVWSClient<F> -where - F: Fn(String, Option<String>, Emote), -{ - fn on_emote_create(&mut self, func: F) { - self.on_emote_create = Some(func); +impl RetrieveEmoteWS<Emote> for SevenTVWSClient { + fn on_emote_create( + &mut self, + func: impl Fn(String, Option<String>, Emote) + Send + Sync + 'static, + ) { + self.on_emote_create = Some(Arc::new(func)); } - fn on_emote_update(&mut self, func: F) { - self.on_emote_update = Some(func); + fn on_emote_update( + &mut self, + func: impl Fn(String, Option<String>, Emote) + Send + Sync + 'static, + ) { + self.on_emote_update = Some(Arc::new(func)); } - fn on_emote_delete(&mut self, func: F) { - self.on_emote_delete = Some(func); + fn on_emote_delete( + &mut self, + func: impl Fn(String, Option<String>, Emote) + Send + Sync + 'static, + ) { + self.on_emote_delete = Some(Arc::new(func)); } } -impl<F> SevenTVWSClient<F> -where - F: Fn(String, Option<String>, Emote), -{ +impl SevenTVWSClient { pub async fn new() -> Result<(WebSocketStream<MaybeTlsStream<TcpStream>>, Self)> { let url = "wss://events.7tv.io/v3"; |
