From f3fec5292a5c7b30ccf8b087a5eb3218e3716733 Mon Sep 17 00:00:00 2001 From: Miguel da Mota Date: Sun, 10 Mar 2024 20:11:39 +0100 Subject: [PATCH] chore: use `VecDeque` for music queue --- src/music/music_queue.rs | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/music/music_queue.rs b/src/music/music_queue.rs index 37b76fb..e4480fe 100644 --- a/src/music/music_queue.rs +++ b/src/music/music_queue.rs @@ -2,11 +2,19 @@ use serenity::all::GuildId; use songbird::input::YoutubeDl; use tokio::sync::Mutex; -use std::collections::HashMap; +use std::collections::{HashMap, VecDeque}; use std::sync::Arc; type MusicQueueItem = Arc>; +#[derive(Debug)] +pub struct MusicQueue { + // God this sucks. This needs to be reprogrammed properly. + pub guild_id: GuildId, + pub queue: VecDeque, + pub now_playing: Option, +} + lazy_static! { static ref HASHMAP: Mutex> = Mutex::new(HashMap::new()); } @@ -18,7 +26,7 @@ async fn get_music_queue(guild_id: &GuildId) -> MusicQueueItem { .entry(*guild_id) .or_insert(Arc::new(Mutex::new(MusicQueue { guild_id: *guild_id, - queue: Vec::new(), + queue: VecDeque::new(), now_playing: None, }))) .clone() @@ -35,14 +43,6 @@ where f(&mut queue) } -#[derive(Debug)] -pub struct MusicQueue { - // God this sucks. This needs to be reprogrammed properly. - pub guild_id: GuildId, - pub queue: Vec, - pub now_playing: Option, -} - pub async fn delete_queue(guild_id: &GuildId) { with_music_queue(guild_id, |queue| { queue.now_playing = None; @@ -52,21 +52,11 @@ pub async fn delete_queue(guild_id: &GuildId) { } pub async fn add_to_queue(guild_id: &GuildId, input: YoutubeDl) { - with_music_queue(guild_id, |queue| { - queue.queue.push(input); - }) - .await; + with_music_queue(guild_id, |queue| queue.queue.push_back(input)).await; } pub async fn get_head(guild_id: &GuildId) -> Option { - with_music_queue(guild_id, |queue| { - if queue.queue.is_empty() { - None - } else { - Some(queue.queue.remove(0)) - } - }) - .await + with_music_queue(guild_id, |queue| queue.queue.pop_front()).await } pub async fn set_now_playing(guild_id: &GuildId, now_playing: Option) {