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

DB: Implementation of iterators returning references

parent d3031456
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 {
/// 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
......
......@@ -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> = (
......
......@@ -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()
}
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter