Skip to content

Commit

Permalink
Remove Into<*Id> and AsRef<str> (serenity-rs#2704)
Browse files Browse the repository at this point in the history
`Into<*Id>` is often entirely unnecessary, and at worst hides the type of
the Id with hard-coded Ids. This fixes this by just using the Id type
itself, which also helps with monomorphisation bloat.

I also removed `AsRef<str>`, as this can just be `&str` without
requiring a generic and without requiring a call to as_ref in the
function, while also not accidentally taking ownership of a `String`
passed in misleadingly.
  • Loading branch information
GnomedDev committed Mar 19, 2024
1 parent b403f67 commit 17aaf89
Show file tree
Hide file tree
Showing 42 changed files with 312 additions and 524 deletions.
2 changes: 1 addition & 1 deletion examples/e05_command_framework/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ async fn say(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
// We do not want to clean channel mentions as they do not ping users.
.clean_channel(false);

x = content_safe(&guild, x, settings, &msg.mentions);
x = content_safe(&guild, &x, settings, &msg.mentions);
}

msg.channel_id.say(&ctx.http, x).await?;
Expand Down
2 changes: 1 addition & 1 deletion examples/e11_gateway_intents/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async fn main() {
let intents =
GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT;
// Build our client.
let mut client = Client::builder(token, intents)
let mut client = Client::builder(&token, intents)
.event_handler(Handler)
.await
.expect("Error creating client");
Expand Down
2 changes: 1 addition & 1 deletion examples/e14_slash_commands/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async fn main() {
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");

// Build our client.
let mut client = Client::builder(token, GatewayIntents::empty())
let mut client = Client::builder(&token, GatewayIntents::empty())
.event_handler(Handler)
.await
.expect("Error creating client");
Expand Down
2 changes: 1 addition & 1 deletion examples/e15_simple_dashboard/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let mut client = Client::builder(token, intents)
let mut client = Client::builder(&token, intents)
.event_handler(Handler)
.framework(framework)
.type_map_insert::<RillRateComponents>(components)
Expand Down
2 changes: 1 addition & 1 deletion examples/e17_message_components/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ async fn main() {
let intents = GatewayIntents::GUILD_MESSAGES
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;
let mut client = Client::builder(token, intents)
let mut client = Client::builder(&token, intents)
.event_handler(Handler)
.await
.expect("Error creating client");
Expand Down
2 changes: 1 addition & 1 deletion examples/testing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,5 +428,5 @@ async fn main() -> Result<(), serenity::Error> {
env_logger::init();
let token = std::env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let intents = GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT;
Client::builder(token, intents).event_handler(Handler).await?.start().await
Client::builder(&token, intents).event_handler(Handler).await?.start().await
}
8 changes: 4 additions & 4 deletions src/builder/bot_auth_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ impl<'a> CreateBotAuthParameters<'a> {
}

/// Specify the client Id of your application.
pub fn client_id(mut self, client_id: impl Into<ApplicationId>) -> Self {
self.client_id = Some(client_id.into());
pub fn client_id(mut self, client_id: ApplicationId) -> Self {
self.client_id = Some(client_id);
self
}

Expand Down Expand Up @@ -106,8 +106,8 @@ impl<'a> CreateBotAuthParameters<'a> {
}

/// Specify the Id of the guild to prefill the dropdown picker for the user.
pub fn guild_id(mut self, guild_id: impl Into<GuildId>) -> Self {
self.guild_id = Some(guild_id.into());
pub fn guild_id(mut self, guild_id: GuildId) -> Self {
self.guild_id = Some(guild_id);
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/create_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ impl<'a> CreateChannel<'a> {
/// Only for [`ChannelType::Text`], [`ChannelType::Voice`], [`ChannelType::News`],
/// [`ChannelType::Stage`], [`ChannelType::Forum`]
#[doc(alias = "parent_id")]
pub fn category(mut self, id: impl Into<ChannelId>) -> Self {
self.parent_id = Some(id.into());
pub fn category(mut self, id: ChannelId) -> Self {
self.parent_id = Some(id);
self
}

Expand Down
15 changes: 6 additions & 9 deletions src/builder/create_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ impl<'a> CreateMessage<'a> {
///
/// **Note**: This will replace all existing stickers. Use [`Self::add_sticker_id()`] to keep
/// existing stickers.
pub fn sticker_id(self, sticker_id: impl Into<StickerId>) -> Self {
self.sticker_ids(vec![sticker_id.into()])
pub fn sticker_id(self, sticker_id: StickerId) -> Self {
self.sticker_ids(vec![sticker_id])
}

/// Sets a list of sticker IDs to include in the message.
Expand All @@ -229,8 +229,8 @@ impl<'a> CreateMessage<'a> {
///
/// **Note**: This will keep all existing stickers. Use [`Self::sticker_id()`] to replace
/// existing sticker.
pub fn add_sticker_id(mut self, sticker_id: impl Into<StickerId>) -> Self {
self.sticker_ids.to_mut().push(sticker_id.into());
pub fn add_sticker_id(mut self, sticker_id: StickerId) -> Self {
self.sticker_ids.to_mut().push(sticker_id);
self
}

Expand All @@ -240,11 +240,8 @@ impl<'a> CreateMessage<'a> {
///
/// **Note**: This will keep all existing stickers. Use [`Self::sticker_ids()`] to replace
/// existing stickers.
pub fn add_sticker_ids<T: Into<StickerId>>(
mut self,
sticker_ids: impl IntoIterator<Item = T>,
) -> Self {
self.sticker_ids.to_mut().extend(sticker_ids.into_iter().map(Into::into));
pub fn add_sticker_ids(mut self, sticker_ids: impl IntoIterator<Item = StickerId>) -> Self {
self.sticker_ids.to_mut().extend(sticker_ids);
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/create_scheduled_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ impl<'a> CreateScheduledEvent<'a> {

/// Sets the channel id of the scheduled event. Required if [`Self::kind`] is
/// [`ScheduledEventType::StageInstance`] or [`ScheduledEventType::Voice`].
pub fn channel_id<C: Into<ChannelId>>(mut self, channel_id: C) -> Self {
self.channel_id = Some(channel_id.into());
pub fn channel_id(mut self, channel_id: ChannelId) -> Self {
self.channel_id = Some(channel_id);
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ impl<'a> EditGuild<'a> {
/// Transfers the ownership of the guild to another user by Id.
///
/// **Note**: The current user must be the owner of the guild.
pub fn owner(mut self, user_id: impl Into<UserId>) -> Self {
self.owner_id = Some(user_id.into());
pub fn owner(mut self, user_id: UserId) -> Self {
self.owner_id = Some(user_id);
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_guild_welcome_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ impl<'a> CreateGuildWelcomeChannel<'a> {
}

/// The Id of the channel to show.
pub fn id(mut self, id: impl Into<ChannelId>) -> Self {
self.channel_id = id.into();
pub fn id(mut self, id: ChannelId) -> Self {
self.channel_id = id;
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_guild_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ impl<'a> EditGuildWidget<'a> {
}

/// The server description shown in the welcome screen.
pub fn channel_id(mut self, id: impl Into<ChannelId>) -> Self {
self.channel_id = Some(id.into());
pub fn channel_id(mut self, id: ChannelId) -> Self {
self.channel_id = Some(id);
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ impl<'a> EditMember<'a> {
/// **Note**: Requires the [Move Members] permission.
///
/// [Move Members]: Permissions::MOVE_MEMBERS
pub fn voice_channel(mut self, channel_id: impl Into<ChannelId>) -> Self {
self.channel_id = Some(Some(channel_id.into()));
pub fn voice_channel(mut self, channel_id: ChannelId) -> Self {
self.channel_id = Some(Some(channel_id));
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_scheduled_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl<'a> EditScheduledEvent<'a> {
/// [`kind`]: EditScheduledEvent::kind
/// [`Voice`]: ScheduledEventType::Voice
/// [`External`]: ScheduledEventType::External
pub fn channel_id(mut self, channel_id: impl Into<ChannelId>) -> Self {
self.channel_id = Some(Some(channel_id.into()));
pub fn channel_id(mut self, channel_id: ChannelId) -> Self {
self.channel_id = Some(Some(channel_id));
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ impl<'a> EditWebhook<'a> {
}

/// Set the channel to move the webhook to.
pub fn channel_id(mut self, channel_id: impl Into<ChannelId>) -> Self {
self.channel_id = Some(channel_id.into());
pub fn channel_id(mut self, channel_id: ChannelId) -> Self {
self.channel_id = Some(channel_id);
self
}

Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_webhook_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl<'a> EditWebhookMessage<'a> {

/// Edits a message within a given thread. If the provided thread Id doesn't belong to the
/// current webhook, the API will return an error.
pub fn in_thread(mut self, thread_id: impl Into<ChannelId>) -> Self {
self.thread_id = Some(thread_id.into());
pub fn in_thread(mut self, thread_id: ChannelId) -> Self {
self.thread_id = Some(thread_id);
self
}

Expand Down
8 changes: 4 additions & 4 deletions src/builder/execute_webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,20 @@ impl<'a> ExecuteWebhook<'a> {
/// ```rust,no_run
/// # use serenity::builder::ExecuteWebhook;
/// # use serenity::http::Http;
/// # use serenity::model::webhook::Webhook;
/// # use serenity::model::{id::ChannelId, webhook::Webhook};
/// #
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// # let http: Http = unimplemented!();
/// let url = "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
/// let mut webhook = Webhook::from_url(&http, url).await?;
///
/// let builder = ExecuteWebhook::new().in_thread(12345678).content("test");
/// let builder = ExecuteWebhook::new().in_thread(ChannelId::new(12345678)).content("test");
/// webhook.execute(&http, false, builder).await?;
/// # Ok(())
/// # }
/// ```
pub fn in_thread(mut self, thread_id: impl Into<ChannelId>) -> Self {
self.thread_id = Some(thread_id.into());
pub fn in_thread(mut self, thread_id: ChannelId) -> Self {
self.thread_id = Some(thread_id);
self
}

Expand Down
12 changes: 6 additions & 6 deletions src/builder/get_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ impl GetMessages {
}

/// Indicates to retrieve the messages after a specific message, given its Id.
pub fn after(mut self, message_id: impl Into<MessageId>) -> Self {
self.search_filter = Some(SearchFilter::After(message_id.into()));
pub fn after(mut self, message_id: MessageId) -> Self {
self.search_filter = Some(SearchFilter::After(message_id));
self
}

/// Indicates to retrieve the messages _around_ a specific message, in other words in either
/// direction from the message in time.
pub fn around(mut self, message_id: impl Into<MessageId>) -> Self {
self.search_filter = Some(SearchFilter::Around(message_id.into()));
pub fn around(mut self, message_id: MessageId) -> Self {
self.search_filter = Some(SearchFilter::Around(message_id));
self
}

/// Indicates to retrieve the messages before a specific message, given its Id.
pub fn before(mut self, message_id: impl Into<MessageId>) -> Self {
self.search_filter = Some(SearchFilter::Before(message_id.into()));
pub fn before(mut self, message_id: MessageId) -> Self {
self.search_filter = Some(SearchFilter::Before(message_id));
self
}

Expand Down
Loading

0 comments on commit 17aaf89

Please sign in to comment.