You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lolistealer/src/work_async/tasklist.rs

98 lines
1.7 KiB

use super::*;
#[cfg(nightly)]
use std::{
collections::LinkedList,
};
#[cfg(not(nightly))]
use linked_list::LinkedList;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TaskList(LinkedList<(usize, String)>, String, usize);
#[cfg(not(nightly))]
unsafe impl std::marker::Send for TaskList{}
#[cfg(not(nightly))]
unsafe impl std::marker::Sync for TaskList{}
fn find<T,F>(list: &LinkedList<T>, mut fun: F) -> Option<usize>
where F: FnMut(&T) -> bool
{
for (i, x) in (0..).zip(list.iter())
{
if fun(x) {
return Some(i);
}
}
None
}
impl TaskList
{
/// Create a new tasklist
pub fn new() -> Self
{
Self(LinkedList::new(), String::new(), 0)
}
pub fn push(&mut self, string: impl Into<String>) -> usize
{
let idx = {
self.2 += 1;
self.2
};
self.0.push_front((idx, string.into()));
self.recalc_buffer();
idx
}
pub fn pop(&mut self, idx: usize) -> bool
{
if let Some(idx) = find(&self.0, |&(i, _)| idx == i)
{
self.0.remove(idx);
self.recalc_buffer();
true
} else {
false
}
}
pub fn clear(&mut self)
{
self.0.clear();
self.recalc_buffer();
}
pub fn pop_value(&mut self, string: impl AsRef<str>) -> bool
{
let string = string.as_ref();
if let Some(idx) = find(&self.0, |(_, s)| s.as_str() == string)
{
self.0.remove(idx);
self.recalc_buffer();
true
} else {
false
}
}
pub fn recalc_buffer(&mut self)
{
self.1 = self.0.iter().map(|(_, s)| s.as_str()).join(", ");
}
pub fn as_str(&self) -> &str
{
&self.1[..]
}
}
impl AsRef<str> for TaskList
{
fn as_ref(&self) -> &str
{
self.as_str()
}
}