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 Oct 19, 2024
1 parent 6c26be9 commit 79578ec
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 28 deletions.
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
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ pub mod samples {
}

#[doc(hidden)]
pub use {async_trait::async_trait, futures_util};
pub use 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
15 changes: 8 additions & 7 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 @@ -169,7 +171,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 +188,18 @@ 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>> {
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>> {
execute_modal(ctx, Some(defaults), None)
}
}
6 changes: 0 additions & 6 deletions src/prefix_argument/argument_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ macro_rules! pop_prefix_argument {
/// 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 {
/// Parse [`Self`] from the front of the given string and return a tuple of the remaining string
/// and [`Self`]. If parsing failed, an error is returned and, if applicable, the string on
Expand All @@ -43,7 +42,6 @@ pub trait PopArgument<'a>: Sized {
}

#[doc(hidden)]
#[async_trait::async_trait]
pub trait PopArgumentHack<'a, T>: Sized {
async fn pop_from(
self,
Expand All @@ -54,7 +52,6 @@ pub trait PopArgumentHack<'a, T>: Sized {
) -> Result<(&'a str, usize, T), (Box<dyn std::error::Error + Send + Sync>, Option<String>)>;
}

#[async_trait::async_trait]
impl<'a, T: serenity::ArgumentConvert + Send> PopArgumentHack<'a, T> for PhantomData<T>
where
T::Err: std::error::Error + Send + Sync + 'static,
Expand All @@ -77,7 +74,6 @@ where
}
}

#[async_trait::async_trait]
impl<'a, T: PopArgument<'a> + Send + Sync> PopArgumentHack<'a, T> for &PhantomData<T> {
async fn pop_from(
self,
Expand All @@ -91,7 +87,6 @@ impl<'a, T: PopArgument<'a> + Send + Sync> PopArgumentHack<'a, T> for &PhantomDa
}
}

#[async_trait::async_trait]
impl<'a> PopArgumentHack<'a, bool> for &PhantomData<bool> {
async fn pop_from(
self,
Expand All @@ -114,7 +109,6 @@ impl<'a> PopArgumentHack<'a, bool> for &PhantomData<bool> {
}
}

#[async_trait::async_trait]
impl<'a> PopArgumentHack<'a, serenity::Attachment> for &PhantomData<serenity::Attachment> {
async fn pop_from(
self,
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
15 changes: 5 additions & 10 deletions src/slash_argument/slash_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
use super::SlashArgError;
use std::convert::TryInto as _;
use std::future::Future;
use std::marker::PhantomData;

#[allow(unused_imports)] // import is required if serenity simdjson feature is enabled
use crate::serenity::json::*;
use crate::serenity_prelude as serenity;

/// 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::json::Value`].
///
/// Don't call this method directly! Use [`crate::extract_slash_argument!`]
async fn extract(
fn extract(
ctx: &serenity::Context,
interaction: &serenity::CommandInteraction,
value: &serenity::ResolvedValue<'_>,
) -> Result<Self, SlashArgError>;
) -> impl Future<Output = Result<Self, SlashArgError>>;

/// Create a slash command parameter equivalent to type T.
///
Expand All @@ -42,14 +42,13 @@ pub trait SlashArgument: Sized {
/// Currently marked `#[doc(hidden)]` because implementing this trait requires some jank due to a
/// `PhantomData` hack and the auto-deref specialization hack.
#[doc(hidden)]
#[async_trait::async_trait]
pub trait SlashArgumentHack<T>: Sized {
async fn extract(
fn extract(
self,
ctx: &serenity::Context,
interaction: &serenity::CommandInteraction,
value: &serenity::ResolvedValue<'_>,
) -> Result<T, SlashArgError>;
) -> impl Future<Output = Result<T, SlashArgError>>;

fn create(self, builder: serenity::CreateCommandOption) -> serenity::CreateCommandOption;

Expand Down Expand Up @@ -90,7 +89,6 @@ macro_rules! slash_argument_choices {
}

/// Handles arbitrary types that can be parsed from string.
#[async_trait::async_trait]
impl<T> SlashArgumentHack<T> for PhantomData<T>
where
T: serenity::ArgumentConvert + Send + Sync,
Expand Down Expand Up @@ -132,7 +130,6 @@ where
/// 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 @@ -162,7 +159,6 @@ macro_rules! impl_for_integer {
}
impl_for_integer!(i8 i16 i32 i64 isize u8 u16 u32 u64 usize);

#[async_trait::async_trait]
impl<T: SlashArgument + Sync> SlashArgumentHack<T> for &PhantomData<T> {
async fn extract(
self,
Expand All @@ -185,7 +181,6 @@ impl<T: SlashArgument + Sync> SlashArgumentHack<T> for &PhantomData<T> {
/// Versatile macro to implement `SlashArgumentHack` 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 79578ec

Please sign in to comment.