Add ExtendableQueue utility and refactor code
This commit is contained in:
parent
fedb55c50a
commit
f0d7f7499f
|
@ -3,15 +3,15 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
util::compile::{CompileOptions, FunctionCompilerState, MutCompilerState},
|
||||
util::{
|
||||
compile::{CompileOptions, FunctionCompilerState, MutCompilerState},
|
||||
extendable_queue::ExtendableQueue,
|
||||
},
|
||||
virtual_fs::VFolder,
|
||||
};
|
||||
|
||||
use super::{function::Function, tag::Tag};
|
||||
use std::{
|
||||
collections::{HashMap, VecDeque},
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
|
||||
/// Namespace of a datapack
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
|
@ -89,20 +89,10 @@ impl Namespace {
|
|||
functions.push_front(("main".to_string(), self.main_function.clone()));
|
||||
}
|
||||
|
||||
let functions = Arc::new(Mutex::new(functions));
|
||||
let mut functions = ExtendableQueue::from(functions);
|
||||
|
||||
loop {
|
||||
let Some((path, function)) = ({
|
||||
let mut functions = functions.lock().unwrap();
|
||||
let entry = functions.pop_front();
|
||||
drop(functions);
|
||||
entry
|
||||
}) else {
|
||||
break;
|
||||
};
|
||||
|
||||
let function_state =
|
||||
FunctionCompilerState::new(&path, &self.name, Arc::downgrade(&functions));
|
||||
while let Some((path, function)) = functions.next() {
|
||||
let function_state = FunctionCompilerState::new(&path, &self.name, functions.clone());
|
||||
root_folder.add_file(
|
||||
&format!("functions/{}.mcfunction", path),
|
||||
function.compile(options, state, &function_state),
|
||||
|
|
|
@ -1,16 +1,14 @@
|
|||
//! Compile options for the compiler.
|
||||
|
||||
use std::{
|
||||
cell::Cell,
|
||||
collections::VecDeque,
|
||||
sync::{Mutex, Weak},
|
||||
};
|
||||
use std::{cell::Cell, sync::Mutex};
|
||||
|
||||
use getset::Getters;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::datapack::Function;
|
||||
|
||||
use super::extendable_queue::ExtendableQueue;
|
||||
|
||||
/// Compile options for the compiler.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CompileOptions {
|
||||
|
@ -46,7 +44,7 @@ pub struct FunctionCompilerState {
|
|||
functions: FunctionQueue,
|
||||
}
|
||||
|
||||
type FunctionQueue = Weak<Mutex<VecDeque<(String, Function)>>>;
|
||||
type FunctionQueue = ExtendableQueue<(String, Function)>;
|
||||
|
||||
impl FunctionCompilerState {
|
||||
/// Create a new function compiler state.
|
||||
|
@ -61,11 +59,6 @@ impl FunctionCompilerState {
|
|||
|
||||
/// Add a function to the queue.
|
||||
pub fn add_function(&self, name: &str, function: Function) {
|
||||
if let Some(queue) = self.functions.upgrade() {
|
||||
queue
|
||||
.lock()
|
||||
.unwrap()
|
||||
.push_back((name.to_string(), function));
|
||||
}
|
||||
self.functions.push((name.to_string(), function));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
//! A queue that can be extended while iterating over it.
|
||||
|
||||
use std::{
|
||||
collections::VecDeque,
|
||||
sync::{Arc, RwLock, Weak},
|
||||
};
|
||||
|
||||
/// A queue that can be extended while iterating over it.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ExtendableQueue<T> {
|
||||
queue: Arc<RwLock<VecDeque<T>>>,
|
||||
}
|
||||
|
||||
impl Default for ExtendableQueue<String> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
queue: Arc::new(RwLock::new(VecDeque::new())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, V> From<V> for ExtendableQueue<T>
|
||||
where
|
||||
V: Into<VecDeque<T>>,
|
||||
{
|
||||
fn from(value: V) -> Self {
|
||||
Self {
|
||||
queue: Arc::new(RwLock::new(value.into())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ExtendableQueue<T> {
|
||||
/// Add an element to the queue.
|
||||
pub fn push(&self, value: T) {
|
||||
self.queue.write().unwrap().push_back(value);
|
||||
}
|
||||
|
||||
/// Get the queue.
|
||||
pub fn get(&self) -> &Arc<RwLock<VecDeque<T>>> {
|
||||
&self.queue
|
||||
}
|
||||
|
||||
/// Get a weak reference to the queue.
|
||||
pub fn get_weak(&self) -> Weak<RwLock<VecDeque<T>>> {
|
||||
Arc::downgrade(&self.queue)
|
||||
}
|
||||
|
||||
/// Clear the queue.
|
||||
pub fn clear(&self) {
|
||||
self.queue.write().unwrap().clear();
|
||||
}
|
||||
|
||||
/// Get the length of the queue.
|
||||
pub fn len(&self) -> usize {
|
||||
self.queue.read().unwrap().len()
|
||||
}
|
||||
|
||||
/// Check if the queue is empty.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.queue.read().unwrap().is_empty()
|
||||
}
|
||||
|
||||
/// Get and remove the next item without needing mutable access.
|
||||
pub fn pop_front(&self) -> Option<T> {
|
||||
self.queue.write().unwrap().pop_front()
|
||||
}
|
||||
}
|
||||
|
||||
impl<A> Extend<A> for ExtendableQueue<A> {
|
||||
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T) {
|
||||
self.queue.write().unwrap().extend(iter);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Iterator for ExtendableQueue<T> {
|
||||
type Item = T;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.queue.write().unwrap().pop_front()
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
//! Utility functions for the Shulkerbox project.
|
||||
|
||||
pub mod compile;
|
||||
pub mod extendable_queue;
|
||||
|
|
Loading…
Reference in New Issue