Skip to content

Commit

Permalink
Swap Vec<T> for Cow<[T]> in CreateActionRow::Buttons (serenity-…
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Oct 7, 2024
1 parent 29ffaba commit 7f04f87
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/builder/create_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,26 @@ use crate::model::prelude::*;
#[derive(Clone, Debug)]
#[must_use]
pub enum CreateActionRow<'a> {
Buttons(Vec<CreateButton<'a>>),
Buttons(Cow<'a, [CreateButton<'a>]>),
SelectMenu(CreateSelectMenu<'a>),
/// Only valid in modals!
InputText(CreateInputText<'a>),
}

impl<'a> CreateActionRow<'a> {
pub fn buttons(buttons: impl Into<Cow<'a, [CreateButton<'a>]>>) -> Self {
Self::Buttons(buttons.into())
}

pub fn select_menu(select_menu: impl Into<CreateSelectMenu<'a>>) -> Self {
Self::SelectMenu(select_menu.into())
}

pub fn input_text(input_text: impl Into<CreateInputText<'a>>) -> Self {
Self::InputText(input_text.into())
}
}

impl<'a> serde::Serialize for CreateActionRow<'a> {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
use serde::ser::Error as _;
Expand Down
4 changes: 2 additions & 2 deletions src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ macro_rules! button_and_select_menu_convenience_methods {
pub fn button(mut $self, button: super::CreateButton<'a>) -> Self {
let rows = $self$(.$components_path)+.get_or_insert_with(Cow::default).to_mut();
let row_with_space_left = rows.last_mut().and_then(|row| match row {
super::CreateActionRow::Buttons(buttons) if buttons.len() < 5 => Some(buttons),
super::CreateActionRow::Buttons(buttons) if buttons.len() < 5 => Some(buttons.to_mut()),
_ => None,
});
match row_with_space_left {
Some(row) => row.push(button),
None => rows.push(super::CreateActionRow::Buttons(vec![button])),
None => rows.push(super::CreateActionRow::buttons(vec![button])),
}
$self
}
Expand Down

0 comments on commit 7f04f87

Please sign in to comment.