From fd13f7ae64dc5921bf2c9a361d5997cdc8ae4ca5 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 10 Apr 2021 22:49:59 +0200 Subject: [PATCH] Reimplement shuffle for VecDeque in coreutils Introduced the new trait Shuffle that is implemented for VecDeque and shuffles it. Previously the VecDeque was shuffled with a function provided in the main utils module. Signed-off-by: trivernis --- Cargo.lock | 1 + bot-coreutils/Cargo.lock | 64 ++++++++++++++++++++++++++++++++++++ bot-coreutils/Cargo.toml | 3 +- bot-coreutils/src/lib.rs | 1 + bot-coreutils/src/shuffle.rs | 20 +++++++++++ src/providers/music/queue.rs | 4 +-- src/utils/mod.rs | 12 ------- 7 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 bot-coreutils/src/shuffle.rs diff --git a/Cargo.lock b/Cargo.lock index 972a1eb..a76e4a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -184,6 +184,7 @@ version = "0.1.0" dependencies = [ "log 0.4.14", "mime_guess", + "rand 0.8.3", "tokio", "url", ] diff --git a/bot-coreutils/Cargo.lock b/bot-coreutils/Cargo.lock index 2d0bc50..05faaae 100644 --- a/bot-coreutils/Cargo.lock +++ b/bot-coreutils/Cargo.lock @@ -12,6 +12,7 @@ version = "0.1.0" dependencies = [ "log", "mime_guess", + "rand", "tokio", "url", ] @@ -38,6 +39,17 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "idna" version = "0.2.2" @@ -141,6 +153,52 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc0e1f259c92177c30a4c9d177246edd0a3568b25756a977d0632cf8fa37e905" +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + [[package]] name = "signal-hook-registry" version = "1.3.0" @@ -227,6 +285,12 @@ version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + [[package]] name = "winapi" version = "0.3.9" diff --git a/bot-coreutils/Cargo.toml b/bot-coreutils/Cargo.toml index de19dcb..1811ea2 100644 --- a/bot-coreutils/Cargo.toml +++ b/bot-coreutils/Cargo.toml @@ -10,4 +10,5 @@ edition = "2018" tokio = {version = "1.4.0", features = ["process", "io-util"]} log = "0.4.14" url = "2.2.1" -mime_guess = "2.0.3" \ No newline at end of file +mime_guess = "2.0.3" +rand = "0.8.3" \ No newline at end of file diff --git a/bot-coreutils/src/lib.rs b/bot-coreutils/src/lib.rs index 2e0c021..c53855b 100644 --- a/bot-coreutils/src/lib.rs +++ b/bot-coreutils/src/lib.rs @@ -2,5 +2,6 @@ mod tests; pub mod process; +pub mod shuffle; /// Utilities to quickly check strings that represent urls pub mod url; diff --git a/bot-coreutils/src/shuffle.rs b/bot-coreutils/src/shuffle.rs new file mode 100644 index 0000000..f768b77 --- /dev/null +++ b/bot-coreutils/src/shuffle.rs @@ -0,0 +1,20 @@ +use rand::Rng; +use std::collections::VecDeque; + +pub trait Shuffle { + fn shuffle(&mut self); +} + +impl Shuffle for VecDeque { + /// Fisher-Yates shuffle implementation + /// for VecDeque. + fn shuffle(&mut self) { + let mut rng = rand::thread_rng(); + let mut i = self.len(); + + while i >= 2 { + i -= 1; + self.swap(i, rng.gen_range(0..i + 1)) + } + } +} diff --git a/src/providers/music/queue.rs b/src/providers/music/queue.rs index 90972a4..93401e6 100644 --- a/src/providers/music/queue.rs +++ b/src/providers/music/queue.rs @@ -5,8 +5,8 @@ use songbird::tracks::TrackHandle; use crate::messages::music::NowPlayingMessage; use crate::providers::music::responses::{PlaylistEntry, VideoInformation}; use crate::providers::music::youtube_dl; -use crate::utils::shuffle_vec_deque; use aspotify::{Track, TrackSimplified}; +use bot_coreutils::shuffle::Shuffle; #[derive(Clone)] pub struct MusicQueue { @@ -40,7 +40,7 @@ impl MusicQueue { /// Shuffles the queue pub fn shuffle(&mut self) { - shuffle_vec_deque(&mut self.inner) + self.inner.shuffle() } /// Returns a reference to the inner deque diff --git a/src/utils/mod.rs b/src/utils/mod.rs index c0a9bbe..f59d102 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,24 +1,12 @@ use crate::utils::error::BotResult; -use rand::Rng; use serenity::client::Context; use serenity::model::channel::Message; -use std::collections::VecDeque; pub(crate) mod context_data; pub(crate) mod error; pub(crate) mod logging; pub(crate) mod messages; -/// Fisher-Yates shuffle for VecDeque -pub fn shuffle_vec_deque(deque: &mut VecDeque) { - let mut rng = rand::thread_rng(); - let mut i = deque.len(); - while i >= 2 { - i -= 1; - deque.swap(i, rng.gen_range(0..i + 1)) - } -} - /// Returns the message the given message is a reply to or the message sent before that pub async fn get_previous_message_or_reply( ctx: &Context,