mirror of
https://github.com/Noratrieb/haesli.git
synced 2026-01-14 11:45:02 +01:00
rename lol
This commit is contained in:
parent
c68cd04af7
commit
543e39f129
70 changed files with 283 additions and 266 deletions
8
haesli_datastructure/Cargo.toml
Normal file
8
haesli_datastructure/Cargo.toml
Normal 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]
|
||||
3
haesli_datastructure/src/lib.rs
Normal file
3
haesli_datastructure/src/lib.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
mod message_queue;
|
||||
|
||||
pub use message_queue::MessageQueue;
|
||||
57
haesli_datastructure/src/message_queue.rs
Normal file
57
haesli_datastructure/src/message_queue.rs
Normal 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()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue