rename lol

This commit is contained in:
nora 2022-03-19 14:27:30 +01:00
parent c68cd04af7
commit 543e39f129
70 changed files with 283 additions and 266 deletions

View file

@ -0,0 +1,8 @@
[package]
name = "haesli_datastructure"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,3 @@
mod message_queue;
pub use message_queue::MessageQueue;

View file

@ -0,0 +1,57 @@
// using std::sync::Mutex because it's only temporary anyways
use std::{
collections::VecDeque,
fmt::{Debug, Formatter},
sync::Mutex,
};
/// The data structure behind the message queue.
///
/// Needs to support:
/// * concurrent access
/// * priority
///
/// Currently supports
/// * mutex lol
// todo: see above
pub struct MessageQueue<T> {
deque: Mutex<VecDeque<T>>,
}
impl<T> MessageQueue<T> {
pub fn new() -> Self {
Self {
deque: Mutex::default(),
}
}
pub fn append(&self, message: T) {
let mut lock = self.deque.lock().unwrap();
lock.push_back(message);
}
pub fn try_get(&self) -> Option<T> {
let mut lock = self.deque.lock().unwrap();
lock.pop_front()
}
pub fn len(&self) -> usize {
self.deque.lock().unwrap().len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl<T> Default for MessageQueue<T> {
fn default() -> Self {
Self::new()
}
}
impl<T: Debug> Debug for MessageQueue<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MessageQueue").finish_non_exhaustive()
}
}