From 0586fb6f309313df0b4e0db24daa6b52ce10b459 Mon Sep 17 00:00:00 2001 From: rgiot Date: Thu, 9 Jul 2020 16:41:44 +0200 Subject: [PATCH] Remove uneeded code for random selection --- src/db.rs | 22 +++++++++++++--------- src/main.rs | 7 +++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/db.rs b/src/db.rs index 703fc681..ba49e31d 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,7 +1,8 @@ use rand::seq::IteratorRandom; use rand::rngs::ThreadRng; use itertools::Itertools; -use rand::Rng; +use rand::random; + pub use std::convert::TryFrom; include!(concat!(env!("OUT_DIR"), "/", "db.rs")); @@ -217,12 +218,15 @@ impl Music { } pub fn is_theme_valid(&self) -> bool { - self.selected_theme() < self.header().unwrap().nb_themes() + match self.header() { + Ok(h) => self.selected_theme() < h.nb_themes(), + Err(_) => true + } } - pub fn select_random_theme(&mut self, rng: &mut ThreadRng) { + pub fn select_random_theme(&mut self) { self.select_theme( - rng.gen::() % self.header().unwrap().nb_themes() + random::() % self.header().unwrap().nb_themes() ) } @@ -249,7 +253,7 @@ impl CpcMix { /// Return None if: /// - music is not found /// - music is found but theme does not exists - pub fn music(&self, key: &str, rng: &mut ThreadRng) -> Option { + pub fn music(&self, key: &str) -> Option { let (key, theme) = if key.contains(":") { let words = key.split(':').collect::>(); @@ -267,7 +271,7 @@ impl CpcMix { })) .and_then(|mut m| { let theme = if theme == "?" { - m.select_random_theme(rng); + m.select_random_theme(); } else { let theme = u8::from_str_radix(theme, 10).unwrap(); @@ -282,9 +286,9 @@ impl CpcMix { }) } - pub fn random(&self, rng: &mut ThreadRng) -> Music { - let mut music = self.music(self.keys().choose(rng).unwrap(), rng).unwrap(); - music.select_random_theme(rng); + pub fn random(&self) -> Music { + let mut music = self.music(self.keys().choose(&mut rand::thread_rng()).unwrap()).unwrap(); + music.select_random_theme(); music } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 6eb93284..d43518db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,6 @@ use std::thread; use itertools::Itertools; use cpcmix; - /// Dummy player /// M4 must have a directory /tmp /// cpcmix @@ -37,7 +36,7 @@ with having such values: "list" => { let repr = mix.keys() - .map(|k| (k, mix.music(k, &mut rand::thread_rng()).unwrap())) + .map(|k| (k, mix.music(k).unwrap())) .map(|(k, m) | ( k, m.title().unwrap_or("".to_owned()), @@ -68,7 +67,7 @@ with having such values: ); return; } - any => mix.music(any, &mut rng) + any => mix.music(any) }; let music = match music { @@ -78,7 +77,7 @@ with having such values: if let Some(music) = mix.keys() - .map(|k| mix.music(k, &mut rand::thread_rng()).unwrap()) + .map(|k| mix.music(k).unwrap()) .filter(|m| { m.author().unwrap_or("??".to_owned()).to_lowercase() == args[2] })