Change add_control on menu builder to pass the function instead of the Arc

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/9/head
trivernis 3 years ago
parent bcd1a96c1b
commit cbdb2e3265
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -21,26 +21,33 @@ pub static NEXT_PAGE_EMOJI: &str = "➡️";
pub static PREVIOUS_PAGE_EMOJI: &str = "⬅️"; pub static PREVIOUS_PAGE_EMOJI: &str = "⬅️";
pub static CLOSE_MENU_EMOJI: &str = "❌"; pub static CLOSE_MENU_EMOJI: &str = "❌";
pub type ControlAction = Arc< pub type ControlActionResult<'b> =
dyn for<'b> Fn( Pin<Box<dyn Future<Output = SerenityUtilsResult<()>> + Send + 'b>>;
&'b Context,
&'b mut Menu<'_>, pub type ControlActionArc = Arc<
Reaction, dyn for<'b> Fn(&'b Context, &'b mut Menu<'_>, Reaction) -> ControlActionResult<'b>
) -> Pin<Box<dyn Future<Output = SerenityUtilsResult<()>> + Send + 'b>>
+ Send + Send
+ Sync, + Sync,
>; >;
#[derive(Clone)] #[derive(Clone)]
pub struct ActionContainer { pub struct ActionContainer {
inner: ControlAction, inner: ControlActionArc,
position: usize, position: usize,
} }
impl ActionContainer { impl ActionContainer {
/// Creates a new control action /// Creates a new control action
pub fn new(position: usize, inner: ControlAction) -> Self { pub fn new<F: 'static>(position: usize, callback: F) -> Self
Self { inner, position } where
F: for<'b> Fn(&'b Context, &'b mut Menu<'_>, Reaction) -> ControlActionResult<'b>
+ Send
+ Sync,
{
Self {
inner: Arc::new(callback),
position,
}
} }
/// Runs the action /// Runs the action
@ -158,15 +165,15 @@ impl MenuBuilder {
let mut controls = HashMap::new(); let mut controls = HashMap::new();
controls.insert( controls.insert(
PREVIOUS_PAGE_EMOJI.to_string(), PREVIOUS_PAGE_EMOJI.to_string(),
ActionContainer::new(0, Arc::new(|c, m, r| previous_page(c, m, r).boxed())), ActionContainer::new(0, |c, m, r| previous_page(c, m, r).boxed()),
); );
controls.insert( controls.insert(
CLOSE_MENU_EMOJI.to_string(), CLOSE_MENU_EMOJI.to_string(),
ActionContainer::new(1, Arc::new(|c, m, r| close_menu(c, m, r).boxed())), ActionContainer::new(1, |c, m, r| close_menu(c, m, r).boxed()),
); );
controls.insert( controls.insert(
NEXT_PAGE_EMOJI.to_string(), NEXT_PAGE_EMOJI.to_string(),
ActionContainer::new(2, Arc::new(|c, m, r| next_page(c, m, r).boxed())), ActionContainer::new(2, |c, m, r| next_page(c, m, r).boxed()),
); );
Self { Self {
@ -194,12 +201,13 @@ impl MenuBuilder {
} }
/// Adds a single control to the message /// Adds a single control to the message
pub fn add_control<S: ToString>( pub fn add_control<S, F: 'static>(mut self, position: usize, emoji: S, action: F) -> Self
mut self, where
position: usize, S: ToString,
emoji: S, F: for<'b> Fn(&'b Context, &'b mut Menu<'_>, Reaction) -> ControlActionResult<'b>
action: ControlAction, + Send
) -> Self { + Sync,
{
self.controls self.controls
.insert(emoji.to_string(), ActionContainer::new(position, action)); .insert(emoji.to_string(), ActionContainer::new(position, action));
@ -210,11 +218,16 @@ impl MenuBuilder {
pub fn add_controls<S, I>(mut self, controls: I) -> Self pub fn add_controls<S, I>(mut self, controls: I) -> Self
where where
S: ToString, S: ToString,
I: IntoIterator<Item = (usize, S, ControlAction)>, I: IntoIterator<Item = (usize, S, ControlActionArc)>,
{ {
for (position, emoji, action) in controls { for (position, emoji, action) in controls {
self.controls self.controls.insert(
.insert(emoji.to_string(), ActionContainer::new(position, action)); emoji.to_string(),
ActionContainer {
position,
inner: action,
},
);
} }
self self

@ -6,7 +6,7 @@ pub(crate) mod traits;
pub use container::*; pub use container::*;
pub use controls::*; pub use controls::*;
pub use menu::{ pub use menu::{
ActionContainer, ControlAction, Menu, MenuBuilder, CLOSE_MENU_EMOJI, NEXT_PAGE_EMOJI, ActionContainer, ControlActionArc, Menu, MenuBuilder, CLOSE_MENU_EMOJI, NEXT_PAGE_EMOJI,
PREVIOUS_PAGE_EMOJI, PREVIOUS_PAGE_EMOJI,
}; };
pub use traits::EventDrivenMessage; pub use traits::EventDrivenMessage;

Loading…
Cancel
Save