diff options
| author | Zeyla Hellyer <[email protected]> | 2017-10-18 08:34:06 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-10-18 08:34:06 -0700 |
| commit | 9908999a6bae1585bb70b7814f13b49bf99b6c32 (patch) | |
| tree | d789f716400502ae0d124933f5c4d927867e7033 /src/builder/edit_guild.rs | |
| parent | Fix some compilation feature targets, fix lints (diff) | |
| download | serenity-9908999a6bae1585bb70b7814f13b49bf99b6c32.tar.xz serenity-9908999a6bae1585bb70b7814f13b49bf99b6c32.zip | |
Slightly improve performance of builders
Builders would keep a `serde_json::Map<String, Value>`, which would
require re-creating owned strings for the same parameter multiple times
in some cases, depending on builder defaults and keying strategies.
This commit uses a `std::collections::HashMap<&'static str, Value>`
internally, and moves over values to a `serde_json::Map<String, Value>`
when it comes time to sending them to the appropriate `http` module
function.
This saves the number of heap-allocated string creations on most
builders, with specific performance increase on `builder::CreateMessage`
and `builder::CreateEmbed` & co.
Diffstat (limited to 'src/builder/edit_guild.rs')
| -rw-r--r-- | src/builder/edit_guild.rs | 26 |
1 files changed, 11 insertions, 15 deletions
diff --git a/src/builder/edit_guild.rs b/src/builder/edit_guild.rs index 0719305..45969fe 100644 --- a/src/builder/edit_guild.rs +++ b/src/builder/edit_guild.rs @@ -1,5 +1,6 @@ use internal::prelude::*; use model::{ChannelId, Region, UserId, VerificationLevel}; +use std::collections::HashMap; /// A builder to optionally edit certain fields of a [`Guild`]. This is meant /// for usage with [`Guild::edit`]. @@ -11,7 +12,7 @@ use model::{ChannelId, Region, UserId, VerificationLevel}; /// [`Guild`]: ../model/struct.Guild.html /// [Manage Guild]: ../model/permissions/constant.MANAGE_GUILD.html #[derive(Clone, Debug, Default)] -pub struct EditGuild(pub Map<String, Value>); +pub struct EditGuild(pub HashMap<&'static str, Value>); impl EditGuild { /// Set the "AFK voice channel" that users are to move to if they have been @@ -24,7 +25,7 @@ impl EditGuild { /// [`afk_timeout`]: #method.afk_timeout pub fn afk_channel<C: Into<ChannelId>>(mut self, channel: Option<C>) -> Self { self.0.insert( - "afk_channel_id".to_string(), + "afk_channel_id", match channel { Some(channel) => Value::Number(Number::from(channel.into().0)), None => Value::Null, @@ -40,7 +41,7 @@ impl EditGuild { /// [`afk_channel`]: #method.afk_channel pub fn afk_timeout(mut self, timeout: u64) -> Self { self.0.insert( - "afk_timeout".to_string(), + "afk_timeout", Value::Number(Number::from(timeout)), ); @@ -78,7 +79,7 @@ impl EditGuild { /// [`utils::read_image`]: ../utils/fn.read_image.html pub fn icon(mut self, icon: Option<&str>) -> Self { self.0.insert( - "icon".to_string(), + "icon", icon.map_or_else(|| Value::Null, |x| Value::String(x.to_string())), ); @@ -89,8 +90,7 @@ impl EditGuild { /// /// **Note**: Must be between (and including) 2-100 chracters. pub fn name(mut self, name: &str) -> Self { - self.0 - .insert("name".to_string(), Value::String(name.to_string())); + self.0.insert("name", Value::String(name.to_string())); self } @@ -99,10 +99,8 @@ impl EditGuild { /// /// **Note**: The current user must be the owner of the guild. pub fn owner<U: Into<UserId>>(mut self, user_id: U) -> Self { - self.0.insert( - "owner_id".to_string(), - Value::Number(Number::from(user_id.into().0)), - ); + let id = Value::Number(Number::from(user_id.into().0)); + self.0.insert("owner_id", id); self } @@ -134,8 +132,7 @@ impl EditGuild { /// /// [`Region::UsWest`]: ../model/enum.Region.html#variant.UsWest pub fn region(mut self, region: Region) -> Self { - self.0 - .insert("region".to_string(), Value::String(region.name().to_string())); + self.0.insert("region", Value::String(region.name().to_string())); self } @@ -149,8 +146,7 @@ impl EditGuild { /// [`features`]: ../model/struct.LiveGuild.html#structfield.features pub fn splash(mut self, splash: Option<&str>) -> Self { let splash = splash.map_or(Value::Null, |x| Value::String(x.to_string())); - - self.0.insert("splash".to_string(), splash); + self.0.insert("splash", splash); self } @@ -189,7 +185,7 @@ impl EditGuild { where V: Into<VerificationLevel> { let num = Value::Number(Number::from(verification_level.into().num())); - self.0.insert("verification_level".to_string(), num); + self.0.insert("verification_level", num); self } |