|
1 | | -use std::{borrow::Cow, fmt::Write}; |
| 1 | +use std::{borrow::Cow, fmt::Write, ops::ControlFlow}; |
2 | 2 |
|
| 3 | +use arrayvec::ArrayVec; |
3 | 4 | use indexmap::IndexMap; |
4 | 5 |
|
5 | 6 | use self::serenity::CreateEmbed; |
@@ -67,35 +68,41 @@ pub async fn autocomplete<'a>( |
67 | 68 | searching: &'a str, |
68 | 69 | ) -> serenity::CreateAutocompleteResponse<'a> { |
69 | 70 | fn flatten_commands<'a>( |
70 | | - result: &mut Vec<serenity::AutocompleteChoice<'a>>, |
| 71 | + result: &mut ArrayVec<serenity::AutocompleteChoice<'a>, 25>, |
71 | 72 | commands: &'a [Command], |
72 | 73 | searching: &str, |
73 | | - ) { |
| 74 | + ) -> ControlFlow<()> { |
74 | 75 | for command in commands { |
75 | 76 | if command.owners_only || command.hide_in_help { |
76 | 77 | continue; |
77 | 78 | } |
78 | 79 |
|
79 | 80 | if command.subcommands.is_empty() { |
80 | 81 | if command.qualified_name.starts_with(searching) { |
81 | | - result.push(serenity::AutocompleteChoice::new( |
| 82 | + let choice = serenity::AutocompleteChoice::new( |
82 | 83 | command.qualified_name.as_ref(), |
83 | 84 | command.qualified_name.as_ref(), |
84 | | - )); |
| 85 | + ); |
| 86 | + |
| 87 | + if result.try_push(choice).is_err() { |
| 88 | + return ControlFlow::Break(()); |
| 89 | + }; |
85 | 90 | } |
86 | 91 | } else { |
87 | | - flatten_commands(result, &command.subcommands, searching); |
| 92 | + flatten_commands(result, &command.subcommands, searching)?; |
88 | 93 | } |
89 | 94 | } |
| 95 | + |
| 96 | + ControlFlow::Continue(()) |
90 | 97 | } |
91 | 98 |
|
92 | 99 | let commands = &ctx.framework.options().commands; |
93 | | - let mut result = Vec::with_capacity(commands.len()); |
| 100 | + let mut result = ArrayVec::<_, 25>::new(); |
94 | 101 |
|
95 | 102 | flatten_commands(&mut result, commands, searching); |
96 | 103 |
|
97 | 104 | result.sort_by_cached_key(|a| strsim::levenshtein(&a.name, searching)); |
98 | | - serenity::CreateAutocompleteResponse::new().set_choices(result) |
| 105 | + serenity::CreateAutocompleteResponse::new().set_choices(result.into_iter().collect::<Vec<_>>()) |
99 | 106 | } |
100 | 107 |
|
101 | 108 | /// Shows TTS Bot's commands and descriptions of them |
|
0 commit comments