Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 5476c5d3 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

DB: Implementation of iterators returning references

parent d4309b5b
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -161,7 +161,7 @@ impl LktDatabaseConnection { ...@@ -161,7 +161,7 @@ impl LktDatabaseConnection {
/// Peek the next kara to play /// Peek the next kara to play
pub fn peek_next_kara_in_queue(&self) -> Option<u64> { 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 /// Pop the next kara to play
......
...@@ -8,12 +8,7 @@ pub mod schema; ...@@ -8,12 +8,7 @@ pub mod schema;
pub(self) use diesel::prelude::*; pub(self) use diesel::prelude::*;
pub(self) use error::*; pub(self) use error::*;
pub(self) use std::{ pub(self) use std::{collections::VecDeque, ops::Range, path::Path};
collections::VecDeque,
ops::{Index, Range},
path::Path,
slice::SliceIndex,
};
/// All the information needed to add a kara recieved from a repo! /// All the information needed to add a kara recieved from a repo!
pub type NewKaraRequest<'a> = ( pub type NewKaraRequest<'a> = (
......
...@@ -23,6 +23,16 @@ macro_rules! impl_from_for_proprity { ...@@ -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!(u8);
impl_from_for_proprity!(u16); impl_from_for_proprity!(u16);
impl_from_for_proprity!(u32); impl_from_for_proprity!(u32);
...@@ -33,33 +43,28 @@ impl_from_for_proprity!(i16); ...@@ -33,33 +43,28 @@ impl_from_for_proprity!(i16);
impl_from_for_proprity!(i32); impl_from_for_proprity!(i32);
impl_from_for_proprity!(i64); impl_from_for_proprity!(i64);
impl From<LktDatabasePriority> for i32 { impl_into_for_priority!(i32);
fn from(val: LktDatabasePriority) -> Self { impl_into_for_priority!(usize);
val.value as i32
}
}
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> { pub struct LktDatabaseQueueIter<'a> {
queue: &'a LktDatabaseQueue, queue: &'a LktDatabaseQueue,
priority: Option<usize>, priority: Option<usize>,
index: 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> { pub struct LktDatabaseQueueRangeIter<'a> {
iterator: LktDatabaseQueueIter<'a>, iterator: LktDatabaseQueueIter<'a>,
remaining: usize, remaining: usize,
} }
impl<'a> Iterator for LktDatabaseQueueIter<'a> { impl<'a> Iterator for LktDatabaseQueueIter<'a> {
type Item = u64; type Item = &'a u64;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let priority = self.priority?.clamp(0, LKT_DATABASE_QUEUES_COUNT - 1); let priority = self.priority?.clamp(0, LKT_DATABASE_QUEUES_COUNT - 1);
...@@ -67,7 +72,7 @@ impl<'a> Iterator for LktDatabaseQueueIter<'a> { ...@@ -67,7 +72,7 @@ impl<'a> Iterator for LktDatabaseQueueIter<'a> {
match level.get(self.index) { match level.get(self.index) {
Some(ret) => { Some(ret) => {
self.index += 1; self.index += 1;
Some(*ret) Some(ret)
} }
None if priority == 0 => None, None if priority == 0 => None,
None => { None => {
...@@ -78,6 +83,20 @@ impl<'a> Iterator for LktDatabaseQueueIter<'a> { ...@@ -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. /// The queue datastructure used for storing karas in the queue.
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct LktDatabaseQueue { pub struct LktDatabaseQueue {
...@@ -117,7 +136,7 @@ impl LktDatabaseQueue { ...@@ -117,7 +136,7 @@ impl LktDatabaseQueue {
} }
} }
pub fn peek_next(&self) -> Option<u64> { pub fn peek_next(&self) -> Option<&u64> {
self.iter().next() self.iter().next()
} }
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter