From e4176937f0584a6bcc5aba15abb1e2df6ddf588d Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 5 May 2023 19:37:40 +0000 Subject: remove more unnecessary brigadier cloning! --- azalea-brigadier/src/command_dispatcher.rs | 36 ++++++---------------- .../src/context/command_context_builder.rs | 16 +++++----- azalea-brigadier/src/parse_results.rs | 6 ++-- 3 files changed, 20 insertions(+), 38 deletions(-) diff --git a/azalea-brigadier/src/command_dispatcher.rs b/azalea-brigadier/src/command_dispatcher.rs index ba41223e..29fadfd1 100755 --- a/azalea-brigadier/src/command_dispatcher.rs +++ b/azalea-brigadier/src/command_dispatcher.rs @@ -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 { +pub struct CommandDispatcher<'a, S> { pub root: Rc>>, - _marker: PhantomData, + _marker: PhantomData<(S, &'a ())>, } -impl CommandDispatcher { +impl<'a, S> CommandDispatcher<'a, S> { pub fn new() -> Self { Self { root: Rc::new(RefCell::new(CommandNode::default())), @@ -30,21 +30,16 @@ impl CommandDispatcher { } pub fn parse(&self, command: StringReader, source: Rc) -> ParseResults { - 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>>, original_reader: &StringReader, - context_so_far: CommandContextBuilder, - ) -> Result, CommandSyntaxException> { + context_so_far: CommandContextBuilder<'a, S>, + ) -> Result, CommandSyntaxException> { let source = context_so_far.source.clone(); let mut errors = HashMap::>, CommandSyntaxException>::new(); let mut potentials: Vec> = vec![]; @@ -91,12 +86,8 @@ impl CommandDispatcher { }) { 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 CommandDispatcher { // Ok(if forked { successful_forks } else { result }) } } - -impl Clone for CommandDispatcher { - fn clone(&self) -> Self { - Self { - root: self.root.clone(), - _marker: PhantomData, - } - } -} diff --git a/azalea-brigadier/src/context/command_context_builder.rs b/azalea-brigadier/src/context/command_context_builder.rs index 7516ab9e..b0677158 100755 --- a/azalea-brigadier/src/context/command_context_builder.rs +++ b/azalea-brigadier/src/context/command_context_builder.rs @@ -9,20 +9,20 @@ use crate::{ }; use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc}; -pub struct CommandContextBuilder { +pub struct CommandContextBuilder<'a, S> { pub arguments: HashMap, pub root: Rc>>, pub nodes: Vec>, - pub dispatcher: Rc>, + pub dispatcher: &'a CommandDispatcher<'a, S>, pub source: Rc, pub command: Command, - pub child: Option>>, + pub child: Option>>, pub range: StringRange, pub modifier: Option>>, pub forks: bool, } -impl Clone for CommandContextBuilder { +impl Clone for CommandContextBuilder<'_, S> { fn clone(&self) -> Self { Self { arguments: self.arguments.clone(), @@ -39,9 +39,9 @@ impl Clone for CommandContextBuilder { } } -impl CommandContextBuilder { +impl<'a, S> CommandContextBuilder<'a, S> { pub fn new( - dispatcher: Rc>, + dispatcher: &'a CommandDispatcher, source: Rc, root_node: Rc>>, start: usize, @@ -64,7 +64,7 @@ impl CommandContextBuilder { self.command = command.clone(); self } - pub fn with_child(&mut self, child: Rc>) -> &Self { + pub fn with_child(&mut self, child: Rc>) -> &Self { self.child = Some(child); self } @@ -99,7 +99,7 @@ impl CommandContextBuilder { } } -impl Debug for CommandContextBuilder { +impl Debug for CommandContextBuilder<'_, S> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("CommandContextBuilder") // .field("arguments", &self.arguments) diff --git a/azalea-brigadier/src/parse_results.rs b/azalea-brigadier/src/parse_results.rs index 3698ae82..aa7d79ea 100755 --- a/azalea-brigadier/src/parse_results.rs +++ b/azalea-brigadier/src/parse_results.rs @@ -4,13 +4,13 @@ use crate::{ }; use std::{collections::HashMap, fmt::Debug, rc::Rc}; -pub struct ParseResults { - pub context: CommandContextBuilder, +pub struct ParseResults<'a, S> { + pub context: CommandContextBuilder<'a, S>, pub reader: StringReader, pub exceptions: HashMap>, CommandSyntaxException>, } -impl Debug for ParseResults { +impl Debug for ParseResults<'_, S> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("ParseResults") .field("context", &self.context) -- cgit v1.2.3