From b45fe30ca5f68f70ba83f3c5be5b1b7640dff682 Mon Sep 17 00:00:00 2001 From: Michael Ripley Date: Mon, 12 Aug 2024 04:29:46 -0500 Subject: [PATCH 1/4] create_application_commands can now take any IntoIterator: not just &[] --- src/builtins/register.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/builtins/register.rs b/src/builtins/register.rs index 8caa9a2f2b1b..0c9b23122461 100644 --- a/src/builtins/register.rs +++ b/src/builtins/register.rs @@ -16,8 +16,8 @@ use crate::serenity_prelude as serenity; /// serenity::Command::set_global_commands(ctx, create_commands).await?; /// # Ok(()) } /// ``` -pub fn create_application_commands( - commands: &[crate::Command], +pub fn create_application_commands<'a, U:'a , E: 'a>( + commands: impl IntoIterator>, ) -> Vec { /// We decided to extract context menu commands recursively, despite the subcommand hierarchy /// not being preserved. Because it's more confusing to just silently discard context menu @@ -35,7 +35,7 @@ pub fn create_application_commands( } } - let mut commands_builder = Vec::with_capacity(commands.len()); + let mut commands_builder = Vec::new(); for command in commands { if let Some(slash_command) = command.create_as_slash_command() { commands_builder.push(slash_command); From 18083985e3e319fe0fa8eadcddb5e98fd8eb5737 Mon Sep 17 00:00:00 2001 From: Michael Ripley Date: Mon, 12 Aug 2024 05:01:00 -0500 Subject: [PATCH 2/4] also update functions that call create_application_commands --- src/builtins/register.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/builtins/register.rs b/src/builtins/register.rs index 0c9b23122461..95ad7bbd4e74 100644 --- a/src/builtins/register.rs +++ b/src/builtins/register.rs @@ -49,9 +49,9 @@ pub fn create_application_commands<'a, U:'a , E: 'a>( /// /// Thin wrapper around [`create_application_commands`] that funnels the returned builder into /// [`serenity::Command::set_global_commands`]. -pub async fn register_globally( +pub async fn register_globally<'a, U: 'a, E: 'a>( http: impl AsRef, - commands: &[crate::Command], + commands: impl IntoIterator>, ) -> Result<(), serenity::Error> { let builder = create_application_commands(commands); serenity::Command::set_global_commands(http, builder).await?; @@ -62,9 +62,9 @@ pub async fn register_globally( /// /// Thin wrapper around [`create_application_commands`] that funnels the returned builder into /// [`serenity::GuildId::set_commands`]. -pub async fn register_in_guild( +pub async fn register_in_guild<'a, U: 'a, E: 'a>( http: impl AsRef, - commands: &[crate::Command], + commands: impl IntoIterator>, guild_id: serenity::GuildId, ) -> Result<(), serenity::Error> { let builder = create_application_commands(commands); From bb3758b458c361cc60474052552913b96cb8bc9c Mon Sep 17 00:00:00 2001 From: Michael Ripley Date: Mon, 12 Aug 2024 05:13:26 -0500 Subject: [PATCH 3/4] Fix rustfmt errors --- src/builtins/register.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/builtins/register.rs b/src/builtins/register.rs index 95ad7bbd4e74..1271b7b1ebec 100644 --- a/src/builtins/register.rs +++ b/src/builtins/register.rs @@ -16,8 +16,8 @@ use crate::serenity_prelude as serenity; /// serenity::Command::set_global_commands(ctx, create_commands).await?; /// # Ok(()) } /// ``` -pub fn create_application_commands<'a, U:'a , E: 'a>( - commands: impl IntoIterator>, +pub fn create_application_commands<'a, U: 'a, E: 'a>( + commands: impl IntoIterator>, ) -> Vec { /// We decided to extract context menu commands recursively, despite the subcommand hierarchy /// not being preserved. Because it's more confusing to just silently discard context menu @@ -51,7 +51,7 @@ pub fn create_application_commands<'a, U:'a , E: 'a>( /// [`serenity::Command::set_global_commands`]. pub async fn register_globally<'a, U: 'a, E: 'a>( http: impl AsRef, - commands: impl IntoIterator>, + commands: impl IntoIterator>, ) -> Result<(), serenity::Error> { let builder = create_application_commands(commands); serenity::Command::set_global_commands(http, builder).await?; @@ -64,7 +64,7 @@ pub async fn register_globally<'a, U: 'a, E: 'a>( /// [`serenity::GuildId::set_commands`]. pub async fn register_in_guild<'a, U: 'a, E: 'a>( http: impl AsRef, - commands: impl IntoIterator>, + commands: impl IntoIterator>, guild_id: serenity::GuildId, ) -> Result<(), serenity::Error> { let builder = create_application_commands(commands); From b9edb995c841f084c5644fb6e6b3565855ef7ae6 Mon Sep 17 00:00:00 2001 From: Michael Ripley Date: Mon, 12 Aug 2024 05:24:40 -0500 Subject: [PATCH 4/4] Restore with_capacity optimization --- src/builtins/register.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/builtins/register.rs b/src/builtins/register.rs index 1271b7b1ebec..dd6e9315b613 100644 --- a/src/builtins/register.rs +++ b/src/builtins/register.rs @@ -35,8 +35,9 @@ pub fn create_application_commands<'a, U: 'a, E: 'a>( } } - let mut commands_builder = Vec::new(); - for command in commands { + let command_iter = commands.into_iter(); + let mut commands_builder = Vec::with_capacity(command_iter.size_hint().0); + for command in command_iter { if let Some(slash_command) = command.create_as_slash_command() { commands_builder.push(slash_command); }