From b39ab982238a50eecfefbfd25a07ffc1be44a516 Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Fri, 21 Feb 2025 13:08:25 +0000 Subject: [PATCH] Chore: Bump rand->0.9, tokio-tungstenite->0.26 --- Cargo.toml | 4 ++-- Makefile.toml | 4 ++-- build.rs | 12 ++++++++++++ examples/twilight/Cargo.toml | 4 +++- src/driver/crypto.rs | 2 +- src/driver/tasks/ws.rs | 7 ++++--- src/input/sources/ytdl.rs | 2 +- src/ws.rs | 26 +++++++++----------------- 8 files changed, 34 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f547c6f..1cce984 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,7 @@ futures = "0.3" nohash-hasher = { optional = true, version = "0.2.0" } parking_lot = { optional = true, version = "0.12" } pin-project = "1" -rand = { optional = true, version = "0.8" } +rand = { optional = true, version = "0.9" } reqwest = { default-features = false, features = [ "stream", ], optional = true, version = "0.12.2" } @@ -56,7 +56,7 @@ stream_lib = { default-features = false, optional = true, version = "0.5.2" } symphonia = { default-features = false, optional = true, version = "0.5.2" } symphonia-core = { optional = true, version = "0.5.2" } tokio = { default-features = false, optional = true, version = "1.0" } -tokio-tungstenite = { optional = true, version = "0.24", features = ["url"] } +tokio-tungstenite = { optional = true, version = "0.26", features = ["url"] } tokio-websockets = { optional = true, version = "0.11", features = [ "client", "fastrand", diff --git a/Makefile.toml b/Makefile.toml index 631b886..56d6c5a 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -19,12 +19,12 @@ command = "cargo" dependencies = ["format"] [tasks.build-driver] -args = ["build", "--no-default-features", "--features", "driver,rustls"] +args = ["build", "--no-default-features", "--features", "driver,rustls,tungstenite"] command = "cargo" dependencies = ["format"] [tasks.build-variants] -dependencies = ["build", "build-gateway", "build-driver", "build-simd"] +dependencies = ["build", "build-gateway", "build-driver"] [tasks.check] args = ["check", "--features", "full-doc"] diff --git a/build.rs b/build.rs index 1e6e597..771145c 100644 --- a/build.rs +++ b/build.rs @@ -9,4 +9,16 @@ compile_error!( If you are unsure, go with `rustls`." ); +#[cfg(any( + all(feature = "driver", feature = "tws", feature = "tungstenite"), + all(feature = "driver", not(feature = "tws"), not(feature = "tungstenite")) +))] +compile_error!( + "You have the `driver` feature enabled: \ + this requires you specify either: \n\ + - `tungstenite` (recommended with serenity)\n\ + - or `tws` (recommended with twilight).\n\ + You have either specified none, or both - choose exactly one." +); + fn main() {} diff --git a/examples/twilight/Cargo.toml b/examples/twilight/Cargo.toml index 7cc6809..d00ac2b 100644 --- a/examples/twilight/Cargo.toml +++ b/examples/twilight/Cargo.toml @@ -7,7 +7,9 @@ edition = "2021" [dependencies] futures = "0.3" reqwest = { workspace = true } -songbird = { workspace = true, features = ["driver", "gateway", "twilight", "rustls", "tws"] } +# In an actual twilight project, use the "tws" feature as below. +# songbird = { workspace = true, features = ["driver", "gateway", "twilight", "rustls", "tws"] } +songbird = { workspace = true, features = ["driver", "gateway", "twilight", "rustls"] } symphonia = { workspace = true } tracing = { workspace = true } tracing-subscriber = { workspace = true, default-features = true } diff --git a/src/driver/crypto.rs b/src/driver/crypto.rs index c21b7f3..9a54af2 100644 --- a/src/driver/crypto.rs +++ b/src/driver/crypto.rs @@ -423,7 +423,7 @@ impl CryptoState { match self { Self::Suffix => { - rand::thread_rng().fill(&mut packet.payload_mut()[startpoint..endpoint]); + rand::rng().fill(&mut packet.payload_mut()[startpoint..endpoint]); }, Self::Lite(ref mut i) | Self::Aes256Gcm(ref mut i) diff --git a/src/driver/tasks/ws.rs b/src/driver/tasks/ws.rs index 6478a1a..281261d 100644 --- a/src/driver/tasks/ws.rs +++ b/src/driver/tasks/ws.rs @@ -12,7 +12,7 @@ use crate::{ ConnectionInfo, }; use flume::Receiver; -use rand::{distributions::Uniform, Rng}; +use rand::{distr::Uniform, Rng}; #[cfg(feature = "receive")] use std::sync::Arc; use std::time::Duration; @@ -181,8 +181,9 @@ impl AuxNetwork { // Discord have suddenly, mysteriously, started rejecting // ints-as-strings. Keep JS happy here, I suppose... const JS_MAX_INT: u64 = (1u64 << 53) - 1; - let nonce_range = Uniform::from(0..JS_MAX_INT); - let nonce = rand::thread_rng().sample(nonce_range); + let nonce_range = + Uniform::new(0, JS_MAX_INT).expect("uniform range is finite and nonempty"); + let nonce = rand::rng().sample(nonce_range); self.last_heartbeat_nonce = Some(nonce); trace!("Sent heartbeat {:?}", self.speaking); diff --git a/src/input/sources/ytdl.rs b/src/input/sources/ytdl.rs index f81f8fc..4183f06 100644 --- a/src/input/sources/ytdl.rs +++ b/src/input/sources/ytdl.rs @@ -140,7 +140,7 @@ impl<'a> YoutubeDl<'a> { "--no-playlist", ]; - let mut output = Command::new(self.program) + let output = Command::new(self.program) .args(self.user_args.clone()) .args(ytdl_args) .output() diff --git a/src/ws.rs b/src/ws.rs index e64e559..9c98c10 100644 --- a/src/ws.rs +++ b/src/ws.rs @@ -1,5 +1,6 @@ use crate::{error::JsonError, model::Event}; +use bytes::Bytes; use futures::{SinkExt, StreamExt, TryStreamExt}; use tokio::{ net::TcpStream, @@ -27,12 +28,6 @@ use tokio_websockets::{ use tracing::{debug, instrument}; use url::Url; -#[cfg(any( - all(feature = "tws", feature = "tungstenite"), - all(not(feature = "tws"), not(feature = "tungstenite")) -))] -compile_error!("specify one of `features = [\"tungstenite\"]` (recommended w/ serenity) or `features = [\"tws\"]` (recommended w/ twilight)"); - pub struct WsStream(WebSocketStream>); impl WsStream { @@ -41,11 +36,11 @@ impl WsStream { #[cfg(feature = "tungstenite")] let (stream, _) = tokio_tungstenite::connect_async_with_config::( url, - Some(Config { - max_message_size: None, - max_frame_size: None, - ..Default::default() - }), + Some( + Config::default() + .max_message_size(None) + .max_frame_size(None), + ), true, ) .await?; @@ -78,9 +73,6 @@ impl WsStream { pub(crate) async fn send_json(&mut self, value: &Event) -> Result<()> { let res = crate::json::to_string(value); - #[cfg(feature = "tungstenite")] - let res = res.map(Message::Text); - #[cfg(feature = "tws")] let res = res.map(Message::text); Ok(res.map_err(Error::from).map(|m| self.0.send(m))?.await?) } @@ -94,7 +86,7 @@ pub enum Error { /// The discord voice gateway does not support or offer zlib compression. /// As a result, only text messages are expected. - UnexpectedBinaryMessage(Vec), + UnexpectedBinaryMessage(Bytes), #[cfg(feature = "tungstenite")] Ws(TungsteniteError), @@ -102,7 +94,7 @@ pub enum Error { Ws(TwsError), #[cfg(feature = "tungstenite")] - WsClosed(Option>), + WsClosed(Option), #[cfg(feature = "tws")] WsClosed(Option), } @@ -151,7 +143,7 @@ pub(crate) fn convert_ws_message(message: Option) -> Result { return Err(Error::UnexpectedBinaryMessage( - message.into_payload().to_vec(), + message.into_payload().into(), )); }, Some(message) if message.is_close() => {