From 20d129c8aa956ef4b7d0ba84b1d3267043ace7e2 Mon Sep 17 00:00:00 2001 From: Zach Date: Sun, 5 Jan 2025 11:53:15 -0500 Subject: [PATCH] make channel size a const generic --- src/lib.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2dd42fa..6f497b8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,7 +33,11 @@ struct TxChannelPayload { } #[derive(Debug)] -pub struct Socketeer Deserialize<'a> + Debug, TxMessage: Serialize + Debug> { +pub struct Socketeer< + RxMessage: for<'a> Deserialize<'a> + Debug, + TxMessage: Serialize + Debug, + const CHANNEL_SIZE: usize = 4, +> { url: Url, receiever: mpsc::Receiver, sender: mpsc::Sender, @@ -42,8 +46,11 @@ pub struct Socketeer Deserialize<'a> + Debug, TxMessage: Seri _tx_message: std::marker::PhantomData, } -impl Deserialize<'a> + Debug, TxMessage: Serialize + Debug> - Socketeer +impl< + RxMessage: for<'a> Deserialize<'a> + Debug, + TxMessage: Serialize + Debug, + const CHANNEL_SIZE: usize, + > Socketeer { /// Create a `Socketeer` connected to the provided URL. /// Once connected, Socketeer manages the underlying WebSocket connection, transparently handling protocol messages. @@ -51,7 +58,9 @@ impl Deserialize<'a> + Debug, TxMessage: Serialize + Debug> /// - If the URL cannot be parsed /// - If the WebSocket connection to the requested URL fails #[cfg_attr(feature = "tracing", instrument)] - pub async fn connect(url: &str) -> Result, Error> { + pub async fn connect( + url: &str, + ) -> Result, Error> { let url = Url::parse(url).map_err(|source| Error::UrlParse { url: url.to_string(), source, @@ -60,8 +69,9 @@ impl Deserialize<'a> + Debug, TxMessage: Serialize + Debug> let (socket, response) = connect_async(url.as_str()).await?; #[cfg(feature = "tracing")] info!("Connection Successful, connection info: \n{:#?}", response); - let (tx_tx, tx_rx) = mpsc::channel::(8); - let (rx_tx, rx_rx) = mpsc::channel::(8); + + let (tx_tx, tx_rx) = mpsc::channel::(CHANNEL_SIZE); + let (rx_tx, rx_rx) = mpsc::channel::(CHANNEL_SIZE); let socket_handle = tokio::spawn(async move { socket_loop(tx_rx, rx_tx, socket).await }); Ok(Socketeer {