Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create_application_commands can now take any IntoIterator: not just &[] #298

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/builtins/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<U, E>(
commands: &[crate::Command<U, E>],
pub fn create_application_commands<'a, U: 'a, E: 'a>(
commands: impl IntoIterator<Item = &'a crate::Command<U, E>>,
) -> Vec<serenity::CreateCommand> {
/// 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
Expand All @@ -35,8 +35,9 @@ pub fn create_application_commands<U, E>(
}
}

let mut commands_builder = Vec::with_capacity(commands.len());
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);
}
Expand All @@ -49,9 +50,9 @@ pub fn create_application_commands<U, E>(
///
/// Thin wrapper around [`create_application_commands`] that funnels the returned builder into
/// [`serenity::Command::set_global_commands`].
pub async fn register_globally<U, E>(
pub async fn register_globally<'a, U: 'a, E: 'a>(
http: impl AsRef<serenity::Http>,
commands: &[crate::Command<U, E>],
commands: impl IntoIterator<Item = &'a crate::Command<U, E>>,
) -> Result<(), serenity::Error> {
let builder = create_application_commands(commands);
serenity::Command::set_global_commands(http, builder).await?;
Expand All @@ -62,9 +63,9 @@ pub async fn register_globally<U, E>(
///
/// Thin wrapper around [`create_application_commands`] that funnels the returned builder into
/// [`serenity::GuildId::set_commands`].
pub async fn register_in_guild<U, E>(
pub async fn register_in_guild<'a, U: 'a, E: 'a>(
http: impl AsRef<serenity::Http>,
commands: &[crate::Command<U, E>],
commands: impl IntoIterator<Item = &'a crate::Command<U, E>>,
guild_id: serenity::GuildId,
) -> Result<(), serenity::Error> {
let builder = create_application_commands(commands);
Expand Down
Loading