diff --git a/src/rust/liblektor-rs/lektor_db/src/connexion.rs b/src/rust/liblektor-rs/lektor_db/src/connexion.rs index 36cb14a954c28114f4cca742d9b6b42ffb8d558b..680477611116f6ccd295bd96b84767acbce09312 100644 --- a/src/rust/liblektor-rs/lektor_db/src/connexion.rs +++ b/src/rust/liblektor-rs/lektor_db/src/connexion.rs @@ -161,7 +161,7 @@ impl LktDatabaseConnection { /// Peek the next kara to play pub fn peek_next_kara_in_queue(&self) -> Option<u64> { - self.queue.peek_next() + self.queue.peek_next().copied() } /// Pop the next kara to play diff --git a/src/rust/liblektor-rs/lektor_db/src/lib.rs b/src/rust/liblektor-rs/lektor_db/src/lib.rs index 123d70c57d28f3c29d018753297a9d43ea681df4..8f8e39f1cf846d1f91aa192a84d7080a489b4aa6 100644 --- a/src/rust/liblektor-rs/lektor_db/src/lib.rs +++ b/src/rust/liblektor-rs/lektor_db/src/lib.rs @@ -8,12 +8,7 @@ pub mod schema; pub(self) use diesel::prelude::*; pub(self) use error::*; -pub(self) use std::{ - collections::VecDeque, - ops::{Index, Range}, - path::Path, - slice::SliceIndex, -}; +pub(self) use std::{collections::VecDeque, ops::Range, path::Path}; /// All the information needed to add a kara recieved from a repo! pub type NewKaraRequest<'a> = ( diff --git a/src/rust/liblektor-rs/lektor_db/src/queue.rs b/src/rust/liblektor-rs/lektor_db/src/queue.rs index 50ab6ae35134c6fd13fa37d1307816f1bb3180ab..f41050a281fa2c54e77adbbab7ebaa6054457c69 100644 --- a/src/rust/liblektor-rs/lektor_db/src/queue.rs +++ b/src/rust/liblektor-rs/lektor_db/src/queue.rs @@ -23,6 +23,16 @@ macro_rules! impl_from_for_proprity { }; } +macro_rules! impl_into_for_priority { + ($ty: ty) => { + impl From<LktDatabasePriority> for $ty { + fn from(val: LktDatabasePriority) -> Self { + val.value as $ty + } + } + }; +} + impl_from_for_proprity!(u8); impl_from_for_proprity!(u16); impl_from_for_proprity!(u32); @@ -33,33 +43,28 @@ impl_from_for_proprity!(i16); impl_from_for_proprity!(i32); impl_from_for_proprity!(i64); -impl From<LktDatabasePriority> for i32 { - fn from(val: LktDatabasePriority) -> Self { - val.value as i32 - } -} +impl_into_for_priority!(i32); +impl_into_for_priority!(usize); -impl From<LktDatabasePriority> for usize { - fn from(val: LktDatabasePriority) -> Self { - val.value as usize - } -} - -/// The iterator for the database queue. +/// The iterator for the database queue. The iterator returns references to +/// karas' id to protect modifications while a reference to some karas are +/// present... pub struct LktDatabaseQueueIter<'a> { queue: &'a LktDatabaseQueue, priority: Option<usize>, index: usize, } -/// The iterator for a range of karas from the database queue. +/// The iterator for a range of karas from the database queue. The iterator +/// returns references to karas' id to protect modifications while a reference +/// to some karas are present... pub struct LktDatabaseQueueRangeIter<'a> { iterator: LktDatabaseQueueIter<'a>, remaining: usize, } impl<'a> Iterator for LktDatabaseQueueIter<'a> { - type Item = u64; + type Item = &'a u64; fn next(&mut self) -> Option<Self::Item> { let priority = self.priority?.clamp(0, LKT_DATABASE_QUEUES_COUNT - 1); @@ -67,7 +72,7 @@ impl<'a> Iterator for LktDatabaseQueueIter<'a> { match level.get(self.index) { Some(ret) => { self.index += 1; - Some(*ret) + Some(ret) } None if priority == 0 => None, None => { @@ -78,6 +83,20 @@ impl<'a> Iterator for LktDatabaseQueueIter<'a> { } } +impl<'a> Iterator for LktDatabaseQueueRangeIter<'a> { + type Item = &'a u64; + + fn next(&mut self) -> Option<Self::Item> { + let new_remaining = self.remaining.saturating_sub(1); + if new_remaining != self.remaining { + self.remaining = new_remaining; + self.iterator.next() + } else { + None + } + } +} + /// The queue datastructure used for storing karas in the queue. #[derive(Debug, Default)] pub struct LktDatabaseQueue { @@ -117,7 +136,7 @@ impl LktDatabaseQueue { } } - pub fn peek_next(&self) -> Option<u64> { + pub fn peek_next(&self) -> Option<&u64> { self.iter().next() }