Skip to content

Commit

Permalink
Remove unnecessary lifetimes in CommandDispatcher and Command (#419)
Browse files Browse the repository at this point in the history
* make command dispatcher use owned strings and not take any lifetimes

* remove get_argument_consumer

* fix clippy

* fix elided lifetimes
  • Loading branch information
urisinger authored Dec 26, 2024
1 parent 29ed502 commit 25f4834
Show file tree
Hide file tree
Showing 45 changed files with 429 additions and 476 deletions.
6 changes: 3 additions & 3 deletions pumpkin-protocol/src/client/play/c_command_suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl ClientPacket for CCommandSuggestions<'_> {
bytebuf.put_var_int(&self.length);

bytebuf.put_list(&self.matches, |bytebuf, suggestion| {
bytebuf.put_string(suggestion.suggestion);
bytebuf.put_string(&suggestion.suggestion);
bytebuf.put_bool(suggestion.tooltip.is_some());
if let Some(tooltip) = &suggestion.tooltip {
bytebuf.put_slice(&tooltip.encode());
Expand All @@ -46,12 +46,12 @@ impl ClientPacket for CCommandSuggestions<'_> {

#[derive(PartialEq, Eq, Hash, Debug)]
pub struct CommandSuggestion<'a> {
pub suggestion: &'a str,
pub suggestion: String,
pub tooltip: Option<TextComponent<'a>>,
}

impl<'a> CommandSuggestion<'a> {
pub fn new(suggestion: &'a str, tooltip: Option<TextComponent<'a>>) -> Self {
pub fn new(suggestion: String, tooltip: Option<TextComponent<'a>>) -> Self {
Self {
suggestion,
tooltip,
Expand Down
14 changes: 5 additions & 9 deletions pumpkin/src/command/args/arg_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl GetClientSideArgParser for BlockArgumentConsumer {
#[async_trait]
impl ArgumentConsumer for BlockArgumentConsumer {
async fn consume<'a>(
&self,
&'a self,
_sender: &CommandSender<'a>,
_server: &'a Server,
args: &mut RawArgs<'a>,
Expand All @@ -41,7 +41,7 @@ impl ArgumentConsumer for BlockArgumentConsumer {
}

async fn suggest<'a>(
&self,
&'a self,
_sender: &CommandSender<'a>,
_server: &'a Server,
_input: &'a str,
Expand All @@ -51,19 +51,15 @@ impl ArgumentConsumer for BlockArgumentConsumer {
}

impl DefaultNameArgConsumer for BlockArgumentConsumer {
fn default_name(&self) -> &'static str {
"block"
}

fn get_argument_consumer(&self) -> &dyn ArgumentConsumer {
self
fn default_name(&self) -> String {
"block".to_string()
}
}

impl<'a> FindArg<'a> for BlockArgumentConsumer {
type Data = &'a Block;

fn find_arg(args: &'a super::ConsumedArgs, name: &'a str) -> Result<Self::Data, CommandError> {
fn find_arg(args: &'a super::ConsumedArgs, name: &str) -> Result<Self::Data, CommandError> {
match args.get(name) {
Some(Arg::Block(name)) => block_registry::get_block(name).map_or_else(
|| {
Expand Down
6 changes: 3 additions & 3 deletions pumpkin/src/command/args/arg_bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl GetClientSideArgParser for BoolArgConsumer {
#[async_trait]
impl ArgumentConsumer for BoolArgConsumer {
async fn consume<'a>(
&self,
&'a self,
_sender: &CommandSender<'a>,
_server: &'a Server,
args: &mut RawArgs<'a>,
Expand All @@ -38,7 +38,7 @@ impl ArgumentConsumer for BoolArgConsumer {
}

async fn suggest<'a>(
&self,
&'a self,
_sender: &CommandSender<'a>,
_server: &'a Server,
_input: &'a str,
Expand All @@ -50,7 +50,7 @@ impl ArgumentConsumer for BoolArgConsumer {
impl<'a> FindArg<'a> for BoolArgConsumer {
type Data = bool;

fn find_arg(args: &'a super::ConsumedArgs, name: &'a str) -> Result<Self::Data, CommandError> {
fn find_arg(args: &'a super::ConsumedArgs, name: &str) -> Result<Self::Data, CommandError> {
match args.get(name) {
Some(Arg::Bool(data)) => Ok(*data),
_ => Err(CommandError::InvalidConsumption(Some(name.to_string()))),
Expand Down
16 changes: 6 additions & 10 deletions pumpkin/src/command/args/arg_bossbar_color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl GetClientSideArgParser for BossbarColorArgumentConsumer {
#[async_trait]
impl ArgumentConsumer for BossbarColorArgumentConsumer {
async fn consume<'a>(
&self,
&'a self,
_sender: &CommandSender<'a>,
_server: &'a Server,
args: &mut RawArgs<'a>,
Expand All @@ -49,34 +49,30 @@ impl ArgumentConsumer for BossbarColorArgumentConsumer {
}

async fn suggest<'a>(
&self,
&'a self,
_sender: &CommandSender<'a>,
_server: &'a Server,
_input: &'a str,
) -> Result<Option<Vec<CommandSuggestion<'a>>>, CommandError> {
let colors = ["blue", "green", "pink", "purple", "red", "white", "yellow"];
let suggestions: Vec<CommandSuggestion> = colors
.iter()
.map(|color| CommandSuggestion::new(color, None))
.map(|color| CommandSuggestion::new((*color).to_string(), None))
.collect();
Ok(Some(suggestions))
}
}

impl DefaultNameArgConsumer for BossbarColorArgumentConsumer {
fn default_name(&self) -> &'static str {
"color"
}

fn get_argument_consumer(&self) -> &dyn ArgumentConsumer {
self
fn default_name(&self) -> String {
"color".to_string()
}
}

impl<'a> FindArg<'a> for BossbarColorArgumentConsumer {
type Data = &'a BossbarColor;

fn find_arg(args: &'a super::ConsumedArgs, name: &'a str) -> Result<Self::Data, CommandError> {
fn find_arg(args: &'a super::ConsumedArgs, name: &str) -> Result<Self::Data, CommandError> {
match args.get(name) {
Some(Arg::BossbarColor(data)) => Ok(data),
_ => Err(CommandError::InvalidConsumption(Some(name.to_string()))),
Expand Down
16 changes: 6 additions & 10 deletions pumpkin/src/command/args/arg_bossbar_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl GetClientSideArgParser for BossbarStyleArgumentConsumer {
#[async_trait]
impl ArgumentConsumer for BossbarStyleArgumentConsumer {
async fn consume<'a>(
&self,
&'a self,
_sender: &CommandSender<'a>,
_server: &'a Server,
args: &mut RawArgs<'a>,
Expand All @@ -47,7 +47,7 @@ impl ArgumentConsumer for BossbarStyleArgumentConsumer {
}

async fn suggest<'a>(
&self,
&'a self,
_sender: &CommandSender<'a>,
_server: &'a Server,
_input: &'a str,
Expand All @@ -61,26 +61,22 @@ impl ArgumentConsumer for BossbarStyleArgumentConsumer {
];
let suggestions: Vec<CommandSuggestion> = styles
.iter()
.map(|style| CommandSuggestion::new(style, None))
.map(|style| CommandSuggestion::new((*style).to_string(), None))
.collect();
Ok(Some(suggestions))
}
}

impl DefaultNameArgConsumer for BossbarStyleArgumentConsumer {
fn default_name(&self) -> &'static str {
"style"
}

fn get_argument_consumer(&self) -> &dyn ArgumentConsumer {
self
fn default_name(&self) -> String {
"style".to_string()
}
}

impl<'a> FindArg<'a> for BossbarStyleArgumentConsumer {
type Data = &'a BossbarDivisions;

fn find_arg(args: &'a super::ConsumedArgs, name: &'a str) -> Result<Self::Data, CommandError> {
fn find_arg(args: &'a super::ConsumedArgs, name: &str) -> Result<Self::Data, CommandError> {
match args.get(name) {
Some(Arg::BossbarStyle(data)) => Ok(data),
_ => Err(CommandError::InvalidConsumption(Some(name.to_string()))),
Expand Down
12 changes: 4 additions & 8 deletions pumpkin/src/command/args/arg_bounded_num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where
Self: GetClientSideArgParser,
{
async fn consume<'a>(
&self,
&'a self,
_src: &CommandSender<'a>,
_server: &'a Server,
args: &mut RawArgs<'a>,
Expand All @@ -48,7 +48,7 @@ where
}

async fn suggest<'a>(
&self,
&'a self,
_sender: &CommandSender<'a>,
_server: &'a Server,
_input: &'a str,
Expand Down Expand Up @@ -238,12 +238,8 @@ impl<T: ToFromNumber> DefaultNameArgConsumer for BoundedNumArgumentConsumer<T>
where
Self: ArgumentConsumer,
{
fn default_name(&self) -> &'static str {
fn default_name(&self) -> String {
// setting a single default name for all BoundedNumArgumentConsumer variants is probably a bad idea since it would lead to confusion
self.name.expect("Only use *_default variants of methods with a BoundedNumArgumentConsumer that has a name.")
}

fn get_argument_consumer(&self) -> &dyn ArgumentConsumer {
self
self.name.expect("Only use *_default variants of methods with a BoundedNumArgumentConsumer that has a name.").to_string()
}
}
20 changes: 8 additions & 12 deletions pumpkin/src/command/args/arg_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl GetClientSideArgParser for CommandTreeArgumentConsumer {
#[async_trait]
impl ArgumentConsumer for CommandTreeArgumentConsumer {
async fn consume<'a>(
&self,
&'a self,
_sender: &CommandSender<'a>,
server: &'a Server,
args: &mut RawArgs<'a>,
Expand All @@ -40,11 +40,11 @@ impl ArgumentConsumer for CommandTreeArgumentConsumer {
let dispatcher = server.command_dispatcher.read().await;
dispatcher
.get_tree(s)
.map_or_else(|_| None, |tree| Some(Arg::CommandTree(tree)))
.map_or_else(|_| None, |tree| Some(Arg::CommandTree(tree.clone())))
}

async fn suggest<'a>(
&self,
&'a self,
_sender: &CommandSender<'a>,
server: &'a Server,
input: &'a str,
Expand All @@ -58,26 +58,22 @@ impl ArgumentConsumer for CommandTreeArgumentConsumer {
.commands
.keys()
.filter(|suggestion| suggestion.starts_with(input))
.map(|suggestion| CommandSuggestion::new(suggestion, None))
.map(|suggestion| CommandSuggestion::new(suggestion.to_string(), None))
.collect();
Ok(Some(suggestions))
}
}

impl DefaultNameArgConsumer for CommandTreeArgumentConsumer {
fn default_name(&self) -> &'static str {
"cmd"
}

fn get_argument_consumer(&self) -> &dyn ArgumentConsumer {
&Self
fn default_name(&self) -> String {
"cmd".to_string()
}
}

impl<'a> FindArg<'a> for CommandTreeArgumentConsumer {
type Data = &'a CommandTree<'a>;
type Data = &'a CommandTree;

fn find_arg(args: &'a super::ConsumedArgs, name: &'a str) -> Result<Self::Data, CommandError> {
fn find_arg(args: &'a super::ConsumedArgs, name: &str) -> Result<Self::Data, CommandError> {
match args.get(name) {
Some(Arg::CommandTree(tree)) => Ok(tree),
_ => Err(CommandError::InvalidConsumption(Some(name.to_string()))),
Expand Down
14 changes: 5 additions & 9 deletions pumpkin/src/command/args/arg_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl GetClientSideArgParser for EntitiesArgumentConsumer {
#[async_trait]
impl ArgumentConsumer for EntitiesArgumentConsumer {
async fn consume<'a>(
&self,
&'a self,
src: &CommandSender<'a>,
server: &'a Server,
args: &mut RawArgs<'a>,
Expand All @@ -47,7 +47,7 @@ impl ArgumentConsumer for EntitiesArgumentConsumer {
}

async fn suggest<'a>(
&self,
&'a self,
_sender: &CommandSender<'a>,
_server: &'a Server,
_input: &'a str,
Expand All @@ -57,19 +57,15 @@ impl ArgumentConsumer for EntitiesArgumentConsumer {
}

impl DefaultNameArgConsumer for EntitiesArgumentConsumer {
fn default_name(&self) -> &'static str {
"targets"
}

fn get_argument_consumer(&self) -> &dyn ArgumentConsumer {
&Self
fn default_name(&self) -> String {
"targets".to_string()
}
}

impl<'a> FindArg<'a> for EntitiesArgumentConsumer {
type Data = &'a [Arc<Player>];

fn find_arg(args: &'a super::ConsumedArgs, name: &'a str) -> Result<Self::Data, CommandError> {
fn find_arg(args: &'a super::ConsumedArgs, name: &str) -> Result<Self::Data, CommandError> {
match args.get(name) {
Some(Arg::Entities(data)) => Ok(data),
_ => Err(CommandError::InvalidConsumption(Some(name.to_string()))),
Expand Down
14 changes: 5 additions & 9 deletions pumpkin/src/command/args/arg_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl GetClientSideArgParser for EntityArgumentConsumer {
#[async_trait]
impl ArgumentConsumer for EntityArgumentConsumer {
async fn consume<'a>(
&self,
&'a self,
src: &CommandSender<'a>,
server: &'a Server,
args: &mut RawArgs<'a>,
Expand Down Expand Up @@ -69,7 +69,7 @@ impl ArgumentConsumer for EntityArgumentConsumer {
}

async fn suggest<'a>(
&self,
&'a self,
_sender: &CommandSender<'a>,
_server: &'a Server,
_input: &'a str,
Expand All @@ -79,19 +79,15 @@ impl ArgumentConsumer for EntityArgumentConsumer {
}

impl DefaultNameArgConsumer for EntityArgumentConsumer {
fn default_name(&self) -> &'static str {
"target"
}

fn get_argument_consumer(&self) -> &dyn ArgumentConsumer {
&Self
fn default_name(&self) -> String {
"target".to_string()
}
}

impl<'a> FindArg<'a> for EntityArgumentConsumer {
type Data = Arc<Player>;

fn find_arg(args: &'a super::ConsumedArgs, name: &'a str) -> Result<Self::Data, CommandError> {
fn find_arg(args: &'a super::ConsumedArgs, name: &str) -> Result<Self::Data, CommandError> {
match args.get(name) {
Some(Arg::Entity(data)) => Ok(data.clone()),
_ => Err(CommandError::InvalidConsumption(Some(name.to_string()))),
Expand Down
Loading

0 comments on commit 25f4834

Please sign in to comment.