Skip to content

Commit 6764370

Browse files
committed
Swap Vec<T> for Cow<[T]> in CreateActionRow::Buttons (#2985)
Closes #2984.
1 parent 3b9ee7e commit 6764370

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/builder/create_components.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,26 @@ use crate::model::prelude::*;
1111
#[derive(Clone, Debug)]
1212
#[must_use]
1313
pub enum CreateActionRow<'a> {
14-
Buttons(Vec<CreateButton<'a>>),
14+
Buttons(Cow<'a, [CreateButton<'a>]>),
1515
SelectMenu(CreateSelectMenu<'a>),
1616
/// Only valid in modals!
1717
InputText(CreateInputText<'a>),
1818
}
1919

20+
impl<'a> CreateActionRow<'a> {
21+
pub fn buttons(buttons: impl Into<Cow<'a, [CreateButton<'a>]>>) -> Self {
22+
Self::Buttons(buttons.into())
23+
}
24+
25+
pub fn select_menu(select_menu: impl Into<CreateSelectMenu<'a>>) -> Self {
26+
Self::SelectMenu(select_menu.into())
27+
}
28+
29+
pub fn input_text(input_text: impl Into<CreateInputText<'a>>) -> Self {
30+
Self::InputText(input_text.into())
31+
}
32+
}
33+
2034
impl<'a> serde::Serialize for CreateActionRow<'a> {
2135
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2236
use serde::ser::Error as _;

src/builder/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ macro_rules! button_and_select_menu_convenience_methods {
128128
pub fn button(mut $self, button: super::CreateButton<'a>) -> Self {
129129
let rows = $self$(.$components_path)+.get_or_insert_with(Cow::default).to_mut();
130130
let row_with_space_left = rows.last_mut().and_then(|row| match row {
131-
super::CreateActionRow::Buttons(buttons) if buttons.len() < 5 => Some(buttons),
131+
super::CreateActionRow::Buttons(buttons) if buttons.len() < 5 => Some(buttons.to_mut()),
132132
_ => None,
133133
});
134134
match row_with_space_left {
135135
Some(row) => row.push(button),
136-
None => rows.push(super::CreateActionRow::Buttons(vec![button])),
136+
None => rows.push(super::CreateActionRow::buttons(vec![button])),
137137
}
138138
$self
139139
}

0 commit comments

Comments
 (0)