Skip to content

Commit

Permalink
Swap to native async traits where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Nov 22, 2024
1 parent cce8cc6 commit 3ddef59
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 35 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ repository = "https://github.com/serenity-rs/poise/"
tokio = { version = "1.25.1", default-features = false } # for async in general
futures-util = { version = "0.3.13", default-features = false } # for async in general
poise_macros = { path = "macros", version = "0.6.1" } # remember to update the version on changes!
async-trait = { version = "0.1.48", default-features = false } # various traits
regex = { version = "1.6.0", default-features = false, features = ["std"] } # prefix
tracing = { version = "0.1.40", features = ["log"] } # warning about weird state
derivative = "2.2.0"
Expand Down
2 changes: 0 additions & 2 deletions src/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::{
/// This is useful if you need to take an argument via a string, but immediately convert it via [`FromStr`].
pub struct StrArg<T>(pub T);

#[async_trait::async_trait]
impl<T> SlashArgument for StrArg<T>
where
T: FromStr,
Expand Down Expand Up @@ -40,7 +39,6 @@ where
}
}

#[async_trait::async_trait]
impl<'a, T> PopArgument<'a> for StrArg<T>
where
T: FromStr,
Expand Down
2 changes: 0 additions & 2 deletions src/choice_parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub trait ChoiceParameter: Sized {
fn localized_name(&self, locale: &str) -> Option<&'static str>;
}

#[async_trait::async_trait]
impl<T: ChoiceParameter> crate::SlashArgument for T {
async fn extract(
_: &serenity::Context,
Expand Down Expand Up @@ -55,7 +54,6 @@ impl<T: ChoiceParameter> crate::SlashArgument for T {
}
}

#[async_trait::async_trait]
impl<'a, T: ChoiceParameter> crate::PopArgument<'a> for T {
async fn pop_from(
args: &'a str,
Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,6 @@ pub mod samples {
pub use crate::builtins::*;
}

#[doc(hidden)]
pub use {async_trait::async_trait, futures_util};

/// This module re-exports a bunch of items from all over serenity. Useful if you can't
/// remember the full paths of serenity items.
///
Expand Down
26 changes: 15 additions & 11 deletions src/modal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Modal trait and utility items for implementing it (mainly for the derive macro)
use std::future::Future;

use crate::serenity_prelude as serenity;

/// Meant for use in derived [`Modal::parse`] implementation
Expand Down Expand Up @@ -40,10 +42,7 @@ pub fn find_modal_text(

/// Underlying code for the modal spawning convenience function which abstracts over the kind of
/// interaction
async fn execute_modal_generic<
M: Modal,
F: std::future::Future<Output = Result<(), serenity::Error>>,
>(
async fn execute_modal_generic<M: Modal, F: Future<Output = Result<(), serenity::Error>>>(
ctx: &serenity::Context,
create_interaction_response: impl FnOnce(serenity::CreateInteractionResponse) -> F,
modal_custom_id: String,
Expand Down Expand Up @@ -169,7 +168,6 @@ pub async fn execute_modal_on_component_interaction<M: Modal>(
/// Ok(())
/// }
/// ```
#[async_trait::async_trait]
pub trait Modal: Sized {
/// Returns an interaction response builder which creates the modal for this type
///
Expand All @@ -187,18 +185,24 @@ pub trait Modal: Sized {
///
/// For a variant that is triggered on component interactions, see [`execute_modal_on_component_interaction`].
// TODO: add execute_with_defaults? Or add a `defaults: Option<Self>` param?
async fn execute<U: Send + Sync, E>(
fn execute<U: Send + Sync, E>(
ctx: crate::ApplicationContext<'_, U, E>,
) -> Result<Option<Self>, serenity::Error> {
execute_modal(ctx, None::<Self>, None).await
) -> impl Future<Output = Result<Option<Self>, serenity::Error>> + Send
where
Self: Send,
{
execute_modal(ctx, None::<Self>, None)
}

/// Calls `execute_modal(ctx, Some(defaults), None)`. See [`execute_modal`]
// TODO: deprecate this in favor of execute_modal()?
async fn execute_with_defaults<U: Send + Sync, E>(
fn execute_with_defaults<U: Send + Sync, E>(
ctx: crate::ApplicationContext<'_, U, E>,
defaults: Self,
) -> Result<Option<Self>, serenity::Error> {
execute_modal(ctx, Some(defaults), None).await
) -> impl Future<Output = Result<Option<Self>, serenity::Error>> + Send
where
Self: Send,
{
execute_modal(ctx, Some(defaults), None)
}
}
9 changes: 2 additions & 7 deletions src/prefix_argument/argument_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@ pub(crate) type PopArgumentResult<'a, T> =
/// does. This is for consistency's sake and also because it keeps open the possibility of parsing whitespace.
///
/// Similar in spirit to [`std::str::FromStr`].
#[async_trait::async_trait]
pub trait PopArgument<'a>: Sized {
/// Pops an argument from the `args` string.
///
/// See the documentation of [`PopArgumentResult`] for the return type.
async fn pop_from(
fn pop_from(
args: &'a str,
attachment_index: usize,
ctx: &serenity::Context,
msg: &serenity::Message,
) -> PopArgumentResult<'a, Self>;
) -> impl std::future::Future<Output = PopArgumentResult<'a, Self>>;
}

#[async_trait::async_trait]
impl<'a> PopArgument<'a> for String {
async fn pop_from(
args: &'a str,
Expand All @@ -47,7 +45,6 @@ impl<'a> PopArgument<'a> for String {
}
}

#[async_trait::async_trait]
impl<'a> PopArgument<'a> for bool {
async fn pop_from(
args: &'a str,
Expand All @@ -68,7 +65,6 @@ impl<'a> PopArgument<'a> for bool {
}
}

#[async_trait::async_trait]
impl<'a> PopArgument<'a> for serenity::Attachment {
async fn pop_from(
args: &'a str,
Expand Down Expand Up @@ -108,7 +104,6 @@ where
/// Implements PopArgument for many types via `[pop_from_via_argumentconvert`].
macro_rules! impl_popargument_via_argumentconvert {
($($type:ty),*) => {$(
#[async_trait::async_trait]
impl<'a> PopArgument<'a> for $type {
async fn pop_from(
args: &'a str,
Expand Down
1 change: 0 additions & 1 deletion src/prefix_argument/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ fn pop_from(args: &str) -> Result<(&str, CodeBlock), CodeBlockError> {
}
}

#[async_trait::async_trait]
impl<'a> PopArgument<'a> for CodeBlock {
/// Parse a single-line or multi-line code block. The output of `Self::code` should mirror what
/// the official Discord client renders, and the output of `Self::language` should mirror the
Expand Down
1 change: 0 additions & 1 deletion src/prefix_argument/key_value_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ impl KeyValueArgs {
}
}

#[async_trait::async_trait]
impl<'a> PopArgument<'a> for KeyValueArgs {
async fn pop_from(
args: &'a str,
Expand Down
8 changes: 2 additions & 6 deletions src/slash_argument/slash_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ use crate::serenity::json::*;
use crate::{serenity_prelude as serenity, CowVec};

/// Implement this trait on types that you want to use as a slash command parameter.
#[async_trait::async_trait]
pub trait SlashArgument: Sized {
/// Extract a Rust value of type T from the slash command argument, given via a [`serenity::ResolvedValue`].
async fn extract(
fn extract(
ctx: &serenity::Context,
interaction: &serenity::CommandInteraction,
value: &serenity::ResolvedValue<'_>,
) -> Result<Self, SlashArgError>;
) -> impl std::future::Future<Output = Result<Self, SlashArgError>>;

/// Create a slash command parameter equivalent to type T.
///
Expand Down Expand Up @@ -64,7 +63,6 @@ where
/// Implements `SlashArgument` via `serenity::ArgumentConvert`
macro_rules! impl_for_argumentconvert {
($type:ty) => {
#[async_trait::async_trait]
impl SlashArgument for $type {
async fn extract(
ctx: &serenity::Context,
Expand All @@ -86,7 +84,6 @@ impl_for_argumentconvert!(serenity::Message);
/// Implements slash argument trait for integer types
macro_rules! impl_for_integer {
($($t:ty)*) => { $(
#[async_trait::async_trait]
impl SlashArgument for $t {
async fn extract(
_: &serenity::Context,
Expand Down Expand Up @@ -120,7 +117,6 @@ impl_for_integer!(i8 i16 i32 i64 isize u8 u16 u32 u64 usize);
/// Versatile macro to implement `SlashArgument` for simple types
macro_rules! impl_slash_argument {
($type:ty, |$ctx:pat, $interaction:pat, $slash_param_type:ident ( $($arg:pat),* )| $extractor:expr) => {
#[async_trait::async_trait]
impl SlashArgument for $type {
async fn extract(
$ctx: &serenity::Context,
Expand Down

0 comments on commit 3ddef59

Please sign in to comment.