Skip to content

Commit

Permalink
Swap Vec<T> for Cow<[T]> in CreateActionRow::Buttons (#2985)
Browse files Browse the repository at this point in the history
Closes #2984.
  • Loading branch information
GnomedDev authored Oct 7, 2024
1 parent c1b69fe commit c88004c
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 @@ -126,12 +126,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 c88004c

Please sign in to comment.