Skip to content

Commit

Permalink
Remove async from methods that can just be synchronous (serenity-rs…
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev authored and mkrasnitski committed May 30, 2023
1 parent 878786b commit 9b471ec
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 43 deletions.
12 changes: 5 additions & 7 deletions examples/e13_parallel_loops/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ impl EventHandler for Handler {
// the application.
tokio::spawn(async move {
loop {
// We clone Context again here, because Arc is owned, so it moves to the
// new function.
log_system_load(Arc::clone(&ctx1)).await;
log_system_load(&ctx1).await;
tokio::time::sleep(Duration::from_secs(120)).await;
}
});
Expand All @@ -63,7 +61,7 @@ impl EventHandler for Handler {
let ctx2 = Arc::clone(&ctx);
tokio::spawn(async move {
loop {
set_activity_to_current_time(Arc::clone(&ctx2)).await;
set_activity_to_current_time(&ctx2);
tokio::time::sleep(Duration::from_secs(60)).await;
}
});
Expand All @@ -74,7 +72,7 @@ impl EventHandler for Handler {
}
}

async fn log_system_load(ctx: Arc<Context>) {
async fn log_system_load(ctx: &Context) {
let cpu_load = sys_info::loadavg().unwrap();
let mem_use = sys_info::mem_info().unwrap();

Expand All @@ -99,11 +97,11 @@ async fn log_system_load(ctx: Arc<Context>) {
};
}

async fn set_activity_to_current_time(ctx: Arc<Context>) {
fn set_activity_to_current_time(ctx: &Context) {
let current_time = Utc::now();
let formatted_time = current_time.to_rfc2822();

ctx.set_activity(Some(ActivityData::playing(formatted_time))).await;
ctx.set_activity(Some(ActivityData::playing(formatted_time)));
}

#[tokio::main]
Expand Down
42 changes: 16 additions & 26 deletions src/client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Context {
/// impl EventHandler for Handler {
/// async fn message(&self, ctx: Context, msg: Message) {
/// if msg.content == "!online" {
/// ctx.online().await;
/// ctx.online();
/// }
/// }
/// }
Expand All @@ -123,9 +123,8 @@ impl Context {
///
/// [`Online`]: OnlineStatus::Online
#[cfg(feature = "gateway")]
#[allow(clippy::unused_async)]
#[inline]
pub async fn online(&self) {
pub fn online(&self) {
self.shard.set_status(OnlineStatus::Online);
}

Expand All @@ -146,7 +145,7 @@ impl Context {
/// impl EventHandler for Handler {
/// async fn message(&self, ctx: Context, msg: Message) {
/// if msg.content == "!idle" {
/// ctx.idle().await;
/// ctx.idle();
/// }
/// }
/// }
Expand All @@ -162,9 +161,8 @@ impl Context {
///
/// [`Idle`]: OnlineStatus::Idle
#[cfg(feature = "gateway")]
#[allow(clippy::unused_async)]
#[inline]
pub async fn idle(&self) {
pub fn idle(&self) {
self.shard.set_status(OnlineStatus::Idle);
}

Expand All @@ -185,7 +183,7 @@ impl Context {
/// impl EventHandler for Handler {
/// async fn message(&self, ctx: Context, msg: Message) {
/// if msg.content == "!dnd" {
/// ctx.dnd().await;
/// ctx.dnd();
/// }
/// }
/// }
Expand All @@ -201,9 +199,8 @@ impl Context {
///
/// [`DoNotDisturb`]: OnlineStatus::DoNotDisturb
#[cfg(feature = "gateway")]
#[allow(clippy::unused_async)]
#[inline]
pub async fn dnd(&self) {
pub fn dnd(&self) {
self.shard.set_status(OnlineStatus::DoNotDisturb);
}

Expand All @@ -224,7 +221,7 @@ impl Context {
/// #[serenity::async_trait]
/// impl EventHandler for Handler {
/// async fn ready(&self, ctx: Context, _: Ready) {
/// ctx.invisible().await;
/// ctx.invisible();
/// }
/// }
///
Expand All @@ -240,9 +237,8 @@ impl Context {
/// [`Event::Ready`]: crate::model::event::Event::Ready
/// [`Invisible`]: OnlineStatus::Invisible
#[cfg(feature = "gateway")]
#[allow(clippy::unused_async)]
#[inline]
pub async fn invisible(&self) {
pub fn invisible(&self) {
self.shard.set_status(OnlineStatus::Invisible);
}

Expand All @@ -264,7 +260,7 @@ impl Context {
/// #[serenity::async_trait]
/// impl EventHandler for Handler {
/// async fn resume(&self, ctx: Context, _: ResumedEvent) {
/// ctx.reset_presence().await;
/// ctx.reset_presence();
/// }
/// }
///
Expand All @@ -280,9 +276,8 @@ impl Context {
/// [`Event::Resumed`]: crate::model::event::Event::Resumed
/// [`Online`]: OnlineStatus::Online
#[cfg(feature = "gateway")]
#[allow(clippy::unused_async)]
#[inline]
pub async fn reset_presence(&self) {
pub fn reset_presence(&self) {
self.shard.set_presence(None, OnlineStatus::Online);
}

Expand All @@ -307,7 +302,7 @@ impl Context {
/// let mut args = msg.content.splitn(2, ' ');
///
/// if let (Some("~setgame"), Some(game)) = (args.next(), args.next()) {
/// ctx.set_activity(Some(ActivityData::playing(game))).await;
/// ctx.set_activity(Some(ActivityData::playing(game)));
/// }
/// }
/// }
Expand All @@ -321,9 +316,8 @@ impl Context {
/// # }
/// ```
#[cfg(feature = "gateway")]
#[allow(clippy::unused_async)]
#[inline]
pub async fn set_activity(&self, activity: Option<ActivityData>) {
pub fn set_activity(&self, activity: Option<ActivityData>) {
self.shard.set_activity(activity);
}

Expand Down Expand Up @@ -391,36 +385,32 @@ impl Context {
/// [`DoNotDisturb`]: OnlineStatus::DoNotDisturb
/// [`Idle`]: OnlineStatus::Idle
#[cfg(feature = "gateway")]
#[allow(clippy::unused_async)]
#[inline]
pub async fn set_presence(&self, activity: Option<ActivityData>, status: OnlineStatus) {
pub fn set_presence(&self, activity: Option<ActivityData>, status: OnlineStatus) {
self.shard.set_presence(activity, status);
}

/// Sets a new `filter` for the shard to check if a message event shall be
/// sent back to `filter`'s paired receiver.
#[cfg(feature = "collector")]
#[allow(clippy::unused_async)]
#[inline]
pub async fn set_message_filter(&self, filter: MessageFilter) {
pub fn set_message_filter(&self, filter: MessageFilter) {
self.shard.set_message_filter(filter);
}

/// Sets a new `filter` for the shard to check if a reaction event shall be
/// sent back to `filter`'s paired receiver.
#[cfg(feature = "collector")]
#[allow(clippy::unused_async)]
#[inline]
pub async fn set_reaction_filter(&self, filter: ReactionFilter) {
pub fn set_reaction_filter(&self, filter: ReactionFilter) {
self.shard.set_reaction_filter(filter);
}

/// Sets a new `filter` for the shard to check if an interaction event shall be
/// sent back to `filter`'s paired receiver.
#[cfg(feature = "collector")]
#[allow(clippy::unused_async)]
#[inline]
pub async fn set_component_interaction_filter(&self, filter: ComponentInteractionFilter) {
pub fn set_component_interaction_filter(&self, filter: ComponentInteractionFilter) {
self.shard.set_component_interaction_filter(filter);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/model/channel/channel_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,7 @@ impl ChannelId {

/// Returns the name of whatever channel this id holds.
#[cfg(feature = "cache")]
#[allow(clippy::unused_async)]
pub async fn name(self, cache: impl AsRef<Cache>) -> Option<String> {
pub fn name(self, cache: impl AsRef<Cache>) -> Option<String> {
let channel = self.to_channel_cached(cache)?;

Some(match channel {
Expand Down
4 changes: 2 additions & 2 deletions src/model/guild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ impl Guild {
/// Returns the "default" channel of the guild for the passed user id.
/// (This returns the first channel that can be read by the user, if there isn't one,
/// returns [`None`])
#[allow(clippy::unused_async)]
pub async fn default_channel(&self, uid: UserId) -> Option<&GuildChannel> {
#[must_use]
pub fn default_channel(&self, uid: UserId) -> Option<&GuildChannel> {
let member = self.members.get(&uid)?;
for channel in self.channels.values() {
if let Channel::Guild(channel) = channel {
Expand Down
7 changes: 3 additions & 4 deletions src/model/guild/partial_guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,18 +960,17 @@ impl PartialGuild {
/// [`position`]: Role::position
#[cfg(feature = "cache")]
#[inline]
pub async fn greater_member_hierarchy(
pub fn greater_member_hierarchy(
&self,
cache: impl AsRef<Cache>,
lhs_id: impl Into<UserId>,
rhs_id: impl Into<UserId>,
) -> Option<UserId> {
self._greater_member_hierarchy(&cache, lhs_id.into(), rhs_id.into()).await
self._greater_member_hierarchy(&cache, lhs_id.into(), rhs_id.into())
}

#[cfg(feature = "cache")]
#[allow(clippy::unused_async)]
async fn _greater_member_hierarchy(
fn _greater_member_hierarchy(
&self,
cache: impl AsRef<Cache>,
lhs_id: UserId,
Expand Down
3 changes: 1 addition & 2 deletions src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,9 +1073,8 @@ impl UserId {

/// Attempts to find a [`User`] by its Id in the cache.
#[cfg(feature = "cache")]
#[allow(clippy::unused_async)]
#[inline]
pub async fn to_user_cached(self, cache: &impl AsRef<Cache>) -> Option<UserRef<'_>> {
pub fn to_user_cached(self, cache: &impl AsRef<Cache>) -> Option<UserRef<'_>> {
cache.as_ref().user(self)
}

Expand Down

0 comments on commit 9b471ec

Please sign in to comment.