aboutsummaryrefslogtreecommitdiff
path: root/azalea-brigadier/src
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2023-05-05 23:09:57 -0500
committermat <git@matdoes.dev>2023-05-05 23:09:57 -0500
commit12370ab07609bf78baef4ec2302fa4ba44317dae (patch)
tree4fc8eecd79350cd06f52b7d3b5c166a0093fd10f /azalea-brigadier/src
parente4176937f0584a6bcc5aba15abb1e2df6ddf588d (diff)
downloadazalea-drasl-12370ab07609bf78baef4ec2302fa4ba44317dae.tar.xz
change some things to be Arc+RwLock in brigadier
Diffstat (limited to 'azalea-brigadier/src')
-rwxr-xr-xazalea-brigadier/src/builder/argument_builder.rs14
-rwxr-xr-xazalea-brigadier/src/command_dispatcher.rs68
-rwxr-xr-xazalea-brigadier/src/context/command_context.rs6
-rwxr-xr-xazalea-brigadier/src/context/command_context_builder.rs18
-rwxr-xr-xazalea-brigadier/src/context/parsed_command_node.rs6
-rwxr-xr-xazalea-brigadier/src/tree/mod.rs47
6 files changed, 88 insertions, 71 deletions
diff --git a/azalea-brigadier/src/builder/argument_builder.rs b/azalea-brigadier/src/builder/argument_builder.rs
index cfaff618..a1f3d6ae 100755
--- a/azalea-brigadier/src/builder/argument_builder.rs
+++ b/azalea-brigadier/src/builder/argument_builder.rs
@@ -1,3 +1,5 @@
+use parking_lot::RwLock;
+
use crate::{
context::CommandContext,
modifier::RedirectModifier,
@@ -5,7 +7,7 @@ use crate::{
};
use super::{literal_argument_builder::Literal, required_argument_builder::Argument};
-use std::{cell::RefCell, fmt::Debug, rc::Rc};
+use std::{fmt::Debug, rc::Rc, sync::Arc};
#[derive(Debug, Clone)]
pub enum ArgumentBuilderType {
@@ -19,7 +21,7 @@ pub struct ArgumentBuilder<S> {
command: Command<S>,
requirement: Rc<dyn Fn(Rc<S>) -> bool>,
- target: Option<Rc<RefCell<CommandNode<S>>>>,
+ target: Option<Arc<RwLock<CommandNode<S>>>>,
forks: bool,
modifier: Option<Rc<RedirectModifier<S>>>,
@@ -46,7 +48,7 @@ impl<S> ArgumentBuilder<S> {
}
pub fn then_built(mut self, argument: CommandNode<S>) -> Self {
- self.arguments.add_child(&Rc::new(RefCell::new(argument)));
+ self.arguments.add_child(&Arc::new(RwLock::new(argument)));
self
}
@@ -66,13 +68,13 @@ impl<S> ArgumentBuilder<S> {
self
}
- pub fn redirect(self, target: Rc<RefCell<CommandNode<S>>>) -> Self {
+ pub fn redirect(self, target: Arc<RwLock<CommandNode<S>>>) -> Self {
self.forward(target, None, false)
}
pub fn fork(
self,
- target: Rc<RefCell<CommandNode<S>>>,
+ target: Arc<RwLock<CommandNode<S>>>,
modifier: Rc<RedirectModifier<S>>,
) -> Self {
self.forward(target, Some(modifier), true)
@@ -80,7 +82,7 @@ impl<S> ArgumentBuilder<S> {
pub fn forward(
mut self,
- target: Rc<RefCell<CommandNode<S>>>,
+ target: Arc<RwLock<CommandNode<S>>>,
modifier: Option<Rc<RedirectModifier<S>>>,
fork: bool,
) -> Self {
diff --git a/azalea-brigadier/src/command_dispatcher.rs b/azalea-brigadier/src/command_dispatcher.rs
index 29fadfd1..0d84171e 100755
--- a/azalea-brigadier/src/command_dispatcher.rs
+++ b/azalea-brigadier/src/command_dispatcher.rs
@@ -1,3 +1,5 @@
+use parking_lot::RwLock;
+
use crate::{
builder::argument_builder::ArgumentBuilder,
context::{CommandContext, CommandContextBuilder},
@@ -6,26 +8,27 @@ use crate::{
string_reader::StringReader,
tree::CommandNode,
};
-use std::{cell::RefCell, cmp::Ordering, collections::HashMap, marker::PhantomData, mem, rc::Rc};
+use std::{cmp::Ordering, collections::HashMap, mem, rc::Rc, sync::Arc};
/// The root of the command tree. You need to make this to register commands.
#[derive(Default)]
-pub struct CommandDispatcher<'a, S> {
- pub root: Rc<RefCell<CommandNode<S>>>,
- _marker: PhantomData<(S, &'a ())>,
+pub struct CommandDispatcher<S>
+// where
+// Self: Sync + Send,
+{
+ pub root: Arc<RwLock<CommandNode<S>>>,
}
-impl<'a, S> CommandDispatcher<'a, S> {
+impl<S> CommandDispatcher<S> {
pub fn new() -> Self {
Self {
- root: Rc::new(RefCell::new(CommandNode::default())),
- _marker: PhantomData,
+ root: Arc::new(RwLock::new(CommandNode::default())),
}
}
- pub fn register(&mut self, node: ArgumentBuilder<S>) -> Rc<RefCell<CommandNode<S>>> {
- let build = Rc::new(RefCell::new(node.build()));
- self.root.borrow_mut().add_child(&build);
+ pub fn register(&mut self, node: ArgumentBuilder<S>) -> Arc<RwLock<CommandNode<S>>> {
+ let build = Arc::new(RwLock::new(node.build()));
+ self.root.write().add_child(&build);
build
}
@@ -34,9 +37,9 @@ impl<'a, S> CommandDispatcher<'a, S> {
self.parse_nodes(&self.root, &command, context).unwrap()
}
- fn parse_nodes(
+ fn parse_nodes<'a>(
&'a self,
- node: &Rc<RefCell<CommandNode<S>>>,
+ node: &Arc<RwLock<CommandNode<S>>>,
original_reader: &StringReader,
context_so_far: CommandContextBuilder<'a, S>,
) -> Result<ParseResults<'a, S>, CommandSyntaxException> {
@@ -45,21 +48,18 @@ impl<'a, S> CommandDispatcher<'a, S> {
let mut potentials: Vec<ParseResults<S>> = vec![];
let cursor = original_reader.cursor();
- for child in node
- .borrow()
- .get_relevant_nodes(&mut original_reader.clone())
- {
- if !child.borrow().can_use(source.clone()) {
+ for child in node.read().get_relevant_nodes(&mut original_reader.clone()) {
+ if !child.read().can_use(source.clone()) {
continue;
}
let mut context = context_so_far.clone();
let mut reader = original_reader.clone();
let parse_with_context_result =
- child.borrow().parse_with_context(&mut reader, &mut context);
+ child.read().parse_with_context(&mut reader, &mut context);
if let Err(ex) = parse_with_context_result {
errors.insert(
- Rc::new((*child.borrow()).clone()),
+ Rc::new((*child.read()).clone()),
BuiltInExceptions::DispatcherParseException {
message: ex.message(),
}
@@ -70,7 +70,7 @@ impl<'a, S> CommandDispatcher<'a, S> {
}
if reader.can_read() && reader.peek() != ' ' {
errors.insert(
- Rc::new((*child.borrow()).clone()),
+ Rc::new((*child.read()).clone()),
BuiltInExceptions::DispatcherExpectedArgumentSeparator
.create_with_context(&reader),
);
@@ -78,14 +78,14 @@ impl<'a, S> CommandDispatcher<'a, S> {
continue;
}
- context.with_command(&child.borrow().command);
- if reader.can_read_length(if child.borrow().redirect.is_none() {
+ context.with_command(&child.read().command);
+ if reader.can_read_length(if child.read().redirect.is_none() {
2
} else {
1
}) {
reader.skip();
- if let Some(redirect) = &child.borrow().redirect {
+ if let Some(redirect) = &child.read().redirect {
let child_context =
CommandContextBuilder::new(self, source, redirect.clone(), reader.cursor);
let parse = self
@@ -151,30 +151,30 @@ impl<'a, S> CommandDispatcher<'a, S> {
}
pub fn add_paths(
- node: Rc<RefCell<CommandNode<S>>>,
- result: &mut Vec<Vec<Rc<RefCell<CommandNode<S>>>>>,
- parents: Vec<Rc<RefCell<CommandNode<S>>>>,
+ node: Arc<RwLock<CommandNode<S>>>,
+ result: &mut Vec<Vec<Arc<RwLock<CommandNode<S>>>>>,
+ parents: Vec<Arc<RwLock<CommandNode<S>>>>,
) {
let mut current = parents;
current.push(node.clone());
result.push(current.clone());
- for child in node.borrow().children.values() {
+ for child in node.read().children.values() {
Self::add_paths(child.clone(), result, current.clone());
}
}
pub fn get_path(&self, target: CommandNode<S>) -> Vec<String> {
- let rc_target = Rc::new(RefCell::new(target));
- let mut nodes: Vec<Vec<Rc<RefCell<CommandNode<S>>>>> = Vec::new();
+ let rc_target = Arc::new(RwLock::new(target));
+ let mut nodes: Vec<Vec<Arc<RwLock<CommandNode<S>>>>> = Vec::new();
Self::add_paths(self.root.clone(), &mut nodes, vec![]);
for list in nodes {
- if *list.last().expect("Nothing in list").borrow() == *rc_target.borrow() {
+ if *list.last().expect("Nothing in list").read() == *rc_target.read() {
let mut result: Vec<String> = Vec::with_capacity(list.len());
for node in list {
- if node != self.root {
- result.push(node.borrow().name().to_string());
+ if !Arc::ptr_eq(&node, &self.root) {
+ result.push(node.read().name().to_string());
}
}
return result;
@@ -183,10 +183,10 @@ impl<'a, S> CommandDispatcher<'a, S> {
vec![]
}
- pub fn find_node(&self, path: &[&str]) -> Option<Rc<RefCell<CommandNode<S>>>> {
+ pub fn find_node(&self, path: &[&str]) -> Option<Arc<RwLock<CommandNode<S>>>> {
let mut node = self.root.clone();
for name in path {
- if let Some(child) = node.clone().borrow().child(name) {
+ if let Some(child) = node.clone().read().child(name) {
node = child;
} else {
return None;
diff --git a/azalea-brigadier/src/context/command_context.rs b/azalea-brigadier/src/context/command_context.rs
index 98609a6e..88af2002 100755
--- a/azalea-brigadier/src/context/command_context.rs
+++ b/azalea-brigadier/src/context/command_context.rs
@@ -1,9 +1,11 @@
+use parking_lot::RwLock;
+
use super::{parsed_command_node::ParsedCommandNode, string_range::StringRange, ParsedArgument};
use crate::{
modifier::RedirectModifier,
tree::{Command, CommandNode},
};
-use std::{any::Any, cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
+use std::{any::Any, collections::HashMap, fmt::Debug, rc::Rc, sync::Arc};
/// A built `CommandContextBuilder`.
pub struct CommandContext<S> {
@@ -11,7 +13,7 @@ pub struct CommandContext<S> {
pub input: String,
pub arguments: HashMap<String, ParsedArgument>,
pub command: Command<S>,
- pub root_node: Rc<RefCell<CommandNode<S>>>,
+ pub root_node: Arc<RwLock<CommandNode<S>>>,
pub nodes: Vec<ParsedCommandNode<S>>,
pub range: StringRange,
pub child: Option<Rc<CommandContext<S>>>,
diff --git a/azalea-brigadier/src/context/command_context_builder.rs b/azalea-brigadier/src/context/command_context_builder.rs
index b0677158..54063879 100755
--- a/azalea-brigadier/src/context/command_context_builder.rs
+++ b/azalea-brigadier/src/context/command_context_builder.rs
@@ -1,3 +1,5 @@
+use parking_lot::RwLock;
+
use super::{
command_context::CommandContext, parsed_command_node::ParsedCommandNode,
string_range::StringRange, ParsedArgument,
@@ -7,13 +9,13 @@ use crate::{
modifier::RedirectModifier,
tree::{Command, CommandNode},
};
-use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
+use std::{collections::HashMap, fmt::Debug, rc::Rc, sync::Arc};
pub struct CommandContextBuilder<'a, S> {
pub arguments: HashMap<String, ParsedArgument>,
- pub root: Rc<RefCell<CommandNode<S>>>,
+ pub root: Arc<RwLock<CommandNode<S>>>,
pub nodes: Vec<ParsedCommandNode<S>>,
- pub dispatcher: &'a CommandDispatcher<'a, S>,
+ pub dispatcher: &'a CommandDispatcher<S>,
pub source: Rc<S>,
pub command: Command<S>,
pub child: Option<Rc<CommandContextBuilder<'a, S>>>,
@@ -28,7 +30,7 @@ impl<S> Clone for CommandContextBuilder<'_, S> {
arguments: self.arguments.clone(),
root: self.root.clone(),
nodes: self.nodes.clone(),
- dispatcher: self.dispatcher.clone(),
+ dispatcher: self.dispatcher,
source: self.source.clone(),
command: self.command.clone(),
child: self.child.clone(),
@@ -43,7 +45,7 @@ impl<'a, S> CommandContextBuilder<'a, S> {
pub fn new(
dispatcher: &'a CommandDispatcher<S>,
source: Rc<S>,
- root_node: Rc<RefCell<CommandNode<S>>>,
+ root_node: Arc<RwLock<CommandNode<S>>>,
start: usize,
) -> Self {
Self {
@@ -72,14 +74,14 @@ impl<'a, S> CommandContextBuilder<'a, S> {
self.arguments.insert(name.to_string(), argument);
self
}
- pub fn with_node(&mut self, node: Rc<RefCell<CommandNode<S>>>, range: StringRange) -> &Self {
+ pub fn with_node(&mut self, node: Arc<RwLock<CommandNode<S>>>, range: StringRange) -> &Self {
self.nodes.push(ParsedCommandNode {
node: node.clone(),
range: range.clone(),
});
self.range = StringRange::encompassing(&self.range, &range);
- self.modifier = node.borrow().modifier.clone();
- self.forks = node.borrow().forks;
+ self.modifier = node.read().modifier.clone();
+ self.forks = node.read().forks;
self
}
diff --git a/azalea-brigadier/src/context/parsed_command_node.rs b/azalea-brigadier/src/context/parsed_command_node.rs
index ed49928d..bba5d121 100755
--- a/azalea-brigadier/src/context/parsed_command_node.rs
+++ b/azalea-brigadier/src/context/parsed_command_node.rs
@@ -1,10 +1,12 @@
+use parking_lot::RwLock;
+
use super::string_range::StringRange;
use crate::tree::CommandNode;
-use std::{cell::RefCell, rc::Rc};
+use std::sync::Arc;
#[derive(Debug)]
pub struct ParsedCommandNode<S> {
- pub node: Rc<RefCell<CommandNode<S>>>,
+ pub node: Arc<RwLock<CommandNode<S>>>,
pub range: StringRange,
}
diff --git a/azalea-brigadier/src/tree/mod.rs b/azalea-brigadier/src/tree/mod.rs
index bb6af68d..902e288b 100755
--- a/azalea-brigadier/src/tree/mod.rs
+++ b/azalea-brigadier/src/tree/mod.rs
@@ -1,3 +1,5 @@
+use parking_lot::RwLock;
+
use crate::{
builder::{
argument_builder::ArgumentBuilderType, literal_argument_builder::Literal,
@@ -8,7 +10,7 @@ use crate::{
modifier::RedirectModifier,
string_reader::StringReader,
};
-use std::{cell::RefCell, collections::HashMap, fmt::Debug, hash::Hash, ptr, rc::Rc};
+use std::{collections::HashMap, fmt::Debug, hash::Hash, ptr, rc::Rc, sync::Arc};
pub type Command<S> = Option<Rc<dyn Fn(&CommandContext<S>) -> i32>>;
@@ -17,13 +19,13 @@ pub type Command<S> = Option<Rc<dyn Fn(&CommandContext<S>) -> i32>>;
pub struct CommandNode<S> {
pub value: ArgumentBuilderType,
- pub children: HashMap<String, Rc<RefCell<CommandNode<S>>>>,
- pub literals: HashMap<String, Rc<RefCell<CommandNode<S>>>>,
- pub arguments: HashMap<String, Rc<RefCell<CommandNode<S>>>>,
+ pub children: HashMap<String, Arc<RwLock<CommandNode<S>>>>,
+ pub literals: HashMap<String, Arc<RwLock<CommandNode<S>>>>,
+ pub arguments: HashMap<String, Arc<RwLock<CommandNode<S>>>>,
pub command: Command<S>,
pub requirement: Rc<dyn Fn(Rc<S>) -> bool>,
- pub redirect: Option<Rc<RefCell<CommandNode<S>>>>,
+ pub redirect: Option<Arc<RwLock<CommandNode<S>>>>,
pub forks: bool,
pub modifier: Option<Rc<RedirectModifier<S>>>,
}
@@ -62,7 +64,7 @@ impl<S> CommandNode<S> {
}
}
- pub fn get_relevant_nodes(&self, input: &mut StringReader) -> Vec<Rc<RefCell<CommandNode<S>>>> {
+ pub fn get_relevant_nodes(&self, input: &mut StringReader) -> Vec<Arc<RwLock<CommandNode<S>>>> {
let literals = &self.literals;
if literals.is_empty() {
@@ -92,20 +94,20 @@ impl<S> CommandNode<S> {
(self.requirement)(source)
}
- pub fn add_child(&mut self, node: &Rc<RefCell<CommandNode<S>>>) {
- let child = self.children.get(node.borrow().name());
+ pub fn add_child(&mut self, node: &Arc<RwLock<CommandNode<S>>>) {
+ let child = self.children.get(node.read().name());
if let Some(child) = child {
// We've found something to merge onto
- if let Some(command) = &node.borrow().command {
- child.borrow_mut().command = Some(command.clone());
+ if let Some(command) = &node.read().command {
+ child.write().command = Some(command.clone());
}
- for grandchild in node.borrow().children.values() {
- child.borrow_mut().add_child(grandchild);
+ for grandchild in node.read().children.values() {
+ child.write().add_child(grandchild);
}
} else {
self.children
- .insert(node.borrow().name().to_string(), node.clone());
- match &node.borrow().value {
+ .insert(node.read().name().to_string(), node.clone());
+ match &node.read().value {
ArgumentBuilderType::Literal(literal) => {
self.literals.insert(literal.value.clone(), node.clone());
}
@@ -123,7 +125,7 @@ impl<S> CommandNode<S> {
}
}
- pub fn child(&self, name: &str) -> Option<Rc<RefCell<CommandNode<S>>>> {
+ pub fn child(&self, name: &str) -> Option<Arc<RwLock<CommandNode<S>>>> {
self.children.get(name).cloned()
}
@@ -142,7 +144,7 @@ impl<S> CommandNode<S> {
};
context_builder.with_argument(&argument.name, parsed.clone());
- context_builder.with_node(Rc::new(RefCell::new(self.clone())), parsed.range);
+ context_builder.with_node(Arc::new(RwLock::new(self.clone())), parsed.range);
Ok(())
}
@@ -152,7 +154,7 @@ impl<S> CommandNode<S> {
if let Some(end) = end {
context_builder.with_node(
- Rc::new(RefCell::new(self.clone())),
+ Arc::new(RwLock::new(self.clone())),
StringRange::between(start, end),
);
return Ok(());
@@ -232,7 +234,7 @@ impl<S> Hash for CommandNode<S> {
// hash the children
for (k, v) in &self.children {
k.hash(state);
- v.borrow().hash(state);
+ v.read().hash(state);
}
// i hope this works because if doesn't then that'll be a problem
ptr::hash(&self.command, state);
@@ -241,9 +243,16 @@ impl<S> Hash for CommandNode<S> {
impl<S> PartialEq for CommandNode<S> {
fn eq(&self, other: &Self) -> bool {
- if self.children != other.children {
+ if self.children.len() != other.children.len() {
return false;
}
+ for (k, v) in &self.children {
+ let other_child = other.children.get(k).unwrap();
+ if !Arc::ptr_eq(v, other_child) {
+ return false;
+ }
+ }
+
if let Some(selfexecutes) = &self.command {
// idk how to do this better since we can't compare `dyn Fn`s
if let Some(otherexecutes) = &other.command {