1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

remove more unnecessary brigadier cloning!

This commit is contained in:
mat 2023-05-05 19:37:40 +00:00
parent df167a5a39
commit e4176937f0
3 changed files with 20 additions and 38 deletions

View file

@ -10,12 +10,12 @@ use std::{cell::RefCell, cmp::Ordering, collections::HashMap, marker::PhantomDat
/// The root of the command tree. You need to make this to register commands.
#[derive(Default)]
pub struct CommandDispatcher<S> {
pub struct CommandDispatcher<'a, S> {
pub root: Rc<RefCell<CommandNode<S>>>,
_marker: PhantomData<S>,
_marker: PhantomData<(S, &'a ())>,
}
impl<S> CommandDispatcher<S> {
impl<'a, S> CommandDispatcher<'a, S> {
pub fn new() -> Self {
Self {
root: Rc::new(RefCell::new(CommandNode::default())),
@ -30,21 +30,16 @@ impl<S> CommandDispatcher<S> {
}
pub fn parse(&self, command: StringReader, source: Rc<S>) -> ParseResults<S> {
let context = CommandContextBuilder::new(
Rc::new(self.clone()),
source,
self.root.clone(),
command.cursor(),
);
let context = CommandContextBuilder::new(self, source, self.root.clone(), command.cursor());
self.parse_nodes(&self.root, &command, context).unwrap()
}
fn parse_nodes(
&self,
&'a self,
node: &Rc<RefCell<CommandNode<S>>>,
original_reader: &StringReader,
context_so_far: CommandContextBuilder<S>,
) -> Result<ParseResults<S>, CommandSyntaxException> {
context_so_far: CommandContextBuilder<'a, S>,
) -> Result<ParseResults<'a, S>, CommandSyntaxException> {
let source = context_so_far.source.clone();
let mut errors = HashMap::<Rc<CommandNode<S>>, CommandSyntaxException>::new();
let mut potentials: Vec<ParseResults<S>> = vec![];
@ -91,12 +86,8 @@ impl<S> CommandDispatcher<S> {
}) {
reader.skip();
if let Some(redirect) = &child.borrow().redirect {
let child_context = CommandContextBuilder::new(
Rc::new(self.clone()),
source,
redirect.clone(),
reader.cursor,
);
let child_context =
CommandContextBuilder::new(self, source, redirect.clone(), reader.cursor);
let parse = self
.parse_nodes(redirect, &reader, child_context)
.expect("Parsing nodes failed");
@ -287,12 +278,3 @@ impl<S> CommandDispatcher<S> {
// Ok(if forked { successful_forks } else { result })
}
}
impl<S> Clone for CommandDispatcher<S> {
fn clone(&self) -> Self {
Self {
root: self.root.clone(),
_marker: PhantomData,
}
}
}

View file

@ -9,20 +9,20 @@ use crate::{
};
use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
pub struct CommandContextBuilder<S> {
pub struct CommandContextBuilder<'a, S> {
pub arguments: HashMap<String, ParsedArgument>,
pub root: Rc<RefCell<CommandNode<S>>>,
pub nodes: Vec<ParsedCommandNode<S>>,
pub dispatcher: Rc<CommandDispatcher<S>>,
pub dispatcher: &'a CommandDispatcher<'a, S>,
pub source: Rc<S>,
pub command: Command<S>,
pub child: Option<Rc<CommandContextBuilder<S>>>,
pub child: Option<Rc<CommandContextBuilder<'a, S>>>,
pub range: StringRange,
pub modifier: Option<Rc<RedirectModifier<S>>>,
pub forks: bool,
}
impl<S> Clone for CommandContextBuilder<S> {
impl<S> Clone for CommandContextBuilder<'_, S> {
fn clone(&self) -> Self {
Self {
arguments: self.arguments.clone(),
@ -39,9 +39,9 @@ impl<S> Clone for CommandContextBuilder<S> {
}
}
impl<S> CommandContextBuilder<S> {
impl<'a, S> CommandContextBuilder<'a, S> {
pub fn new(
dispatcher: Rc<CommandDispatcher<S>>,
dispatcher: &'a CommandDispatcher<S>,
source: Rc<S>,
root_node: Rc<RefCell<CommandNode<S>>>,
start: usize,
@ -64,7 +64,7 @@ impl<S> CommandContextBuilder<S> {
self.command = command.clone();
self
}
pub fn with_child(&mut self, child: Rc<CommandContextBuilder<S>>) -> &Self {
pub fn with_child(&mut self, child: Rc<CommandContextBuilder<'a, S>>) -> &Self {
self.child = Some(child);
self
}
@ -99,7 +99,7 @@ impl<S> CommandContextBuilder<S> {
}
}
impl<S> Debug for CommandContextBuilder<S> {
impl<S> Debug for CommandContextBuilder<'_, S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CommandContextBuilder")
// .field("arguments", &self.arguments)

View file

@ -4,13 +4,13 @@ use crate::{
};
use std::{collections::HashMap, fmt::Debug, rc::Rc};
pub struct ParseResults<S> {
pub context: CommandContextBuilder<S>,
pub struct ParseResults<'a, S> {
pub context: CommandContextBuilder<'a, S>,
pub reader: StringReader,
pub exceptions: HashMap<Rc<CommandNode<S>>, CommandSyntaxException>,
}
impl<S> Debug for ParseResults<S> {
impl<S> Debug for ParseResults<'_, S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ParseResults")
.field("context", &self.context)