Skip to content

Commit

Permalink
Fix unused warnings when compiling without certain features (#2970)
Browse files Browse the repository at this point in the history
This is mostly just clean up, moving things around and gating things
behind features so that unused warnings in CI go away.
  • Loading branch information
mkrasnitski authored Sep 13, 2024
1 parent fa7de5d commit 6f646f1
Show file tree
Hide file tree
Showing 58 changed files with 121 additions and 209 deletions.
1 change: 1 addition & 0 deletions src/builder/create_attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ impl<'a> EditAttachments<'a> {
/// Clones all new attachments into a new Vec, keeping only data and filename, because those
/// are needed for the multipart form data. The data is taken out of `self` in the process, so
/// this method can only be called once.
#[cfg(feature = "http")]
pub(crate) fn take_files(&mut self) -> Vec<CreateAttachment<'a>> {
let mut files = Vec::new();
for attachment in &mut self.new_and_existing_attachments {
Expand Down
1 change: 1 addition & 0 deletions src/builder/create_command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::collections::HashMap;

#[cfg(feature = "http")]
use crate::http::Http;
Expand Down
1 change: 1 addition & 0 deletions src/builder/create_interaction_response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::collections::HashMap;

use serde_json::json;

Expand Down
2 changes: 2 additions & 0 deletions src/builder/edit_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ impl<'a> EditMessage<'a> {
self
}

#[cfg(feature = "cache")]
fn is_only_suppress_embeds(&self) -> bool {
self.flags == Some(MessageFlags::SUPPRESS_EMBEDS)
&& self.content.is_none()
Expand Down Expand Up @@ -234,6 +235,7 @@ impl<'a> EditMessage<'a> {
/// [Manage Messages]: Permissions::MANAGE_MESSAGES
/// [`From<Embed>`]: CreateEmbed#impl-From<Embed>
#[cfg(feature = "http")]
#[cfg_attr(not(feature = "cache"), allow(unused_variables))]
pub async fn execute(
mut self,
cache_http: impl CacheHttp,
Expand Down
1 change: 0 additions & 1 deletion src/builder/edit_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::borrow::Cow;
use super::CreateAttachment;
#[cfg(feature = "http")]
use crate::http::Http;
use crate::internal::prelude::*;
use crate::model::prelude::*;

/// A builder to create or edit a [`Role`] for use via a number of model methods.
Expand Down
1 change: 1 addition & 0 deletions src/builder/get_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl GetMessages {
}
}

#[cfg_attr(not(feature = "http"), allow(dead_code))]
#[derive(Clone, Copy, Debug)]
enum SearchFilter {
After(MessageId),
Expand Down
7 changes: 2 additions & 5 deletions src/cache/event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::{HashSet, VecDeque};
use std::num::NonZeroU16;

use super::{Cache, CacheUpdate};
use crate::internal::prelude::*;
Expand Down Expand Up @@ -32,9 +31,8 @@ use crate::model::event::{
VoiceChannelStatusUpdateEvent,
VoiceStateUpdateEvent,
};
use crate::model::gateway::{Presence, ShardInfo};
use crate::model::gateway::Presence;
use crate::model::guild::{Guild, GuildMemberFlags, Member, MemberGeneratedFlags, Role};
use crate::model::id::ShardId;
use crate::model::user::{CurrentUser, OnlineStatus};
use crate::model::voice::VoiceState;

Expand Down Expand Up @@ -449,8 +447,7 @@ impl CacheUpdate for ReadyEvent {
let mut guilds_to_remove = vec![];
let ready_guilds_hashset =
self.ready.guilds.iter().map(|status| status.id).collect::<HashSet<_>>();
let shard_data =
self.ready.shard.unwrap_or_else(|| ShardInfo::new(ShardId(1), NonZeroU16::MIN));
let shard_data = self.ready.shard.unwrap_or_default();

for guild_entry in cache.guilds.iter() {
let guild = guild_entry.key();
Expand Down
10 changes: 8 additions & 2 deletions src/gateway/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use std::sync::OnceLock;
use futures::channel::mpsc::UnboundedReceiver as Receiver;
use futures::future::BoxFuture;
use futures::StreamExt as _;
use tracing::debug;
use tracing::{debug, warn};

pub use self::context::Context;
pub use self::event_handler::{EventHandler, FullEvent, RawEventHandler};
Expand All @@ -55,7 +55,6 @@ use crate::model::gateway::GatewayIntents;
#[cfg(feature = "voice")]
use crate::model::id::UserId;
use crate::model::user::OnlineStatus;
use crate::utils::check_shard_total;

/// A builder implementing [`IntoFuture`] building a [`Client`] to interact with Discord.
#[must_use = "Builders do nothing unless they are awaited"]
Expand Down Expand Up @@ -762,3 +761,10 @@ impl Client {
Ok(())
}
}

fn check_shard_total(total_shards: u16) -> NonZeroU16 {
NonZeroU16::new(total_shards).unwrap_or_else(|| {
warn!("Invalid shard total provided ({total_shards}), defaulting to 1");
NonZeroU16::MIN
})
}
1 change: 1 addition & 0 deletions src/gateway/sharding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use std::fmt;
use std::sync::Arc;
use std::time::{Duration as StdDuration, Instant};

use aformat::{aformat, CapStr};
use secrecy::{ExposeSecret as _, Secret};
use tokio_tungstenite::tungstenite::error::Error as TungsteniteError;
use tokio_tungstenite::tungstenite::protocol::frame::CloseFrame;
Expand Down
6 changes: 5 additions & 1 deletion src/gateway/sharding/shard_queuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,14 @@ impl ShardQueuer {

#[cfg_attr(feature = "tracing_instrument", instrument(skip(self)))]
async fn start(&mut self, shard_id: ShardId) -> Result<()> {
let shard_info = ShardInfo {
id: shard_id,
total: self.shard_total,
};
let mut shard = Shard::new(
Arc::clone(&self.ws_url),
Arc::clone(self.http.token()),
ShardInfo::new(shard_id, self.shard_total),
shard_info,
self.intents,
self.presence.clone(),
)
Expand Down
8 changes: 3 additions & 5 deletions src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::cell::Cell;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

use aformat::{aformat, CapStr};
use arrayvec::ArrayVec;
use nonmax::{NonMaxU16, NonMaxU8};
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
Expand Down Expand Up @@ -42,10 +43,6 @@ impl Token {
pub fn new(inner: Arc<str>) -> Secret<Self> {
Secret::new(Self(inner))
}

pub fn get_inner(&self) -> &Arc<str> {
&self.0
}
}

impl std::ops::Deref for Token {
Expand Down Expand Up @@ -297,8 +294,9 @@ impl Http {
self.application_id.store(application_id.get(), Ordering::Relaxed);
}

#[cfg(feature = "gateway")]
pub(crate) fn token(&self) -> &Arc<str> {
self.token.expose_secret().get_inner()
&self.token.expose_secret().0
}

/// Adds a [`User`] to a [`Guild`] with a valid OAuth2 access token.
Expand Down
7 changes: 4 additions & 3 deletions src/internal/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

pub use std::result::Result as StdResult;

pub use aformat::{aformat, aformat_into, ArrayString, CapStr};
pub use extract_map::{ExtractKey, ExtractMap, LendingIterator};
pub use serde_json::Value;
pub use small_fixed_array::{FixedArray, FixedString, TruncatingInto};
pub use to_arraystring::ToArrayString;

pub(crate) use super::utils::join_to_string;
pub use crate::error::{Error, Result};
pub use super::utils::join_to_string;
#[cfg(feature = "http")]
pub use crate::error::Error;
pub use crate::error::Result;

pub type JsonMap = serde_json::Map<String, Value>;
2 changes: 1 addition & 1 deletion src/internal/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Write;

pub(crate) fn join_to_string(
pub fn join_to_string(
sep: impl std::fmt::Display,
iter: impl IntoIterator<Item = impl std::fmt::Display>,
) -> String {
Expand Down
13 changes: 1 addition & 12 deletions src/model/application/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,7 @@ use super::{InstallationContext, InteractionContext};
use crate::builder::CreateCommand;
#[cfg(feature = "model")]
use crate::http::Http;
use crate::internal::prelude::*;
use crate::model::channel::ChannelType;
use crate::model::id::{
ApplicationId,
CommandId,
CommandPermissionId,
CommandVersionId,
GuildId,
RoleId,
UserId,
};
use crate::model::Permissions;
use crate::model::prelude::*;

/// The base command model that belongs to an application.
///
Expand Down
21 changes: 1 addition & 20 deletions src/model/application/command_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,8 @@ use crate::builder::{
use crate::gateway::client::Context;
#[cfg(feature = "model")]
use crate::http::Http;
use crate::internal::prelude::*;
use crate::internal::utils::lending_for_each;
use crate::model::application::{CommandOptionType, CommandType};
use crate::model::channel::{Attachment, Message, PartialChannel};
use crate::model::guild::{Member, PartialMember, Role};
use crate::model::id::{
ApplicationId,
AttachmentId,
ChannelId,
CommandId,
GenericId,
GuildId,
InteractionId,
MessageId,
RoleId,
TargetId,
UserId,
};
use crate::model::monetization::Entitlement;
use crate::model::user::User;
use crate::model::Permissions;
use crate::model::prelude::*;
#[cfg(all(feature = "collector", feature = "utils"))]
use crate::utils::{CreateQuickModal, QuickModalResponse};

Expand Down
1 change: 0 additions & 1 deletion src/model/application/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use serde::de::Error as DeError;
use serde::ser::{Serialize, Serializer};
use serde_json::from_value;

use crate::internal::prelude::*;
use crate::model::prelude::*;
use crate::model::utils::{default_true, deserialize_val};

Expand Down
1 change: 0 additions & 1 deletion src/model/application/component_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::builder::{
use crate::gateway::client::Context;
#[cfg(feature = "model")]
use crate::http::Http;
use crate::internal::prelude::*;
use crate::model::prelude::*;
#[cfg(all(feature = "collector", feature = "utils"))]
use crate::utils::{CreateQuickModal, QuickModalResponse};
Expand Down
6 changes: 1 addition & 5 deletions src/model/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ pub use oauth::*;
mod ping_interaction;
pub use ping_interaction::*;

use super::id::{ApplicationId, GenericId, GuildId, SkuId, UserId};
use super::misc::ImageHash;
use super::user::User;
use super::Permissions;
use crate::internal::prelude::*;
use super::prelude::*;

/// Partial information about the given application.
///
Expand Down
1 change: 0 additions & 1 deletion src/model/application/modal_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::builder::{
};
#[cfg(feature = "model")]
use crate::http::Http;
use crate::internal::prelude::*;
use crate::model::prelude::*;

/// An interaction triggered by a modal submit.
Expand Down
3 changes: 1 addition & 2 deletions src/model/application/ping_interaction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use serde::{Deserialize, Serialize};

use crate::internal::prelude::*;
use crate::model::id::{ApplicationId, InteractionId};
use crate::model::prelude::*;

/// A ping interaction, which can only be received through an endpoint url.
///
Expand Down
1 change: 0 additions & 1 deletion src/model/channel/attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use nonmax::NonMaxU32;
use reqwest::Client as ReqwestClient;
use serde_cow::CowStr;

use crate::internal::prelude::*;
use crate::model::prelude::*;
use crate::model::utils::is_false;

Expand Down
3 changes: 2 additions & 1 deletion src/model/channel/channel_id.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "model")]
use std::borrow::Cow;
#[cfg(feature = "model")]
use std::sync::Arc;
Expand Down Expand Up @@ -28,7 +29,6 @@ use crate::collector::{MessageCollector, ReactionCollector};
use crate::gateway::ShardMessenger;
#[cfg(feature = "model")]
use crate::http::{CacheHttp, Http, Typing};
use crate::internal::prelude::*;
use crate::model::prelude::*;

#[cfg(feature = "model")]
Expand Down Expand Up @@ -365,6 +365,7 @@ impl ChannelId {
/// # Errors
///
/// Returns [`Error::Http`] if the channel retrieval request failed.
#[cfg_attr(not(feature = "cache"), allow(unused_variables))]
pub async fn to_channel(
self,
cache_http: impl CacheHttp,
Expand Down
3 changes: 1 addition & 2 deletions src/model/channel/embed.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use nonmax::NonMaxU32;

use crate::internal::prelude::*;
use crate::model::{Colour, Timestamp};
use crate::model::prelude::*;

/// Represents a rich embed which allows using richer markdown, multiple fields and more. This was
/// heavily inspired by [slack's attachments].
Expand Down
2 changes: 1 addition & 1 deletion src/model/channel/guild_channel.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "model")]
use std::borrow::Cow;
use std::fmt;
#[cfg(feature = "model")]
Expand Down Expand Up @@ -29,7 +30,6 @@ use crate::collector::{MessageCollector, ReactionCollector};
use crate::gateway::ShardMessenger;
#[cfg(feature = "model")]
use crate::http::{CacheHttp, Http, Typing};
use crate::internal::prelude::*;
use crate::model::prelude::*;

/// Represents a guild's text, news, or voice channel. Some methods are available only for voice
Expand Down
2 changes: 1 addition & 1 deletion src/model/channel/message.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Models relating to Discord channels.

#[cfg(feature = "model")]
use std::borrow::Cow;

use nonmax::NonMaxU64;
Expand All @@ -20,7 +21,6 @@ use crate::constants;
use crate::gateway::ShardMessenger;
#[cfg(feature = "model")]
use crate::http::{CacheHttp, Http};
use crate::internal::prelude::*;
use crate::model::prelude::*;
use crate::model::utils::{discord_colours, StrOrInt};

Expand Down
2 changes: 1 addition & 1 deletion src/model/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use serde::de::{Error as DeError, Unexpected};
use serde_json::from_value;

pub use self::attachment::*;
#[cfg(feature = "model")]
pub use self::channel_id::*;
pub use self::embed::*;
pub use self::guild_channel::*;
Expand All @@ -24,7 +25,6 @@ pub use self::private_channel::*;
pub use self::reaction::*;
#[cfg(feature = "model")]
use crate::http::Http;
use crate::internal::prelude::*;
use crate::model::prelude::*;
use crate::model::utils::is_false;

Expand Down
5 changes: 1 addition & 4 deletions src/model/channel/partial_channel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::internal::prelude::*;
use crate::model::channel::{ChannelType, ThreadMetadata};
use crate::model::id::{ChannelId, WebhookId};
use crate::model::Permissions;
use crate::model::prelude::*;

/// A container for any partial channel.
///
Expand Down
2 changes: 1 addition & 1 deletion src/model/channel/private_channel.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "model")]
use std::borrow::Cow;
use std::fmt;
#[cfg(feature = "model")]
Expand All @@ -9,7 +10,6 @@ use crate::builder::{CreateAttachment, CreateMessage, EditMessage, GetMessages};
use crate::http::CacheHttp;
#[cfg(feature = "model")]
use crate::http::{Http, Typing};
use crate::internal::prelude::*;
use crate::model::prelude::*;
use crate::model::utils::single_recipient;

Expand Down
Loading

0 comments on commit 6f646f1

Please sign in to comment.