From 7f092bd4f5d84f0103d439d73fafe30cd1a434ca Mon Sep 17 00:00:00 2001 From: Michael Krasnitski <42564254+mkrasnitski@users.noreply.github.com> Date: Sun, 7 Jan 2024 12:17:08 -0500 Subject: [PATCH] Revert `Request::params` back to `Option` (#2695) Follow-up to #2694. When `Request::params` was made into an ArrayVec, the `Option` around it was removed in order to avoid having to add a turbofish on `None` to specify the value of `MAX_PARAMS`. Also, `Request::new` also needed to be changed so that the value of `MAX_PARAMS` could be inferred. Now that the field is a slice again, we can wrap it in `Option` again (at no cost to size, thanks to niche opts). We ensure we never store the redundant `Some(&[])` by checking for an empty slice and storing `None` instead. This way, we ensure we never read an empty slice out of the `Some` variant. --- src/http/client.rs | 375 ++++++++++++++++++++++---------------------- src/http/request.rs | 28 ++-- 2 files changed, 196 insertions(+), 207 deletions(-) diff --git a/src/http/client.rs b/src/http/client.rs index a8c446ef5a6..972c5f4e535 100644 --- a/src/http/client.rs +++ b/src/http/client.rs @@ -253,7 +253,7 @@ impl Http { guild_id, user_id, }, - params: &[], + params: None, }) .await?; @@ -286,7 +286,7 @@ impl Http { role_id, user_id, }, - params: &[], + params: None, }) .await } @@ -318,7 +318,7 @@ impl Http { guild_id, user_id, }, - params: &[("delete_message_seconds", delete_message_seconds.to_string())], + params: Some(&[("delete_message_seconds", delete_message_seconds.to_string())]), }) .await } @@ -340,7 +340,7 @@ impl Http { route: Route::GuildBulkBan { guild_id, }, - params: &[], + params: None, }) .await } @@ -361,7 +361,7 @@ impl Http { route: Route::ChannelTyping { channel_id, }, - params: &[], + params: None, }) .await } @@ -390,7 +390,7 @@ impl Http { route: Route::GuildChannels { guild_id, }, - params: &[], + params: None, }) .await } @@ -407,7 +407,7 @@ impl Http { headers: audit_log_reason.map(reason_into_header), method: LightMethod::Post, route: Route::StageInstances, - params: &[], + params: None, }) .await } @@ -431,7 +431,7 @@ impl Http { channel_id, message_id, }, - params: &[], + params: None, }) .await } @@ -453,7 +453,7 @@ impl Http { route: Route::ChannelThreads { channel_id, }, - params: &[], + params: None, }) .await } @@ -488,7 +488,7 @@ impl Http { route: Route::ChannelForumPosts { channel_id, }, - params: &[], + params: None, }) .await } @@ -514,7 +514,7 @@ impl Http { route: Route::GuildEmojis { guild_id, }, - params: &[], + params: None, }) .await } @@ -537,7 +537,7 @@ impl Http { application_id: self.try_application_id()?, token: interaction_token, }, - params: &[], + params: None, }; if files.is_empty() { @@ -572,7 +572,7 @@ impl Http { route: Route::Commands { application_id: self.try_application_id()?, }, - params: &[], + params: None, }) .await } @@ -590,7 +590,7 @@ impl Http { route: Route::Commands { application_id: self.try_application_id()?, }, - params: &[], + params: None, }) .await } @@ -610,7 +610,7 @@ impl Http { application_id: self.try_application_id()?, guild_id, }, - params: &[], + params: None, }) .await } @@ -655,7 +655,7 @@ impl Http { headers: None, method: LightMethod::Post, route: Route::Guilds, - params: &[], + params: None, }) .await } @@ -681,7 +681,7 @@ impl Http { application_id: self.try_application_id()?, guild_id, }, - params: &[], + params: None, }) .await } @@ -710,7 +710,7 @@ impl Http { guild_id, integration_id, }, - params: &[], + params: None, }) .await } @@ -737,7 +737,7 @@ impl Http { interaction_id, token: interaction_token, }, - params: &[], + params: None, }; if files.is_empty() { @@ -779,7 +779,7 @@ impl Http { route: Route::ChannelInvites { channel_id, }, - params: &[], + params: None, }) .await } @@ -803,7 +803,7 @@ impl Http { channel_id, target_id, }, - params: &[], + params: None, }) .await } @@ -818,7 +818,7 @@ impl Http { headers: None, method: LightMethod::Post, route: Route::UserMeDmChannels, - params: &[], + params: None, }) .await } @@ -840,7 +840,7 @@ impl Http { message_id, reaction: &reaction_type.as_data(), }, - params: &[], + params: None, }) .await } @@ -861,7 +861,7 @@ impl Http { route: Route::GuildRoles { guild_id, }, - params: &[], + params: None, }) .await?; @@ -894,7 +894,7 @@ impl Http { route: Route::GuildScheduledEvents { guild_id, }, - params: &[], + params: None, }) .await } @@ -923,7 +923,7 @@ impl Http { route: Route::GuildStickers { guild_id, }, - params: &[], + params: None, }) .await } @@ -953,7 +953,7 @@ impl Http { route: Route::Entitlements { application_id: self.try_application_id()?, }, - params: &[], + params: None, }) .await } @@ -1000,7 +1000,7 @@ impl Http { route: Route::ChannelWebhooks { channel_id, }, - params: &[], + params: None, }) .await } @@ -1019,7 +1019,7 @@ impl Http { route: Route::Channel { channel_id, }, - params: &[], + params: None, }) .await } @@ -1038,7 +1038,7 @@ impl Http { route: Route::StageInstance { channel_id, }, - params: &[], + params: None, }) .await } @@ -1061,7 +1061,7 @@ impl Http { guild_id, emoji_id, }, - params: &[], + params: None, }) .await } @@ -1082,7 +1082,7 @@ impl Http { token: interaction_token, message_id, }, - params: &[], + params: None, }) .await } @@ -1098,7 +1098,7 @@ impl Http { application_id: self.try_application_id()?, command_id, }, - params: &[], + params: None, }) .await } @@ -1113,7 +1113,7 @@ impl Http { route: Route::Guild { guild_id, }, - params: &[], + params: None, }) .await } @@ -1134,7 +1134,7 @@ impl Http { guild_id, command_id, }, - params: &[], + params: None, }) .await } @@ -1155,7 +1155,7 @@ impl Http { guild_id, integration_id, }, - params: &[], + params: None, }) .await } @@ -1174,7 +1174,7 @@ impl Http { route: Route::Invite { code, }, - params: &[], + params: None, }) .await } @@ -1195,7 +1195,7 @@ impl Http { channel_id, message_id, }, - params: &[], + params: None, }) .await } @@ -1215,7 +1215,7 @@ impl Http { route: Route::ChannelMessagesBulkDelete { channel_id, }, - params: &[], + params: None, }) .await } @@ -1252,7 +1252,7 @@ impl Http { channel_id, message_id, }, - params: &[], + params: None, }) .await } @@ -1274,7 +1274,7 @@ impl Http { message_id, reaction: &reaction_type.as_data(), }, - params: &[], + params: None, }) .await } @@ -1293,7 +1293,7 @@ impl Http { application_id: self.try_application_id()?, token: interaction_token, }, - params: &[], + params: None, }) .await } @@ -1314,7 +1314,7 @@ impl Http { channel_id, target_id, }, - params: &[], + params: None, }) .await } @@ -1338,7 +1338,7 @@ impl Http { user_id, reaction: &reaction_type.as_data(), }, - params: &[], + params: None, }) .await } @@ -1360,7 +1360,7 @@ impl Http { message_id, reaction: &reaction_type.as_data(), }, - params: &[], + params: None, }) .await } @@ -1381,7 +1381,7 @@ impl Http { guild_id, role_id, }, - params: &[], + params: None, }) .await } @@ -1406,7 +1406,7 @@ impl Http { guild_id, event_id, }, - params: &[], + params: None, }) .await } @@ -1429,7 +1429,7 @@ impl Http { guild_id, sticker_id, }, - params: &[], + params: None, }) .await } @@ -1446,7 +1446,7 @@ impl Http { application_id: self.try_application_id()?, entitlement_id, }, - params: &[], + params: None, }) .await } @@ -1483,7 +1483,7 @@ impl Http { route: Route::Webhook { webhook_id, }, - params: &[], + params: None, }) .await } @@ -1524,7 +1524,7 @@ impl Http { webhook_id, token, }, - params: &[], + params: None, }) .await } @@ -1546,7 +1546,7 @@ impl Http { route: Route::Channel { channel_id, }, - params: &[], + params: None, }) .await } @@ -1566,7 +1566,7 @@ impl Http { route: Route::StageInstance { channel_id, }, - params: &[], + params: None, }) .await } @@ -1592,7 +1592,7 @@ impl Http { guild_id, emoji_id, }, - params: &[], + params: None, }) .await } @@ -1619,7 +1619,7 @@ impl Http { token: interaction_token, message_id, }, - params: &[], + params: None, }; if new_attachments.is_empty() { @@ -1655,7 +1655,7 @@ impl Http { token: interaction_token, message_id, }, - params: &[], + params: None, }) .await } @@ -1681,7 +1681,7 @@ impl Http { application_id: self.try_application_id()?, command_id, }, - params: &[], + params: None, }) .await } @@ -1703,7 +1703,7 @@ impl Http { route: Route::Guild { guild_id, }, - params: &[], + params: None, }) .await } @@ -1731,7 +1731,7 @@ impl Http { guild_id, command_id, }, - params: &[], + params: None, }) .await } @@ -1759,7 +1759,7 @@ impl Http { guild_id, command_id, }, - params: &[], + params: None, }) .await } @@ -1780,7 +1780,7 @@ impl Http { route: Route::GuildChannels { guild_id, }, - params: &[], + params: None, }) .await } @@ -1807,7 +1807,7 @@ impl Http { route: Route::GuildMfa { guild_id, }, - params: &[], + params: None, }) .await .map(|mfa: GuildMfaLevel| mfa.level) @@ -1830,7 +1830,7 @@ impl Http { route: Route::GuildWidget { guild_id, }, - params: &[], + params: None, }) .await } @@ -1852,7 +1852,7 @@ impl Http { route: Route::GuildWelcomeScreen { guild_id, }, - params: &[], + params: None, }) .await } @@ -1877,7 +1877,7 @@ impl Http { guild_id, user_id, }, - params: &[], + params: None, }) .await?; @@ -1907,7 +1907,7 @@ impl Http { channel_id, message_id, }, - params: &[], + params: None, }; if new_attachments.is_empty() { @@ -1940,7 +1940,7 @@ impl Http { channel_id, message_id, }, - params: &[], + params: None, }) .await } @@ -1962,7 +1962,7 @@ impl Http { route: Route::GuildMemberMe { guild_id, }, - params: &[], + params: None, }) .await } @@ -1987,7 +1987,7 @@ impl Http { route: Route::GuildMemberMe { guild_id, }, - params: &[], + params: None, }) .await } @@ -2009,7 +2009,7 @@ impl Http { route: Route::ChannelFollowNews { channel_id: news_channel_id, }, - params: &[], + params: None, }) .await } @@ -2028,7 +2028,7 @@ impl Http { application_id: self.try_application_id()?, token: interaction_token, }, - params: &[], + params: None, }) .await } @@ -2053,7 +2053,7 @@ impl Http { application_id: self.try_application_id()?, token: interaction_token, }, - params: &[], + params: None, }; if new_attachments.is_empty() { @@ -2079,7 +2079,7 @@ impl Http { headers: None, method: LightMethod::Patch, route: Route::UserMe, - params: &[], + params: None, }) .await } @@ -2102,7 +2102,7 @@ impl Http { guild_id, role_id, }, - params: &[], + params: None, }) .await?; @@ -2136,7 +2136,7 @@ impl Http { route: Route::GuildRoles { guild_id, }, - params: &[], + params: None, }) .await?; @@ -2173,7 +2173,7 @@ impl Http { guild_id, event_id, }, - params: &[], + params: None, }) .await } @@ -2200,7 +2200,7 @@ impl Http { guild_id, sticker_id, }, - params: &[], + params: None, }) .await?; @@ -2226,7 +2226,7 @@ impl Http { route: Route::Channel { channel_id, }, - params: &[], + params: None, }) .await } @@ -2276,7 +2276,7 @@ impl Http { guild_id, user_id, }, - params: &[], + params: None, }) .await } @@ -2327,7 +2327,7 @@ impl Http { route: Route::GuildVoiceStateMe { guild_id, }, - params: &[], + params: None, }) .await } @@ -2378,7 +2378,7 @@ impl Http { route: Route::Webhook { webhook_id, }, - params: &[], + params: None, }) .await } @@ -2426,7 +2426,7 @@ impl Http { webhook_id, token, }, - params: &[], + params: None, }) .await } @@ -2506,7 +2506,7 @@ impl Http { webhook_id, token, }, - params: ¶ms, + params: Some(¶ms), }; if files.is_empty() { @@ -2536,11 +2536,6 @@ impl Http { token: &str, message_id: MessageId, ) -> Result { - let mut params = ArrayVec::<_, 1>::new(); - if let Some(thread_id) = thread_id { - params.push(("thread_id", thread_id.to_string())); - } - self.fire(Request { body: None, multipart: None, @@ -2551,7 +2546,10 @@ impl Http { token, message_id, }, - params: ¶ms, + params: thread_id + .map(|thread_id| [("thread_id", thread_id.to_string())]) + .as_ref() + .map(<[_; 1]>::as_slice), }) .await } @@ -2566,10 +2564,7 @@ impl Http { map: &impl serde::Serialize, new_attachments: Vec>, ) -> Result { - let mut params = ArrayVec::<_, 1>::new(); - if let Some(thread_id) = thread_id { - params.push(("thread_id", thread_id.to_string())); - } + let params = thread_id.map(|thread_id| [("thread_id", thread_id.to_string())]); let mut request = Request { body: None, @@ -2581,7 +2576,7 @@ impl Http { token, message_id, }, - params: ¶ms, + params: params.as_ref().map(<[_; 1]>::as_slice), }; if new_attachments.is_empty() { @@ -2605,11 +2600,6 @@ impl Http { token: &str, message_id: MessageId, ) -> Result<()> { - let mut params = ArrayVec::<_, 1>::new(); - if let Some(thread_id) = thread_id { - params.push(("thread_id", thread_id.to_string())); - } - self.wind(204, Request { body: None, multipart: None, @@ -2620,7 +2610,10 @@ impl Http { token, message_id, }, - params: ¶ms, + params: thread_id + .map(|thread_id| [("thread_id", thread_id.to_string())]) + .as_ref() + .map(<[_; 1]>::as_slice), }) .await } @@ -2642,7 +2635,7 @@ impl Http { headers: None, method: LightMethod::Get, route: Route::StatusMaintenancesActive, - params: &[], + params: None, }) .await?; @@ -2686,7 +2679,7 @@ impl Http { route: Route::GuildBans { guild_id, }, - params: ¶ms, + params: Some(¶ms), }) .await } @@ -2722,7 +2715,7 @@ impl Http { route: Route::GuildAuditLogs { guild_id, }, - params: ¶ms, + params: Some(¶ms), }) .await } @@ -2739,7 +2732,7 @@ impl Http { route: Route::GuildAutomodRules { guild_id, }, - params: &[], + params: None, }) .await } @@ -2757,7 +2750,7 @@ impl Http { guild_id, rule_id, }, - params: &[], + params: None, }) .await } @@ -2781,7 +2774,7 @@ impl Http { route: Route::GuildAutomodRules { guild_id, }, - params: &[], + params: None, }) .await } @@ -2807,7 +2800,7 @@ impl Http { guild_id, rule_id, }, - params: &[], + params: None, }) .await } @@ -2830,7 +2823,7 @@ impl Http { guild_id, rule_id, }, - params: &[], + params: None, }) .await } @@ -2843,7 +2836,7 @@ impl Http { headers: None, method: LightMethod::Get, route: Route::GatewayBot, - params: &[], + params: None, }) .await } @@ -2858,7 +2851,7 @@ impl Http { route: Route::ChannelInvites { channel_id, }, - params: &[], + params: None, }) .await } @@ -2876,7 +2869,7 @@ impl Http { route: Route::ChannelThreadMembers { channel_id, }, - params: &[], + params: None, }) .await } @@ -2891,7 +2884,7 @@ impl Http { route: Route::GuildThreadsActive { guild_id, }, - params: &[], + params: None, }) .await } @@ -2919,7 +2912,7 @@ impl Http { route: Route::ChannelArchivedPublicThreads { channel_id, }, - params: ¶ms, + params: Some(¶ms), }) .await } @@ -2947,7 +2940,7 @@ impl Http { route: Route::ChannelArchivedPrivateThreads { channel_id, }, - params: ¶ms, + params: Some(¶ms), }) .await } @@ -2975,7 +2968,7 @@ impl Http { route: Route::ChannelJoinedPrivateThreads { channel_id, }, - params: ¶ms, + params: Some(¶ms), }) .await } @@ -2990,7 +2983,7 @@ impl Http { route: Route::ChannelThreadMemberMe { channel_id, }, - params: &[], + params: None, }) .await } @@ -3005,7 +2998,7 @@ impl Http { route: Route::ChannelThreadMemberMe { channel_id, }, - params: &[], + params: None, }) .await } @@ -3025,7 +3018,7 @@ impl Http { channel_id, user_id, }, - params: &[], + params: None, }) .await } @@ -3045,7 +3038,7 @@ impl Http { channel_id, user_id, }, - params: &[], + params: None, }) .await } @@ -3065,7 +3058,7 @@ impl Http { channel_id, user_id, }, - params: &[("with_member", with_member.to_string())], + params: Some(&[("with_member", with_member.to_string())]), }) .await } @@ -3099,7 +3092,7 @@ impl Http { route: Route::ChannelWebhooks { channel_id, }, - params: &[], + params: None, }) .await } @@ -3114,7 +3107,7 @@ impl Http { route: Route::Channel { channel_id, }, - params: &[], + params: None, }) .await } @@ -3129,7 +3122,7 @@ impl Http { route: Route::GuildChannels { guild_id, }, - params: &[], + params: None, }) .await } @@ -3144,7 +3137,7 @@ impl Http { route: Route::StageInstance { channel_id, }, - params: &[], + params: None, }) .await } @@ -3183,7 +3176,7 @@ impl Http { message_id, answer_id, }, - params: ¶ms, + params: Some(¶ms), }) .await?; @@ -3204,7 +3197,7 @@ impl Http { channel_id, message_id, }, - params: &[], + params: None, }) .await } @@ -3219,7 +3212,7 @@ impl Http { headers: None, method: LightMethod::Get, route: Route::Oauth2ApplicationCurrent, - params: &[], + params: None, }) .await } @@ -3232,7 +3225,7 @@ impl Http { headers: None, method: LightMethod::Get, route: Route::UserMe, - params: &[], + params: None, }) .await } @@ -3247,7 +3240,7 @@ impl Http { route: Route::GuildEmojis { guild_id, }, - params: &[], + params: None, }) .await } @@ -3263,7 +3256,7 @@ impl Http { guild_id, emoji_id, }, - params: &[], + params: None, }) .await } @@ -3315,7 +3308,7 @@ impl Http { route: Route::Entitlements { application_id: self.try_application_id()?, }, - params: ¶ms, + params: Some(¶ms), }) .await } @@ -3328,7 +3321,7 @@ impl Http { headers: None, method: LightMethod::Get, route: Route::Gateway, - params: &[], + params: None, }) .await } @@ -3343,7 +3336,7 @@ impl Http { route: Route::Commands { application_id: self.try_application_id()?, }, - params: &[], + params: None, }) .await } @@ -3358,7 +3351,7 @@ impl Http { route: Route::Commands { application_id: self.try_application_id()?, }, - params: &[("with_localizations", String::from("true"))], + params: Some(&[("with_localizations", String::from("true"))]), }) .await } @@ -3374,7 +3367,7 @@ impl Http { application_id: self.try_application_id()?, command_id, }, - params: &[], + params: None, }) .await } @@ -3389,7 +3382,7 @@ impl Http { route: Route::Guild { guild_id, }, - params: &[], + params: None, }) .await } @@ -3404,7 +3397,7 @@ impl Http { route: Route::Guild { guild_id, }, - params: &[("with_counts", String::from("true"))], + params: Some(&[("with_counts", String::from("true"))]), }) .await } @@ -3420,7 +3413,7 @@ impl Http { application_id: self.try_application_id()?, guild_id, }, - params: &[], + params: None, }) .await } @@ -3440,7 +3433,7 @@ impl Http { application_id: self.try_application_id()?, guild_id, }, - params: &[("with_localizations", String::from("true"))], + params: Some(&[("with_localizations", String::from("true"))]), }) .await } @@ -3461,7 +3454,7 @@ impl Http { guild_id, command_id, }, - params: &[], + params: None, }) .await } @@ -3480,7 +3473,7 @@ impl Http { application_id: self.try_application_id()?, guild_id, }, - params: &[], + params: None, }) .await } @@ -3501,7 +3494,7 @@ impl Http { guild_id, command_id, }, - params: &[], + params: None, }) .await } @@ -3518,7 +3511,7 @@ impl Http { route: Route::GuildWidget { guild_id, }, - params: &[], + params: None, }) .await } @@ -3533,7 +3526,7 @@ impl Http { route: Route::GuildPreview { guild_id, }, - params: &[], + params: None, }) .await } @@ -3548,7 +3541,7 @@ impl Http { route: Route::GuildWelcomeScreen { guild_id, }, - params: &[], + params: None, }) .await } @@ -3563,7 +3556,7 @@ impl Http { route: Route::GuildIntegrations { guild_id, }, - params: &[], + params: None, }) .await } @@ -3578,7 +3571,7 @@ impl Http { route: Route::GuildInvites { guild_id, }, - params: &[], + params: None, }) .await } @@ -3598,7 +3591,7 @@ impl Http { route: Route::GuildVanityUrl { guild_id, }, - params: &[], + params: None, }) .await .map(|x: GuildVanityUrl| x.code) @@ -3633,7 +3626,7 @@ impl Http { route: Route::GuildMembers { guild_id, }, - params: ¶ms, + params: Some(¶ms), }) .await?; @@ -3658,7 +3651,7 @@ impl Http { route: Route::GuildPrune { guild_id, }, - params: &[("days", days.to_string())], + params: Some(&[("days", days.to_string())]), }) .await } @@ -3674,7 +3667,7 @@ impl Http { route: Route::GuildRegions { guild_id, }, - params: &[], + params: None, }) .await } @@ -3690,7 +3683,7 @@ impl Http { route: Route::GuildRoles { guild_id, }, - params: &[], + params: None, }) .await?; @@ -3725,7 +3718,7 @@ impl Http { guild_id, event_id, }, - params: &[("with_user_count", with_user_count.to_string())], + params: Some(&[("with_user_count", with_user_count.to_string())]), }) .await } @@ -3748,7 +3741,7 @@ impl Http { route: Route::GuildScheduledEvents { guild_id, }, - params: &[("with_user_count", with_user_count.to_string())], + params: Some(&[("with_user_count", with_user_count.to_string())]), }) .await } @@ -3800,7 +3793,7 @@ impl Http { guild_id, event_id, }, - params: ¶ms, + params: Some(¶ms), }) .await } @@ -3816,7 +3809,7 @@ impl Http { route: Route::GuildStickers { guild_id, }, - params: &[], + params: None, }) .await?; @@ -3847,7 +3840,7 @@ impl Http { guild_id, sticker_id, }, - params: &[], + params: None, }) .await?; @@ -3887,7 +3880,7 @@ impl Http { route: Route::GuildWebhooks { guild_id, }, - params: &[], + params: None, }) .await } @@ -3940,7 +3933,7 @@ impl Http { headers: None, method: LightMethod::Get, route: Route::UserMeGuilds, - params: ¶ms, + params: Some(¶ms), }) .await } @@ -3984,7 +3977,7 @@ impl Http { route: Route::UserMeGuildMember { guild_id, }, - params: &[], + params: None, }) .await?; @@ -4031,7 +4024,7 @@ impl Http { route: Route::Invite { code, }, - params: ¶ms, + params: Some(¶ms), }) .await } @@ -4048,7 +4041,7 @@ impl Http { guild_id, user_id, }, - params: &[], + params: None, }) .await?; @@ -4074,7 +4067,7 @@ impl Http { channel_id, message_id, }, - params: &[], + params: None, }) .await } @@ -4106,7 +4099,7 @@ impl Http { route: Route::ChannelMessages { channel_id, }, - params: ¶ms, + params: Some(¶ms), }) .await } @@ -4124,7 +4117,7 @@ impl Http { headers: None, method: LightMethod::Get, route: Route::StickerPacks, - params: &[], + params: None, }) .await .map(|s: StickerPacks| s.sticker_packs) @@ -4140,7 +4133,7 @@ impl Http { route: Route::ChannelPins { channel_id, }, - params: &[], + params: None, }) .await } @@ -4169,7 +4162,7 @@ impl Http { message_id, reaction: &reaction_type.as_data(), }, - params: ¶ms, + params: Some(¶ms), }) .await } @@ -4184,7 +4177,7 @@ impl Http { route: Route::Skus { application_id: self.try_application_id()?, }, - params: &[], + params: None, }) .await } @@ -4199,7 +4192,7 @@ impl Http { route: Route::Sticker { sticker_id, }, - params: &[], + params: None, }) .await } @@ -4221,7 +4214,7 @@ impl Http { headers: None, method: LightMethod::Get, route: Route::StatusIncidentsUnresolved, - params: &[], + params: None, }) .await?; @@ -4245,7 +4238,7 @@ impl Http { headers: None, method: LightMethod::Get, route: Route::StatusMaintenancesUpcoming, - params: &[], + params: None, }) .await?; @@ -4262,7 +4255,7 @@ impl Http { route: Route::User { user_id, }, - params: &[], + params: None, }) .await } @@ -4279,7 +4272,7 @@ impl Http { headers: None, method: LightMethod::Get, route: Route::UserMeConnections, - params: &[], + params: None, }) .await } @@ -4292,7 +4285,7 @@ impl Http { headers: None, method: LightMethod::Get, route: Route::UserMeDmChannels, - params: &[], + params: None, }) .await } @@ -4305,7 +4298,7 @@ impl Http { headers: None, method: LightMethod::Get, route: Route::VoiceRegions, - params: &[], + params: None, }) .await } @@ -4339,7 +4332,7 @@ impl Http { route: Route::Webhook { webhook_id, }, - params: &[], + params: None, }) .await } @@ -4379,7 +4372,7 @@ impl Http { webhook_id, token, }, - params: &[], + params: None, }) .await } @@ -4416,7 +4409,7 @@ impl Http { webhook_id, token, }, - params: &[], + params: None, }) .await } @@ -4437,7 +4430,7 @@ impl Http { guild_id, user_id, }, - params: &[], + params: None, }) .await } @@ -4452,7 +4445,7 @@ impl Http { route: Route::UserMeGuild { guild_id, }, - params: &[], + params: None, }) .await } @@ -4476,7 +4469,7 @@ impl Http { route: Route::ChannelMessages { channel_id, }, - params: &[], + params: None, }; if files.is_empty() { @@ -4508,7 +4501,7 @@ impl Http { channel_id, message_id, }, - params: &[], + params: None, }) .await } @@ -4529,7 +4522,7 @@ impl Http { guild_id, user_id, }, - params: &[], + params: None, }) .await } @@ -4556,7 +4549,7 @@ impl Http { user_id, role_id, }, - params: &[], + params: None, }) .await } @@ -4578,10 +4571,10 @@ impl Http { route: Route::GuildMembersSearch { guild_id, }, - params: &[ + params: Some(&[ ("query", query.to_string()), ("limit", limit.unwrap_or(constants::MEMBER_FETCH_LIMIT).to_string()), - ], + ]), }) .await?; @@ -4611,7 +4604,7 @@ impl Http { route: Route::GuildPrune { guild_id, }, - params: &[("days", days.to_string())], + params: Some(&[("days", days.to_string())]), }) .await } @@ -4631,7 +4624,7 @@ impl Http { guild_id, integration_id, }, - params: &[], + params: None, }) .await } @@ -4692,7 +4685,7 @@ impl Http { channel_id, message_id, }, - params: &[], + params: None, }) .await } @@ -4722,7 +4715,7 @@ impl Http { /// let channel_id = ChannelId::new(381880193700069377); /// let route = Route::ChannelMessages { channel_id }; /// - /// let mut request = Request::new(route, LightMethod::Post, &[]).body(Some(bytes)); + /// let mut request = Request::new(route, LightMethod::Post).body(Some(bytes)); /// /// let message: Message = http.fire(request).await?; /// @@ -4762,7 +4755,7 @@ impl Http { /// let channel_id = ChannelId::new(381880193700069377); /// let route = Route::ChannelMessages { channel_id }; /// - /// let mut request = Request::new(route, LightMethod::Post, &[]).body(Some(bytes)); + /// let mut request = Request::new(route, LightMethod::Post).body(Some(bytes)); /// /// let response = http.request(request).await?; /// diff --git a/src/http/request.rs b/src/http/request.rs index f02aa03a01e..b5541670306 100644 --- a/src/http/request.rs +++ b/src/http/request.rs @@ -25,22 +25,18 @@ pub struct Request<'a> { pub(super) headers: Option, pub(super) method: LightMethod, pub(super) route: Route<'a>, - pub(super) params: &'a [(&'static str, String)], + pub(super) params: Option<&'a [(&'static str, String)]>, } impl<'a> Request<'a> { - pub fn new( - route: Route<'a>, - method: LightMethod, - params: &'a [(&'static str, String)], - ) -> Self { + pub fn new(route: Route<'a>, method: LightMethod) -> Self { Self { body: None, multipart: None, headers: None, method, route, - params, + params: None, } } @@ -60,7 +56,11 @@ impl<'a> Request<'a> { } pub fn params(mut self, params: &'a [(&'static str, String)]) -> Self { - self.params = params; + if params.is_empty() { + self.params = None; + } else { + self.params = Some(params); + } self } @@ -78,9 +78,9 @@ impl<'a> Request<'a> { path = path.replace("https://discord.com", proxy.trim_end_matches('/')); } - if !self.params.is_empty() { + if let Some(params) = self.params { path += "?"; - for (param, value) in self.params { + for (param, value) in params { write!(path, "&{param}={value}").unwrap(); } } @@ -138,11 +138,7 @@ impl<'a> Request<'a> { } #[must_use] - pub fn params_ref(&self) -> Option<&[(&'static str, String)]> { - if self.params.is_empty() { - None - } else { - Some(self.params) - } + pub fn params_ref(&self) -> Option<&'a [(&'static str, String)]> { + self.params } }