From ae69e5efff072778facdaabc989084b342ff800c Mon Sep 17 00:00:00 2001 From: "Alex M. M" Date: Sun, 15 Nov 2020 19:49:53 +0100 Subject: [PATCH 01/20] Deps: Depend on Serenity's `next` branch on Songbird's `next` branch (#6) * Depend on Serenity's `next` branch on Songbird's `next` branch * Update the examples to use the `next` branch --- Cargo.toml | 6 ++++-- examples/serenity/voice/Cargo.toml | 3 ++- examples/serenity/voice_events_queue/Cargo.toml | 3 ++- examples/serenity/voice_receive/Cargo.toml | 3 ++- examples/serenity/voice_storage/Cargo.toml | 3 ++- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6b0a842e3..ec2900bc3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,13 +74,15 @@ version = "0.8" [dependencies.serenity] optional = true -version = "^0.10.2" default-features = false features = ["voice", "gateway"] +git = "https://github.com/serenity-rs/serenity" +branch = "next" [dependencies.serenity-voice-model] optional = true -version = "0.1" +git = "https://github.com/serenity-rs/serenity" +branch = "next" [dependencies.spin_sleep] optional = true diff --git a/examples/serenity/voice/Cargo.toml b/examples/serenity/voice/Cargo.toml index f4c8b46e3..82879229c 100644 --- a/examples/serenity/voice/Cargo.toml +++ b/examples/serenity/voice/Cargo.toml @@ -13,8 +13,9 @@ tracing-futures = "0.2" path = "../../../" [dependencies.serenity] -version = "0.10" features = ["client", "standard_framework", "voice", "rustls_backend"] +git = "https://github.com/serenity-rs/serenity" +branch = "next" [dependencies.tokio] version = "1.0" diff --git a/examples/serenity/voice_events_queue/Cargo.toml b/examples/serenity/voice_events_queue/Cargo.toml index 53feda06e..b21910c5f 100644 --- a/examples/serenity/voice_events_queue/Cargo.toml +++ b/examples/serenity/voice_events_queue/Cargo.toml @@ -14,8 +14,9 @@ features = ["builtin-queue"] path = "../../../" [dependencies.serenity] -version = "0.10" features = ["cache", "standard_framework", "voice", "rustls_backend"] +git = "https://github.com/serenity-rs/serenity" +branch = "next" [dependencies.tokio] version = "1.0" diff --git a/examples/serenity/voice_receive/Cargo.toml b/examples/serenity/voice_receive/Cargo.toml index 9f48616c8..3670d5798 100644 --- a/examples/serenity/voice_receive/Cargo.toml +++ b/examples/serenity/voice_receive/Cargo.toml @@ -13,8 +13,9 @@ tracing-futures = "0.2" path = "../../../" [dependencies.serenity] -version = "0.10" features = ["client", "standard_framework", "voice", "rustls_backend"] +git = "https://github.com/serenity-rs/serenity" +branch = "next" [dependencies.tokio] version = "1.0" diff --git a/examples/serenity/voice_storage/Cargo.toml b/examples/serenity/voice_storage/Cargo.toml index ce34dee1f..3e4731990 100644 --- a/examples/serenity/voice_storage/Cargo.toml +++ b/examples/serenity/voice_storage/Cargo.toml @@ -13,8 +13,9 @@ tracing-futures = "0.2" path = "../../../" [dependencies.serenity] -version = "0.10" features = ["cache", "framework", "standard_framework", "voice", "http", "rustls_backend"] +git = "https://github.com/serenity-rs/serenity" +branch = "next" [dependencies.tokio] version = "1.0" From 3784490be5cdcf8e6a842950f9f712acf30ec3a8 Mon Sep 17 00:00:00 2001 From: tkt <37575408+tktcorporation@users.noreply.github.com> Date: Wed, 22 Sep 2021 17:45:54 +0900 Subject: [PATCH 02/20] Deps: Bump streamcatcher version -> 1.0 (#93) --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index ec2900bc3..21c8d6af5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -90,7 +90,7 @@ version = "1" [dependencies.streamcatcher] optional = true -version = "0.1" +version = "1" [dependencies.tokio] optional = true From 94bd290fca32430d43b191c2d7fd15974ba6072b Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Sun, 10 Oct 2021 20:52:26 +0100 Subject: [PATCH 03/20] Examples: Fix serenity-next cache accesses (#99) Serenity's cache design has changed, this (finally) prevents the examples from failing to compile on CI. --- examples/serenity/voice/src/main.rs | 14 ++++++------- .../serenity/voice_events_queue/src/main.rs | 20 +++++++++---------- examples/serenity/voice_receive/src/main.rs | 4 ++-- examples/serenity/voice_storage/src/main.rs | 14 ++++++------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/examples/serenity/voice/src/main.rs b/examples/serenity/voice/src/main.rs index e6f0ba780..e4c69971c 100644 --- a/examples/serenity/voice/src/main.rs +++ b/examples/serenity/voice/src/main.rs @@ -74,7 +74,7 @@ async fn main() { #[command] #[only_in(guilds)] async fn deafen(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx).await @@ -107,7 +107,7 @@ async fn deafen(ctx: &Context, msg: &Message) -> CommandResult { #[command] #[only_in(guilds)] async fn join(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let channel_id = guild @@ -134,7 +134,7 @@ async fn join(ctx: &Context, msg: &Message) -> CommandResult { #[command] #[only_in(guilds)] async fn leave(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx).await @@ -157,7 +157,7 @@ async fn leave(ctx: &Context, msg: &Message) -> CommandResult { #[command] #[only_in(guilds)] async fn mute(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx).await @@ -212,7 +212,7 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { return Ok(()); } - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx).await @@ -245,7 +245,7 @@ async fn play(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { #[command] #[only_in(guilds)] async fn undeafen(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx).await @@ -268,7 +268,7 @@ async fn undeafen(ctx: &Context, msg: &Message) -> CommandResult { #[command] #[only_in(guilds)] async fn unmute(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx).await diff --git a/examples/serenity/voice_events_queue/src/main.rs b/examples/serenity/voice_events_queue/src/main.rs index 28cc771b2..c9845b201 100644 --- a/examples/serenity/voice_events_queue/src/main.rs +++ b/examples/serenity/voice_events_queue/src/main.rs @@ -87,7 +87,7 @@ async fn main() { #[command] async fn deafen(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx) @@ -126,7 +126,7 @@ async fn deafen(ctx: &Context, msg: &Message) -> CommandResult { #[command] #[only_in(guilds)] async fn join(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let channel_id = guild @@ -241,7 +241,7 @@ impl VoiceEventHandler for ChannelDurationNotifier { #[command] #[only_in(guilds)] async fn leave(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx) @@ -270,7 +270,7 @@ async fn leave(ctx: &Context, msg: &Message) -> CommandResult { #[command] #[only_in(guilds)] async fn mute(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx) @@ -339,7 +339,7 @@ async fn play_fade(ctx: &Context, msg: &Message, mut args: Args) -> CommandResul return Ok(()); } - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx) @@ -470,7 +470,7 @@ async fn queue(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { return Ok(()); } - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx) @@ -518,7 +518,7 @@ async fn queue(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { #[command] #[only_in(guilds)] async fn skip(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx) @@ -553,7 +553,7 @@ async fn skip(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { #[command] #[only_in(guilds)] async fn stop(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx) @@ -581,7 +581,7 @@ async fn stop(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { #[command] #[only_in(guilds)] async fn undeafen(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx) @@ -614,7 +614,7 @@ async fn undeafen(ctx: &Context, msg: &Message) -> CommandResult { #[command] #[only_in(guilds)] async fn unmute(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx) .await diff --git a/examples/serenity/voice_receive/src/main.rs b/examples/serenity/voice_receive/src/main.rs index 82cf170e0..d6e19777a 100644 --- a/examples/serenity/voice_receive/src/main.rs +++ b/examples/serenity/voice_receive/src/main.rs @@ -192,7 +192,7 @@ async fn join(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { }, }; - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx).await @@ -245,7 +245,7 @@ async fn join(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult { #[command] #[only_in(guilds)] async fn leave(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx).await diff --git a/examples/serenity/voice_storage/src/main.rs b/examples/serenity/voice_storage/src/main.rs index cbebb4929..8c56ea067 100644 --- a/examples/serenity/voice_storage/src/main.rs +++ b/examples/serenity/voice_storage/src/main.rs @@ -151,7 +151,7 @@ async fn main() { #[command] #[only_in(guilds)] async fn deafen(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx).await @@ -184,7 +184,7 @@ async fn deafen(ctx: &Context, msg: &Message) -> CommandResult { #[command] #[only_in(guilds)] async fn join(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let channel_id = guild @@ -262,7 +262,7 @@ impl VoiceEventHandler for LoopPlaySound { #[command] #[only_in(guilds)] async fn leave(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx).await @@ -285,7 +285,7 @@ async fn leave(ctx: &Context, msg: &Message) -> CommandResult { #[command] #[only_in(guilds)] async fn mute(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx).await @@ -318,7 +318,7 @@ async fn mute(ctx: &Context, msg: &Message) -> CommandResult { #[command] #[only_in(guilds)] async fn ting(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx).await @@ -344,7 +344,7 @@ async fn ting(ctx: &Context, msg: &Message, _args: Args) -> CommandResult { #[command] #[only_in(guilds)] async fn undeafen(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx).await @@ -368,7 +368,7 @@ async fn undeafen(ctx: &Context, msg: &Message) -> CommandResult { #[command] #[only_in(guilds)] async fn unmute(ctx: &Context, msg: &Message) -> CommandResult { - let guild = msg.guild(&ctx.cache).await.unwrap(); + let guild = msg.guild(&ctx.cache).unwrap(); let guild_id = guild.id; let manager = songbird::get(ctx).await .expect("Songbird Voice client placed in at initialisation.").clone(); From 21294225882c23dbd30cc2ff9402f13bfbc2d2c1 Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Tue, 19 Oct 2021 17:28:46 +0100 Subject: [PATCH 04/20] Gateway: Add generics to `Call` methods. (#102) Adds generics to any `Id` types on `Call`. Also includes the overlooked `Songbird::get_or_insert`. Closes #94. Tested using `cargo make ready`. --- src/handler.rs | 69 ++++++++++++++++++++++++++++++++++++++++++-------- src/manager.rs | 17 ++++++++----- 2 files changed, 69 insertions(+), 17 deletions(-) diff --git a/src/handler.rs b/src/handler.rs index 74c3886e3..060b88f06 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -10,6 +10,7 @@ use crate::{ }; use flume::Sender; use serde_json::json; +use std::fmt::Debug; use tracing::instrument; #[cfg(feature = "driver-core")] @@ -68,15 +69,28 @@ impl Call { /// the given shard. #[inline] #[instrument] - pub fn new(guild_id: GuildId, ws: Shard, user_id: UserId) -> Self { - Self::new_raw_cfg(guild_id, Some(ws), user_id, Default::default()) + pub fn new(guild_id: G, ws: Shard, user_id: U) -> Self + where + G: Into + Debug, + U: Into + Debug, + { + Self::new_raw_cfg( + guild_id.into(), + Some(ws), + user_id.into(), + Default::default(), + ) } /// Creates a new Call, configuring the driver as specified. #[inline] #[instrument] - pub fn from_config(guild_id: GuildId, ws: Shard, user_id: UserId, config: Config) -> Self { - Self::new_raw_cfg(guild_id, Some(ws), user_id, config) + pub fn from_config(guild_id: G, ws: Shard, user_id: U, config: Config) -> Self + where + G: Into + Debug, + U: Into + Debug, + { + Self::new_raw_cfg(guild_id.into(), Some(ws), user_id.into(), config) } /// Creates a new, standalone Call which is not connected via @@ -89,15 +103,23 @@ impl Call { /// For most use cases you do not want this. #[inline] #[instrument] - pub fn standalone(guild_id: GuildId, user_id: UserId) -> Self { - Self::new_raw_cfg(guild_id, None, user_id, Default::default()) + pub fn standalone(guild_id: G, user_id: U) -> Self + where + G: Into + Debug, + U: Into + Debug, + { + Self::new_raw_cfg(guild_id.into(), None, user_id.into(), Default::default()) } /// Creates a new standalone Call from the given configuration file. #[inline] #[instrument] - pub fn standalone_from_config(guild_id: GuildId, user_id: UserId, config: Config) -> Self { - Self::new_raw_cfg(guild_id, None, user_id, config) + pub fn standalone_from_config(guild_id: G, user_id: U, config: Config) -> Self + where + G: Into + Debug, + U: Into + Debug, + { + Self::new_raw_cfg(guild_id.into(), None, user_id.into(), config) } fn new_raw_cfg(guild_id: GuildId, ws: Option, user_id: UserId, config: Config) -> Self { @@ -198,7 +220,16 @@ impl Call { /// /// [`Songbird::join`]: crate::Songbird::join #[instrument(skip(self))] - pub async fn join(&mut self, channel_id: ChannelId) -> JoinResult { + #[inline] + pub async fn join(&mut self, channel_id: C) -> JoinResult + where + C: Into + Debug, + { + self._join(channel_id.into()).await + } + + #[cfg(feature = "driver-core")] + async fn _join(&mut self, channel_id: ChannelId) -> JoinResult { let (tx, rx) = flume::unbounded(); let (gw_tx, gw_rx) = flume::unbounded(); @@ -250,7 +281,15 @@ impl Call { /// /// [`Songbird::join_gateway`]: crate::Songbird::join_gateway #[instrument(skip(self))] - pub async fn join_gateway(&mut self, channel_id: ChannelId) -> JoinResult { + #[inline] + pub async fn join_gateway(&mut self, channel_id: C) -> JoinResult + where + C: Into + Debug, + { + self._join_gateway(channel_id.into()).await + } + + async fn _join_gateway(&mut self, channel_id: ChannelId) -> JoinResult { let (tx, rx) = flume::unbounded(); let do_conn = self @@ -376,7 +415,15 @@ impl Call { /// /// [`standalone`]: Call::standalone #[instrument(skip(self))] - pub fn update_state(&mut self, session_id: String, channel_id: Option) { + #[inline] + pub fn update_state(&mut self, session_id: String, channel_id: Option) + where + C: Into + Debug, + { + self._update_state(session_id, channel_id.map(|c| c.into())) + } + + fn _update_state(&mut self, session_id: String, channel_id: Option) { if let Some(channel_id) = channel_id { let try_conn = if let Some((ref mut progress, _)) = self.connection.as_mut() { progress.apply_state_update(session_id, channel_id) diff --git a/src/manager.rs b/src/manager.rs index c133716e5..566102067 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -155,7 +155,15 @@ impl Songbird { /// This will not join any calls, or cause connection state to change. /// /// [`Call`]: Call - pub fn get_or_insert(&self, guild_id: GuildId) -> Arc> { + #[inline] + pub fn get_or_insert(&self, guild_id: G) -> Arc> + where + G: Into, + { + self._get_or_insert(guild_id.into()) + } + + fn _get_or_insert(&self, guild_id: GuildId) -> Arc> { self.get(guild_id).unwrap_or_else(|| { self.calls .entry(guild_id) @@ -378,7 +386,7 @@ impl Songbird { if let Some(call) = call { let mut handler = call.lock().await; - handler.update_state(v.0.session_id.clone(), v.0.channel_id.map(Into::into)); + handler.update_state(v.0.session_id.clone(), v.0.channel_id); } }, _ => {}, @@ -432,10 +440,7 @@ impl VoiceGatewayManager for Songbird { if let Some(call) = self.get(guild_id) { let mut handler = call.lock().await; - handler.update_state( - voice_state.session_id.clone(), - voice_state.channel_id.map(Into::into), - ); + handler.update_state(voice_state.session_id.clone(), voice_state.channel_id); } } } From 7956792bc986b0589b8e7c1a03190f6249e195d9 Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Fri, 5 Nov 2021 22:51:55 +0000 Subject: [PATCH 05/20] Gateway: Remove lifetime from Serenity setup trait (#103) This matches a recent serenity change where `ClientBuilder` no longer has an explicit lifetime parameter. This was tested using `cargo make ready`. --- src/serenity.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/serenity.rs b/src/serenity.rs index 014ff749d..92d66e1f4 100644 --- a/src/serenity.rs +++ b/src/serenity.rs @@ -70,7 +70,7 @@ pub trait SerenityInit { fn register_songbird_from_config(self, config: Config) -> Self; } -impl SerenityInit for ClientBuilder<'_> { +impl SerenityInit for ClientBuilder { fn register_songbird(self) -> Self { register(self) } From 89de7a1f2473ac2a86139160d71f12a59bf36fb6 Mon Sep 17 00:00:00 2001 From: vicky5124 Date: Mon, 6 Dec 2021 23:55:48 +0100 Subject: [PATCH 06/20] Support serenity simd_json modified: src/shards.rs --- src/shards.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/shards.rs b/src/shards.rs index 03fa35268..d83d7bf2f 100644 --- a/src/shards.rs +++ b/src/shards.rs @@ -5,6 +5,9 @@ use crate::error::{JoinError, JoinResult}; use futures::channel::mpsc::{TrySendError, UnboundedSender as Sender}; #[cfg(feature = "serenity")] use parking_lot::{lock_api::RwLockWriteGuard, Mutex as PMutex, RwLock as PRwLock}; +#[cfg(feature = "serenity")] +use serenity::json::Value; +#[cfg(not(feature = "serenity"))] use serde_json::Value; #[cfg(feature = "serenity")] use serenity::gateway::InterMessage; From c16c17cfb510e886fa7d34b166f958a31a6be17e Mon Sep 17 00:00:00 2001 From: vicky5124 Date: Tue, 7 Dec 2021 00:31:27 +0100 Subject: [PATCH 07/20] Missed a place modified: src/handler.rs --- src/handler.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/handler.rs b/src/handler.rs index 060b88f06..1f8716b18 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -9,6 +9,9 @@ use crate::{ Config, }; use flume::Sender; +#[cfg(feature = "serenity")] +use serenity::json::json; +#[cfg(not(feature = "serenity"))] use serde_json::json; use std::fmt::Debug; use tracing::instrument; From 45fb51baa70eda3e457f20596fd2cee97c2a9941 Mon Sep 17 00:00:00 2001 From: vicky5124 Date: Tue, 7 Dec 2021 00:33:43 +0100 Subject: [PATCH 08/20] fmt modified: src/handler.rs modified: src/shards.rs --- src/handler.rs | 4 ++-- src/shards.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/handler.rs b/src/handler.rs index 1f8716b18..feaa8a301 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -9,10 +9,10 @@ use crate::{ Config, }; use flume::Sender; -#[cfg(feature = "serenity")] -use serenity::json::json; #[cfg(not(feature = "serenity"))] use serde_json::json; +#[cfg(feature = "serenity")] +use serenity::json::json; use std::fmt::Debug; use tracing::instrument; diff --git a/src/shards.rs b/src/shards.rs index d83d7bf2f..43e08ee2f 100644 --- a/src/shards.rs +++ b/src/shards.rs @@ -5,13 +5,13 @@ use crate::error::{JoinError, JoinResult}; use futures::channel::mpsc::{TrySendError, UnboundedSender as Sender}; #[cfg(feature = "serenity")] use parking_lot::{lock_api::RwLockWriteGuard, Mutex as PMutex, RwLock as PRwLock}; -#[cfg(feature = "serenity")] -use serenity::json::Value; #[cfg(not(feature = "serenity"))] use serde_json::Value; #[cfg(feature = "serenity")] use serenity::gateway::InterMessage; #[cfg(feature = "serenity")] +use serenity::json::Value; +#[cfg(feature = "serenity")] use std::{collections::HashMap, result::Result as StdResult, sync::Arc}; use tracing::{debug, error}; #[cfg(feature = "twilight")] From ce95d4bd97df52575a2e04da49313f548be97622 Mon Sep 17 00:00:00 2001 From: vicky5124 Date: Tue, 7 Dec 2021 18:25:21 +0100 Subject: [PATCH 09/20] Fully replace everything to serenity::json modified: ../../../Cargo.toml modified: ../../../src/driver/mod.rs modified: ../../../src/input/dca.rs modified: ../../../src/input/error.rs modified: ../../../src/input/ffmpeg_src.rs modified: ../../../src/input/metadata.rs modified: ../../../src/input/ytdl_src.rs modified: ../../../src/ws.rs --- examples/serenity/voice_receive/Cargo.toml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/serenity/voice_receive/Cargo.toml b/examples/serenity/voice_receive/Cargo.toml index 3670d5798..bac3c85eb 100644 --- a/examples/serenity/voice_receive/Cargo.toml +++ b/examples/serenity/voice_receive/Cargo.toml @@ -13,9 +13,10 @@ tracing-futures = "0.2" path = "../../../" [dependencies.serenity] -features = ["client", "standard_framework", "voice", "rustls_backend"] -git = "https://github.com/serenity-rs/serenity" -branch = "next" +features = ["client", "standard_framework", "voice", "rustls_backend", "simdjson"] +#git = "https://github.com/serenity-rs/serenity" +#branch = "next" +path = "../../../../serenity" [dependencies.tokio] version = "1.0" From 3c83316752660a7eae40012d8fc991f4d3a53b0f Mon Sep 17 00:00:00 2001 From: vicky5124 Date: Tue, 7 Dec 2021 18:26:17 +0100 Subject: [PATCH 10/20] Fully replace serde_json with serenity::json modified: Cargo.toml modified: src/driver/mod.rs modified: src/input/dca.rs modified: src/input/error.rs modified: src/input/ffmpeg_src.rs modified: src/input/metadata.rs modified: src/input/ytdl_src.rs modified: src/ws.rs --- Cargo.toml | 10 ++++---- src/driver/mod.rs | 4 +++- src/input/dca.rs | 8 +++++++ src/input/error.rs | 3 +++ src/input/ffmpeg_src.rs | 12 ++++++++++ src/input/metadata.rs | 6 +++++ src/input/ytdl_src.rs | 31 ++++++++++++++++++++---- src/ws.rs | 52 +++++++++++++++++++++++++++++++++-------- 8 files changed, 107 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 21c8d6af5..2d087968f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -76,13 +76,15 @@ version = "0.8" optional = true default-features = false features = ["voice", "gateway"] -git = "https://github.com/serenity-rs/serenity" -branch = "next" +#git = "https://github.com/serenity-rs/serenity" +#branch = "next" +path = "../serenity" [dependencies.serenity-voice-model] optional = true -git = "https://github.com/serenity-rs/serenity" -branch = "next" +#git = "https://github.com/serenity-rs/serenity" +#branch = "next" +path = "../serenity/voice-model" [dependencies.spin_sleep] optional = true diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 3d78eab80..2ccbd9ddd 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -24,10 +24,12 @@ pub use decode_mode::DecodeMode; #[cfg(feature = "builtin-queue")] use crate::tracks::TrackQueue; +#[cfg(feature = "builtin-queue")] +use crate::tracks; use crate::{ events::EventData, input::Input, - tracks::{self, Track, TrackHandle}, + tracks::{Track, TrackHandle}, Config, ConnectionInfo, Event, diff --git a/src/input/dca.rs b/src/input/dca.rs index cdda7817e..27daf7ce2 100644 --- a/src/input/dca.rs +++ b/src/input/dca.rs @@ -48,6 +48,11 @@ async fn _dca(path: &OsStr) -> Result { let reader = json_reader.into_inner().into_std().await; + #[cfg(feature = "serenity")] + let metadata: Metadata = serenity::json::prelude::from_slice::(raw_json.as_mut_slice()) + .map_err(DcaError::InvalidMetadata)? + .into(); + #[cfg(not(feature = "serenity"))] let metadata: Metadata = serde_json::from_slice::(raw_json.as_slice()) .map_err(DcaError::InvalidMetadata)? .into(); @@ -71,7 +76,10 @@ pub(crate) struct DcaMetadata { pub(crate) opus: Opus, pub(crate) info: Option, pub(crate) origin: Option, + #[cfg(not(feature = "serenity"))] pub(crate) extra: Option, + #[cfg(feature = "serenity")] + pub(crate) extra: Option, } #[derive(Debug, Deserialize)] diff --git a/src/input/error.rs b/src/input/error.rs index 551ea3695..58ba30021 100644 --- a/src/input/error.rs +++ b/src/input/error.rs @@ -2,7 +2,10 @@ use audiopus::Error as OpusError; use core::fmt; +#[cfg(not(feature = "serenity"))] use serde_json::{Error as JsonError, Value}; +#[cfg(feature = "serenity")] +use serenity::json::prelude::{JsonError, Value}; use std::{error::Error as StdError, io::Error as IoError, process::Output}; use streamcatcher::CatcherError; diff --git a/src/input/ffmpeg_src.rs b/src/input/ffmpeg_src.rs index 1ea14f16f..4956b12ba 100644 --- a/src/input/ffmpeg_src.rs +++ b/src/input/ffmpeg_src.rs @@ -6,7 +6,10 @@ use super::{ Input, Metadata, }; +#[cfg(not(feature = "serenity"))] use serde_json::Value; +#[cfg(feature = "serenity")] +use serenity::json::Value; use std::{ ffi::OsStr, process::{Command, Stdio}, @@ -143,6 +146,15 @@ pub(crate) async fn is_stereo(path: &OsStr) -> Result<(bool, Metadata)> { .output() .await?; + #[cfg(feature = "serenity")] + let value: Value = serenity::json::prelude::from_reader(&out.stdout[..]).map_err(|err| Error::Json { + error: err, + parsed_text: std::str::from_utf8(&out.stdout[..]) + .unwrap_or_default() + .to_string(), + })?; + + #[cfg(not(feature = "serenity"))] let value: Value = serde_json::from_reader(&out.stdout[..]).map_err(|err| Error::Json { error: err, parsed_text: std::str::from_utf8(&out.stdout[..]) diff --git a/src/input/metadata.rs b/src/input/metadata.rs index f05f81c1c..4bba6242c 100644 --- a/src/input/metadata.rs +++ b/src/input/metadata.rs @@ -1,5 +1,11 @@ use crate::constants::*; +#[cfg(not(feature = "serenity"))] use serde_json::Value; +#[cfg(feature = "serenity")] +use serenity::json::Value; +#[cfg(feature = "serenity")] +#[allow(unused_imports)] +use serenity::json::prelude::ValueAccess; use std::time::Duration; /// Information about an [`Input`] source. diff --git a/src/input/ytdl_src.rs b/src/input/ytdl_src.rs index ba03dc888..55222d2d0 100644 --- a/src/input/ytdl_src.rs +++ b/src/input/ytdl_src.rs @@ -6,7 +6,10 @@ use super::{ Input, Metadata, }; +#[cfg(not(feature = "serenity"))] use serde_json::Value; +#[cfg(feature = "serenity")] +use serenity::json::Value; use std::{ io::{BufRead, BufReader, Read}, process::{Command, Stdio}, @@ -81,10 +84,21 @@ pub(crate) async fn _ytdl(uri: &str, pre_args: &[&str]) -> Result { let mut serde_read = BufReader::new(s.by_ref()); // Newline... if let Ok(len) = serde_read.read_until(0xA, &mut o_vec) { - serde_json::from_slice(&o_vec[..len]).map_err(|err| Error::Json { - error: err, - parsed_text: std::str::from_utf8(&o_vec).unwrap_or_default().to_string(), - }) + #[cfg(not(feature = "serenity"))] + { + serde_json::from_slice(&o_vec[..len]).map_err(|err| Error::Json { + error: err, + parsed_text: std::str::from_utf8(&o_vec).unwrap_or_default().to_string(), + }) + } + + #[cfg(feature = "serenity")] + { + serenity::json::prelude::from_slice(&mut o_vec[..len]).map_err(|err| Error::Json { + error: err, + parsed_text: std::str::from_utf8(&o_vec).unwrap_or_default().to_string(), + }) + } } else { Result::Err(Error::Metadata) } @@ -145,6 +159,9 @@ pub(crate) async fn _ytdl_metadata(uri: &str) -> Result { .output() .await?; + #[cfg(feature = "serenity")] + let mut o_vec = youtube_dl_output.stderr; + #[cfg(not(feature = "serenity"))] let o_vec = youtube_dl_output.stderr; let end = (&o_vec) @@ -152,6 +169,12 @@ pub(crate) async fn _ytdl_metadata(uri: &str) -> Result { .position(|el| *el == 0xA) .unwrap_or_else(|| o_vec.len()); + #[cfg(feature = "serenity")] + let value = serenity::json::prelude::from_slice(&mut o_vec[..end]).map_err(|err| Error::Json { + error: err, + parsed_text: std::str::from_utf8(&o_vec).unwrap_or_default().to_string(), + })?; + #[cfg(not(feature = "serenity"))] let value = serde_json::from_slice(&o_vec[..end]).map_err(|err| Error::Json { error: err, parsed_text: std::str::from_utf8(&o_vec).unwrap_or_default().to_string(), diff --git a/src/ws.rs b/src/ws.rs index 80d4379d0..f44cf41e1 100644 --- a/src/ws.rs +++ b/src/ws.rs @@ -16,7 +16,13 @@ use async_tungstenite_compat::{ WebSocketStream, }; use futures::{SinkExt, StreamExt, TryStreamExt}; +#[cfg(not(feature = "serenity"))] use serde_json::Error as JsonError; +#[cfg(feature = "serenity")] +use serenity::json::JsonError; +#[cfg(feature = "serenity")] +#[allow(unused_imports)] +use serenity::json::prelude::ValueAccess; #[cfg(not(feature = "tokio-02-marker"))] use tokio::time::{timeout, Duration}; #[cfg(feature = "tokio-02-marker")] @@ -103,28 +109,54 @@ impl ReceiverExt for WsStream { #[async_trait] impl SenderExt for SplitSink { async fn send_json(&mut self, value: &Event) -> Result<()> { - Ok(serde_json::to_string(value) - .map(Message::Text) - .map_err(Error::from) - .map(|m| self.send(m))? - .await?) + #[cfg(feature = "serenity")] + { + Ok(serenity::json::prelude::to_string(value) + .map(Message::Text) + .map_err(Error::from) + .map(|m| self.send(m))? + .await?) + } + + #[cfg(not(feature = "serenity"))] + { + Ok(serde_json::to_string(value) + .map(Message::Text) + .map_err(Error::from) + .map(|m| self.send(m))? + .await?) + } } } #[async_trait] impl SenderExt for WsStream { async fn send_json(&mut self, value: &Event) -> Result<()> { - Ok(serde_json::to_string(value) - .map(Message::Text) - .map_err(Error::from) - .map(|m| self.send(m))? - .await?) + #[cfg(feature = "serenity")] + { + Ok(serenity::json::prelude::to_string(value) + .map(Message::Text) + .map_err(Error::from) + .map(|m| self.send(m))? + .await?) + } + #[cfg(not(feature = "serenity"))] + { + Ok(serde_json::to_string(value) + .map(Message::Text) + .map_err(Error::from) + .map(|m| self.send(m))? + .await?) + } } } #[inline] pub(crate) fn convert_ws_message(message: Option) -> Result> { Ok(match message { + #[cfg(feature = "serenity")] + Some(Message::Text(mut payload)) => serenity::json::prelude::from_str(&mut payload).map(Some)?, + #[cfg(not(feature = "serenity"))] Some(Message::Text(payload)) => serde_json::from_str(&payload).map(Some)?, Some(Message::Binary(bytes)) => { return Err(Error::UnexpectedBinaryMessage(bytes)); From ec7d02929cbed5af19f39c526e9b0092c21d8d6b Mon Sep 17 00:00:00 2001 From: vicky5124 Date: Tue, 7 Dec 2021 18:27:27 +0100 Subject: [PATCH 11/20] fmt modified: src/driver/mod.rs modified: src/input/dca.rs modified: src/input/ffmpeg_src.rs modified: src/input/metadata.rs modified: src/input/ytdl_src.rs modified: src/tracks/error.rs modified: src/ws.rs --- src/driver/mod.rs | 4 ++-- src/input/dca.rs | 7 ++++--- src/input/ffmpeg_src.rs | 13 +++++++------ src/input/metadata.rs | 4 ++-- src/input/ytdl_src.rs | 19 ++++++++++++------- src/tracks/error.rs | 5 +++-- src/ws.rs | 7 ++++--- 7 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 2ccbd9ddd..967538e9c 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -22,10 +22,10 @@ pub use crypto::CryptoMode; pub(crate) use crypto::CryptoState; pub use decode_mode::DecodeMode; -#[cfg(feature = "builtin-queue")] -use crate::tracks::TrackQueue; #[cfg(feature = "builtin-queue")] use crate::tracks; +#[cfg(feature = "builtin-queue")] +use crate::tracks::TrackQueue; use crate::{ events::EventData, input::Input, diff --git a/src/input/dca.rs b/src/input/dca.rs index 27daf7ce2..2277798de 100644 --- a/src/input/dca.rs +++ b/src/input/dca.rs @@ -49,9 +49,10 @@ async fn _dca(path: &OsStr) -> Result { let reader = json_reader.into_inner().into_std().await; #[cfg(feature = "serenity")] - let metadata: Metadata = serenity::json::prelude::from_slice::(raw_json.as_mut_slice()) - .map_err(DcaError::InvalidMetadata)? - .into(); + let metadata: Metadata = + serenity::json::prelude::from_slice::(raw_json.as_mut_slice()) + .map_err(DcaError::InvalidMetadata)? + .into(); #[cfg(not(feature = "serenity"))] let metadata: Metadata = serde_json::from_slice::(raw_json.as_slice()) .map_err(DcaError::InvalidMetadata)? diff --git a/src/input/ffmpeg_src.rs b/src/input/ffmpeg_src.rs index 4956b12ba..c101d805a 100644 --- a/src/input/ffmpeg_src.rs +++ b/src/input/ffmpeg_src.rs @@ -147,12 +147,13 @@ pub(crate) async fn is_stereo(path: &OsStr) -> Result<(bool, Metadata)> { .await?; #[cfg(feature = "serenity")] - let value: Value = serenity::json::prelude::from_reader(&out.stdout[..]).map_err(|err| Error::Json { - error: err, - parsed_text: std::str::from_utf8(&out.stdout[..]) - .unwrap_or_default() - .to_string(), - })?; + let value: Value = + serenity::json::prelude::from_reader(&out.stdout[..]).map_err(|err| Error::Json { + error: err, + parsed_text: std::str::from_utf8(&out.stdout[..]) + .unwrap_or_default() + .to_string(), + })?; #[cfg(not(feature = "serenity"))] let value: Value = serde_json::from_reader(&out.stdout[..]).map_err(|err| Error::Json { diff --git a/src/input/metadata.rs b/src/input/metadata.rs index 4bba6242c..c7ced29da 100644 --- a/src/input/metadata.rs +++ b/src/input/metadata.rs @@ -2,10 +2,10 @@ use crate::constants::*; #[cfg(not(feature = "serenity"))] use serde_json::Value; #[cfg(feature = "serenity")] -use serenity::json::Value; -#[cfg(feature = "serenity")] #[allow(unused_imports)] use serenity::json::prelude::ValueAccess; +#[cfg(feature = "serenity")] +use serenity::json::Value; use std::time::Duration; /// Information about an [`Input`] source. diff --git a/src/input/ytdl_src.rs b/src/input/ytdl_src.rs index 55222d2d0..51f27b2d7 100644 --- a/src/input/ytdl_src.rs +++ b/src/input/ytdl_src.rs @@ -94,9 +94,13 @@ pub(crate) async fn _ytdl(uri: &str, pre_args: &[&str]) -> Result { #[cfg(feature = "serenity")] { - serenity::json::prelude::from_slice(&mut o_vec[..len]).map_err(|err| Error::Json { - error: err, - parsed_text: std::str::from_utf8(&o_vec).unwrap_or_default().to_string(), + serenity::json::prelude::from_slice(&mut o_vec[..len]).map_err(|err| { + Error::Json { + error: err, + parsed_text: std::str::from_utf8(&o_vec) + .unwrap_or_default() + .to_string(), + } }) } } else { @@ -170,10 +174,11 @@ pub(crate) async fn _ytdl_metadata(uri: &str) -> Result { .unwrap_or_else(|| o_vec.len()); #[cfg(feature = "serenity")] - let value = serenity::json::prelude::from_slice(&mut o_vec[..end]).map_err(|err| Error::Json { - error: err, - parsed_text: std::str::from_utf8(&o_vec).unwrap_or_default().to_string(), - })?; + let value = + serenity::json::prelude::from_slice(&mut o_vec[..end]).map_err(|err| Error::Json { + error: err, + parsed_text: std::str::from_utf8(&o_vec).unwrap_or_default().to_string(), + })?; #[cfg(not(feature = "serenity"))] let value = serde_json::from_slice(&o_vec[..end]).map_err(|err| Error::Json { error: err, diff --git a/src/tracks/error.rs b/src/tracks/error.rs index 1acada94e..9cf3dc3fa 100644 --- a/src/tracks/error.rs +++ b/src/tracks/error.rs @@ -24,8 +24,9 @@ impl fmt::Display for TrackError { write!(f, "failed to operate on track (handle): ")?; match self { TrackError::Finished => write!(f, "track ended"), - TrackError::InvalidTrackEvent => - write!(f, "given event listener can't be fired on a track"), + TrackError::InvalidTrackEvent => { + write!(f, "given event listener can't be fired on a track") + }, TrackError::SeekUnsupported => write!(f, "track did not support seeking"), } } diff --git a/src/ws.rs b/src/ws.rs index f44cf41e1..b699d9d49 100644 --- a/src/ws.rs +++ b/src/ws.rs @@ -19,10 +19,10 @@ use futures::{SinkExt, StreamExt, TryStreamExt}; #[cfg(not(feature = "serenity"))] use serde_json::Error as JsonError; #[cfg(feature = "serenity")] -use serenity::json::JsonError; -#[cfg(feature = "serenity")] #[allow(unused_imports)] use serenity::json::prelude::ValueAccess; +#[cfg(feature = "serenity")] +use serenity::json::JsonError; #[cfg(not(feature = "tokio-02-marker"))] use tokio::time::{timeout, Duration}; #[cfg(feature = "tokio-02-marker")] @@ -155,7 +155,8 @@ impl SenderExt for WsStream { pub(crate) fn convert_ws_message(message: Option) -> Result> { Ok(match message { #[cfg(feature = "serenity")] - Some(Message::Text(mut payload)) => serenity::json::prelude::from_str(&mut payload).map(Some)?, + Some(Message::Text(mut payload)) => + serenity::json::prelude::from_str(&mut payload).map(Some)?, #[cfg(not(feature = "serenity"))] Some(Message::Text(payload)) => serde_json::from_str(&payload).map(Some)?, Some(Message::Binary(bytes)) => { From ab6472816134bdf6edf8cbd87e8b5d31d7e9c4cd Mon Sep 17 00:00:00 2001 From: vicky5124 Date: Tue, 7 Dec 2021 19:23:14 +0100 Subject: [PATCH 12/20] Updated serenity to next from path modified: Cargo.toml modified: examples/serenity/voice_receive/Cargo.toml modified: src/input/metadata.rs modified: src/ws.rs --- Cargo.toml | 12 ++++++------ examples/serenity/voice_receive/Cargo.toml | 6 +++--- src/input/metadata.rs | 3 +-- src/ws.rs | 2 +- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2d087968f..d943b6d76 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -76,15 +76,13 @@ version = "0.8" optional = true default-features = false features = ["voice", "gateway"] -#git = "https://github.com/serenity-rs/serenity" -#branch = "next" -path = "../serenity" +git = "https://github.com/serenity-rs/serenity" +branch = "next" [dependencies.serenity-voice-model] optional = true -#git = "https://github.com/serenity-rs/serenity" -#branch = "next" -path = "../serenity/voice-model" +git = "https://github.com/serenity-rs/serenity" +branch = "next" [dependencies.spin_sleep] optional = true @@ -194,6 +192,8 @@ zlib-simd = ["twilight-gateway/zlib-simd"] zlib-stock = ["twilight-gateway/zlib-stock"] serenity-deps = ["async-trait"] +simdjson = [] + rustls-marker = [] native-marker = [] diff --git a/examples/serenity/voice_receive/Cargo.toml b/examples/serenity/voice_receive/Cargo.toml index bac3c85eb..20df51b42 100644 --- a/examples/serenity/voice_receive/Cargo.toml +++ b/examples/serenity/voice_receive/Cargo.toml @@ -10,13 +10,13 @@ tracing-subscriber = "0.2" tracing-futures = "0.2" [dependencies.songbird] +features = ["simdjson"] path = "../../../" [dependencies.serenity] features = ["client", "standard_framework", "voice", "rustls_backend", "simdjson"] -#git = "https://github.com/serenity-rs/serenity" -#branch = "next" -path = "../../../../serenity" +git = "https://github.com/serenity-rs/serenity" +branch = "next" [dependencies.tokio] version = "1.0" diff --git a/src/input/metadata.rs b/src/input/metadata.rs index c7ced29da..0119a8329 100644 --- a/src/input/metadata.rs +++ b/src/input/metadata.rs @@ -1,8 +1,7 @@ use crate::constants::*; #[cfg(not(feature = "serenity"))] use serde_json::Value; -#[cfg(feature = "serenity")] -#[allow(unused_imports)] +#[cfg(all(feature = "serenity", feature = "simdjson"))] use serenity::json::prelude::ValueAccess; #[cfg(feature = "serenity")] use serenity::json::Value; diff --git a/src/ws.rs b/src/ws.rs index b699d9d49..2bb91d2e8 100644 --- a/src/ws.rs +++ b/src/ws.rs @@ -18,7 +18,7 @@ use async_tungstenite_compat::{ use futures::{SinkExt, StreamExt, TryStreamExt}; #[cfg(not(feature = "serenity"))] use serde_json::Error as JsonError; -#[cfg(feature = "serenity")] +#[cfg(all(feature = "serenity", feature = "simdjson"))] #[allow(unused_imports)] use serenity::json::prelude::ValueAccess; #[cfg(feature = "serenity")] From c749f2ad056fc20ab73c97debf7a35d6d5798533 Mon Sep 17 00:00:00 2001 From: vicky5124 Date: Tue, 7 Dec 2021 19:23:45 +0100 Subject: [PATCH 13/20] Remove simdjson features modified: examples/serenity/voice_receive/Cargo.toml --- examples/serenity/voice_receive/Cargo.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/serenity/voice_receive/Cargo.toml b/examples/serenity/voice_receive/Cargo.toml index 20df51b42..3670d5798 100644 --- a/examples/serenity/voice_receive/Cargo.toml +++ b/examples/serenity/voice_receive/Cargo.toml @@ -10,11 +10,10 @@ tracing-subscriber = "0.2" tracing-futures = "0.2" [dependencies.songbird] -features = ["simdjson"] path = "../../../" [dependencies.serenity] -features = ["client", "standard_framework", "voice", "rustls_backend", "simdjson"] +features = ["client", "standard_framework", "voice", "rustls_backend"] git = "https://github.com/serenity-rs/serenity" branch = "next" From 86feb9908b0b59b010a461894f114190fba604ce Mon Sep 17 00:00:00 2001 From: vicky5124 Date: Tue, 7 Dec 2021 19:25:10 +0100 Subject: [PATCH 14/20] document simdjson feature modified: README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 30195fe7d..b3318511e 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ The library offers: * And, by default, a fully featured voice system featuring events, queues, RT(C)P packet handling, seeking on compatible streams, shared multithreaded audio stream caches, and direct Opus data passthrough from DCA files. + * To be able to use `simd-json` from serenity, you will need to enable the `simdjson` + feature on both songbird and serenity. ## Intents Songbird's gateway functionality requires you to specify the `GUILD_VOICE_STATES` intent. From 889cbed36e572422d9b478f2409df00adcaa3cec Mon Sep 17 00:00:00 2001 From: vicky5124 Date: Tue, 7 Dec 2021 19:27:34 +0100 Subject: [PATCH 15/20] Add simdjson to tests modified: .github/workflows/ci.yml --- .github/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 66d1935e0..870a0ff64 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -59,6 +59,9 @@ jobs: - name: legacy tokio features: serenity-rustls-tokio-02 driver-tokio-02 dont-test: true + - name: simd json + features: simdjson serenity-rustls driver gateway + dont-test: true steps: - name: Checkout sources From 8ca77e1e2160167938c6ef228ba91af5c3bc8d28 Mon Sep 17 00:00:00 2001 From: vicky5124 Date: Tue, 7 Dec 2021 20:37:17 +0100 Subject: [PATCH 16/20] Add serenity's simdjson to test modified: .github/workflows/ci.yml --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 870a0ff64..3422f8ee6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -60,7 +60,7 @@ jobs: features: serenity-rustls-tokio-02 driver-tokio-02 dont-test: true - name: simd json - features: simdjson serenity-rustls driver gateway + features: simdjson serenity-rustls driver gateway serenity/simdjson dont-test: true steps: From 4b7e9e950e749d98e8bcca60fac60398b07bcdb3 Mon Sep 17 00:00:00 2001 From: vicky5124 Date: Fri, 10 Dec 2021 18:56:24 +0100 Subject: [PATCH 17/20] Fix SIMD compilation new file: .cargo/config.toml --- .cargo/config.toml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 000000000..943198092 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +rustflags = ["-C", "target-cpu=haswell"] From 1fe3edf5c7c46076ab53b622429ab5cdf9f67e86 Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Sun, 24 Jul 2022 23:00:27 +0100 Subject: [PATCH 18/20] Better rustflag handling, and use our own simdjson (not just serenity) --- .cargo/config.toml | 2 -- .github/workflows/ci.yml | 2 +- Cargo.toml | 5 +++++ 3 files changed, 6 insertions(+), 3 deletions(-) delete mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index 943198092..000000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[build] -rustflags = ["-C", "target-cpu=haswell"] diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 14bc4f9e3..eadb18578 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,7 +58,7 @@ jobs: features: serenity-rustls dont-test: true - name: simd json - features: simdjson serenity-rustls driver gateway serenity/simdjson + features: simd-json serenity-rustls driver gateway serenity/simd_json rustflags: -C target-cpu=native dont-test: true diff --git a/Cargo.toml b/Cargo.toml index 8049654f9..09b6476bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -106,6 +106,11 @@ optional = true git = "https://github.com/serenity-rs/serenity" branch = "next" +[dependencies.simd-json] +optional = true +features = ["serde_impl"] +version = "0.6.0" + [dependencies.streamcatcher] optional = true version = "1" From f28fc85552b110682e21e25329c49baf1ef19e92 Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Mon, 25 Jul 2022 13:38:55 +0100 Subject: [PATCH 19/20] Migrate metadata handling, all simd_json uses to crate::json --- Makefile.toml | 13 +++++- src/driver/mod.rs | 2 - src/error.rs | 4 ++ src/input/adapters/cached/compressed.rs | 2 +- src/input/adapters/cached/error.rs | 3 +- src/input/codecs/dca/mod.rs | 6 ++- src/input/error.rs | 5 --- src/input/metadata/mod.rs | 5 ++- src/input/sources/file.rs | 4 +- src/input/sources/ytdl.rs | 6 ++- src/lib.rs | 6 +++ src/ws.rs | 58 +++++-------------------- 12 files changed, 49 insertions(+), 65 deletions(-) diff --git a/Makefile.toml b/Makefile.toml index 7e0e9581f..881881917 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -8,6 +8,12 @@ args = ["fmt", "--all"] args = ["build", "--features", "full-doc"] dependencies = ["format"] +[tasks.build-simd] +args = ["build", "--features", "full-doc,simd-json,serenity/simd_json,twilight-gateway/simd-json"] +command = "cargo" +dependencies = ["format"] +env = { "RUSTFLAGS" = "-C target-cpu=native" } + [tasks.build-examples] args = ["build", "--manifest-path", "./examples/Cargo.toml", "--workspace"] command = "cargo" @@ -24,7 +30,7 @@ command = "cargo" dependencies = ["format"] [tasks.build-variants] -dependencies = ["build", "build-gateway", "build-driver"] +dependencies = ["build", "build-gateway", "build-driver", "build-simd"] [tasks.check] args = ["check", "--features", "full-doc"] @@ -37,6 +43,11 @@ dependencies = ["format"] [tasks.test] args = ["test", "--features", "full-doc"] +[tasks.test-simd] +args = ["test", "--features", "full-doc,simd-json,serenity/simd_json,twilight-gateway/simd-json"] +command = "cargo" +env = { "RUSTFLAGS" = "-C target-cpu=native" } + [tasks.bench] description = "Runs performance benchmarks." category = "Test" diff --git a/src/driver/mod.rs b/src/driver/mod.rs index e6eaf3378..20a3f02b5 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -28,8 +28,6 @@ pub use mix_mode::MixMode; #[cfg(test)] pub use test_config::*; -#[cfg(feature = "builtin-queue")] -use crate::tracks; #[cfg(feature = "builtin-queue")] use crate::tracks::TrackQueue; use crate::{ diff --git a/src/error.rs b/src/error.rs index 2691f4e93..17769224a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,8 +2,12 @@ #[cfg(feature = "serenity")] use futures::channel::mpsc::TrySendError; +#[cfg(not(feature = "simd-json"))] +pub use serde_json::Error as JsonError; #[cfg(feature = "serenity")] use serenity::gateway::InterMessage; +#[cfg(feature = "simd-json")] +pub use simd_json::Error as JsonError; #[cfg(feature = "gateway")] use std::{error::Error, fmt}; #[cfg(feature = "twilight")] diff --git a/src/input/adapters/cached/compressed.rs b/src/input/adapters/cached/compressed.rs index c15073cc9..ac8b8996f 100644 --- a/src/input/adapters/cached/compressed.rs +++ b/src/input/adapters/cached/compressed.rs @@ -199,7 +199,7 @@ impl Compressed { )?; let mut metabytes = b"DCA1\0\0\0\0".to_vec(); let orig_len = metabytes.len(); - serde_json::to_writer(&mut metabytes, &metadata)?; + crate::json::to_writer(&mut metabytes, &metadata)?; let meta_len = (metabytes.len() - orig_len) .try_into() .map_err(|_| CodecCacheError::MetadataTooLarge)?; diff --git a/src/input/adapters/cached/error.rs b/src/input/adapters/cached/error.rs index e5892fb3c..4cc63bbe9 100644 --- a/src/input/adapters/cached/error.rs +++ b/src/input/adapters/cached/error.rs @@ -1,6 +1,5 @@ -use crate::input::AudioStreamError; +use crate::{error::JsonError, input::AudioStreamError}; use audiopus::error::Error as OpusError; -use serde_json::Error as JsonError; use std::{ error::Error as StdError, fmt::{Display, Formatter, Result as FmtResult}, diff --git a/src/input/codecs/dca/mod.rs b/src/input/codecs/dca/mod.rs index 826875098..45fcf0757 100644 --- a/src/input/codecs/dca/mod.rs +++ b/src/input/codecs/dca/mod.rs @@ -110,9 +110,11 @@ impl FormatReader for DcaReader { return symph_err::decode_error("missing DCA1 metadata block"); } - let raw_json = source.read_boxed_slice_exact(size as usize)?; + let mut raw_json = source.read_boxed_slice_exact(size as usize)?; - let metadata: DcaMetadata = serde_json::from_slice::(&raw_json) + // NOTE: must be mut for simd-json. + #[allow(clippy::unnecessary_mut_passed)] + let metadata: DcaMetadata = crate::json::from_slice::(&mut raw_json) .map_err(|_| SymphError::DecodeError("malformed DCA1 metadata block"))?; let mut revision = MetadataBuilder::new(); diff --git a/src/input/error.rs b/src/input/error.rs index 2e485a04b..7072584c7 100644 --- a/src/input/error.rs +++ b/src/input/error.rs @@ -1,11 +1,6 @@ use std::{error::Error, fmt::Display, time::Duration}; use symphonia_core::errors::Error as SymphError; -#[cfg(not(feature = "serenity"))] -use serde_json::{Error as JsonError, Value}; -#[cfg(feature = "serenity")] -use serenity::json::prelude::{JsonError, Value}; - /// Errors encountered when creating an [`AudioStream`] or requesting metadata /// from a [`Compose`]. /// diff --git a/src/input/metadata/mod.rs b/src/input/metadata/mod.rs index 916ac610f..e27fd1a25 100644 --- a/src/input/metadata/mod.rs +++ b/src/input/metadata/mod.rs @@ -1,3 +1,4 @@ +use crate::error::JsonError; use std::time::Duration; use symphonia_core::{meta::Metadata as ContainerMetadata, probe::ProbedMetadata}; @@ -47,8 +48,8 @@ pub struct AuxMetadata { impl AuxMetadata { /// Extract metadata and details from the output of `ffprobe -of json`. - pub fn from_ffprobe_json(value: &[u8]) -> Result { - let output: ffprobe::Output = serde_json::from_slice(value)?; + pub fn from_ffprobe_json(value: &mut [u8]) -> Result { + let output: ffprobe::Output = crate::json::from_slice(value)?; Ok(output.into_aux_metadata()) } diff --git a/src/input/sources/file.rs b/src/input/sources/file.rs index 756a81688..5597f1f5c 100644 --- a/src/input/sources/file.rs +++ b/src/input/sources/file.rs @@ -68,13 +68,13 @@ impl + Send + Sync> Compose for File

{ "-i", ]; - let output = Command::new("ffprobe") + let mut output = Command::new("ffprobe") .args(&args) .output() .await .map_err(|e| AudioStreamError::Fail(Box::new(e)))?; - AuxMetadata::from_ffprobe_json(&output.stdout[..]) + AuxMetadata::from_ffprobe_json(&mut output.stdout[..]) .map_err(|e| AudioStreamError::Fail(Box::new(e))) } } diff --git a/src/input/sources/ytdl.rs b/src/input/sources/ytdl.rs index c79623567..6f032ffde 100644 --- a/src/input/sources/ytdl.rs +++ b/src/input/sources/ytdl.rs @@ -59,13 +59,15 @@ impl YoutubeDl { async fn query(&mut self) -> Result { let ytdl_args = ["-j", &self.url, "-f", "ba[abr>0][vcodec=none]/best"]; - let output = Command::new(self.program) + let mut output = Command::new(self.program) .args(&ytdl_args) .output() .await .map_err(|e| AudioStreamError::Fail(Box::new(e)))?; - let stdout: Output = serde_json::from_slice(&output.stdout[..]) + // NOTE: must be mut for simd-json. + #[allow(clippy::unnecessary_mut_passed)] + let stdout: Output = crate::json::from_slice(&mut output.stdout[..]) .map_err(|e| AudioStreamError::Fail(Box::new(e)))?; self.metadata = Some(stdout.as_aux_metadata()); diff --git a/src/lib.rs b/src/lib.rs index 50fc45ec8..18461bcf4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,6 +107,12 @@ pub use serenity_voice_model as model; #[cfg(feature = "driver")] pub use typemap_rev as typemap; +// Re-export serde-json APIs locally to minimise conditional config elsewhere. +#[cfg(not(feature = "simd-json"))] +pub(crate) use serde_json as json; +#[cfg(feature = "simd-json")] +pub(crate) use simd_json::serde as json; + #[cfg(feature = "driver")] pub use crate::{ driver::Driver, diff --git a/src/ws.rs b/src/ws.rs index d7188051e..f47e1a95f 100644 --- a/src/ws.rs +++ b/src/ws.rs @@ -1,4 +1,4 @@ -use crate::model::Event; +use crate::{error::JsonError, model::Event}; use async_trait::async_trait; use async_tungstenite::{ @@ -8,14 +8,6 @@ use async_tungstenite::{ WebSocketStream, }; use futures::{SinkExt, StreamExt, TryStreamExt}; -#[cfg(not(feature = "serenity"))] -use serde_json::Error as JsonError; -#[cfg(all(feature = "serenity", feature = "simdjson"))] -#[allow(unused_imports)] -use serenity::json::prelude::ValueAccess; -#[cfg(feature = "serenity")] -use serenity::json::JsonError; -#[cfg(not(feature = "tokio-02-marker"))] use tokio::time::{timeout, Duration}; use tracing::instrument; @@ -99,56 +91,30 @@ impl ReceiverExt for WsStream { #[async_trait] impl SenderExt for SplitSink { async fn send_json(&mut self, value: &Event) -> Result<()> { - #[cfg(feature = "serenity")] - { - Ok(serenity::json::prelude::to_string(value) - .map(Message::Text) - .map_err(Error::from) - .map(|m| self.send(m))? - .await?) - } - - #[cfg(not(feature = "serenity"))] - { - Ok(serde_json::to_string(value) - .map(Message::Text) - .map_err(Error::from) - .map(|m| self.send(m))? - .await?) - } + Ok(crate::json::to_string(value) + .map(Message::Text) + .map_err(Error::from) + .map(|m| self.send(m))? + .await?) } } #[async_trait] impl SenderExt for WsStream { async fn send_json(&mut self, value: &Event) -> Result<()> { - #[cfg(feature = "serenity")] - { - Ok(serenity::json::prelude::to_string(value) - .map(Message::Text) - .map_err(Error::from) - .map(|m| self.send(m))? - .await?) - } - #[cfg(not(feature = "serenity"))] - { - Ok(serde_json::to_string(value) - .map(Message::Text) - .map_err(Error::from) - .map(|m| self.send(m))? - .await?) - } + Ok(crate::json::to_string(value) + .map(Message::Text) + .map_err(Error::from) + .map(|m| self.send(m))? + .await?) } } #[inline] pub(crate) fn convert_ws_message(message: Option) -> Result> { Ok(match message { - #[cfg(feature = "serenity")] Some(Message::Text(mut payload)) => - serenity::json::prelude::from_str(&mut payload).map(Some)?, - #[cfg(not(feature = "serenity"))] - Some(Message::Text(payload)) => serde_json::from_str(&payload).map(Some)?, + crate::json::from_str(payload.as_mut_str()).map(Some)?, Some(Message::Binary(bytes)) => { return Err(Error::UnexpectedBinaryMessage(bytes)); }, From 2d85f3884c96cbfffd337ed721c454740e3a692c Mon Sep 17 00:00:00 2001 From: Kyle Simpson Date: Mon, 25 Jul 2022 13:59:34 +0100 Subject: [PATCH 20/20] Attempt 2 at CI. --- .github/workflows/ci.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eadb18578..4339d6233 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,7 +27,6 @@ jobs: test: name: Test runs-on: ${{ matrix.os || 'ubuntu-latest' }} - rustflags: ${{ matrix.rustflags || '' }} strategy: fail-fast: false @@ -102,7 +101,7 @@ jobs: - name: Set RUSTFLAGS if: runner.os != 'Windows' - run: echo "RUSTFLAGS=${{ runner.rustflags }}" >> $GITHUB_ENV + run: echo "RUSTFLAGS=${{ matrix.rustflags || '' }}" >> $GITHUB_ENV - name: Build all features if: matrix.features == ''