From a4ce687ddd119ff97ac1efd3b23d0c27013845f4 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Wed, 9 Apr 2025 02:36:05 +0500 Subject: upd: function signatures for RetrieveEmoteWS (attempt 2) --- src/betterttv.rs | 46 +++++++++++++++++++++++----------------------- src/emotes.rs | 15 ++++++++------- 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 for BetterTTVAPIClient { } } -pub struct BetterTTVWSClient -where - F: Fn(String, Option, Emote), -{ +pub struct BetterTTVWSClient { url: String, - on_emote_create: Option, - on_emote_update: Option, - on_emote_delete: Option, + on_emote_create: Option>, + on_emote_update: Option>, + on_emote_delete: Option>, joined_channels: HashSet, awaiting_channels: HashSet, } -impl RetrieveEmoteWS for BetterTTVWSClient -where - F: Fn(String, Option, Emote), -{ - fn on_emote_create(&mut self, func: F) { - self.on_emote_create = Some(func); +impl RetrieveEmoteWS for BetterTTVWSClient { + fn on_emote_create( + &mut self, + func: impl Fn(String, Option, 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, 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, Emote) + Send + Sync + 'static, + ) { + self.on_emote_delete = Some(Arc::new(func)); } } -impl BetterTTVWSClient -where - F: Fn(String, Option, Emote), -{ +impl BetterTTVWSClient { pub async fn new() -> Result<(WebSocketStream>, 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 { async fn get_global_emotes(&self) -> Result, Error>; } -pub trait RetrieveEmoteWS -where - F: Fn(String, Option, 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 = Arc, T) + Send + Sync>; + +pub trait RetrieveEmoteWS { + fn on_emote_create(&mut self, func: impl Fn(String, Option, T) + Send + Sync + 'static); + fn on_emote_update(&mut self, func: impl Fn(String, Option, T) + Send + Sync + 'static); + fn on_emote_delete(&mut self, func: impl Fn(String, Option, 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 for SevenTVAPIClient { } } -pub struct SevenTVWSClient -where - F: Fn(String, Option, Emote), -{ +pub struct SevenTVWSClient { url: String, - on_emote_create: Option, - on_emote_update: Option, - on_emote_delete: Option, + on_emote_create: Option>, + on_emote_update: Option>, + on_emote_delete: Option>, joined_channels: HashSet, awaiting_channels: HashSet, @@ -208,27 +205,30 @@ where identified: bool, } -impl RetrieveEmoteWS for SevenTVWSClient -where - F: Fn(String, Option, Emote), -{ - fn on_emote_create(&mut self, func: F) { - self.on_emote_create = Some(func); +impl RetrieveEmoteWS for SevenTVWSClient { + fn on_emote_create( + &mut self, + func: impl Fn(String, Option, 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, 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, Emote) + Send + Sync + 'static, + ) { + self.on_emote_delete = Some(Arc::new(func)); } } -impl SevenTVWSClient -where - F: Fn(String, Option, Emote), -{ +impl SevenTVWSClient { pub async fn new() -> Result<(WebSocketStream>, Self)> { let url = "wss://events.7tv.io/v3"; -- cgit v1.2.3