From 3a0c8908ce837f6fe64f865a1a7a9de63cbd237c Mon Sep 17 00:00:00 2001 From: acdenisSK Date: Wed, 27 Dec 2017 18:29:34 +0100 Subject: [PATCH] Improve performance of builders even further By negating hashing altogether. The increase is around 1000-ish nanoseconds saved. --- src/builder/create_embed.rs | 14 +-- src/builder/create_invite.rs | 6 +- src/builder/create_message.rs | 9 +- src/builder/edit_channel.rs | 4 +- src/builder/edit_guild.rs | 4 +- src/builder/edit_member.rs | 4 +- src/builder/edit_profile.rs | 4 +- src/builder/edit_role.rs | 6 +- src/builder/execute_webhook.rs | 6 +- src/builder/get_messages.rs | 4 +- src/builder/mod.rs | 2 + src/builder/vec_map.rs | 120 ++++++++++++++++++++++++++ src/client/context.rs | 6 +- src/model/channel/channel_category.rs | 6 +- src/model/channel/channel_id.rs | 22 ++--- src/model/channel/embed.rs | 2 +- src/model/channel/guild_channel.rs | 8 +- src/model/channel/message.rs | 2 +- src/model/guild/guild_id.rs | 8 +- src/model/guild/member.rs | 6 +- src/model/invite.rs | 2 +- src/model/user.rs | 6 +- src/model/webhook.rs | 2 +- src/utils/mod.rs | 12 +++ tests/test_create_embed.rs | 2 +- 25 files changed, 201 insertions(+), 66 deletions(-) create mode 100644 src/builder/vec_map.rs diff --git a/src/builder/create_embed.rs b/src/builder/create_embed.rs index 1b15253e1d0..fa8e12b4990 100644 --- a/src/builder/create_embed.rs +++ b/src/builder/create_embed.rs @@ -19,10 +19,10 @@ use chrono::{DateTime, TimeZone}; use internal::prelude::*; use model::channel::Embed; use serde_json::Value; -use std::collections::HashMap; use std::default::Default; use std::fmt::Display; use utils; +use super::VecMap; #[cfg(feature = "utils")] use utils::Colour; @@ -39,7 +39,7 @@ use utils::Colour; /// [`Embed`]: ../model/struct.Embed.html /// [`ExecuteWebhook::embeds`]: struct.ExecuteWebhook.html#method.embeds #[derive(Clone, Debug)] -pub struct CreateEmbed(pub HashMap<&'static str, Value>); +pub struct CreateEmbed(pub VecMap<&'static str, Value>); impl CreateEmbed { /// Set the author of the embed. @@ -50,7 +50,7 @@ impl CreateEmbed { /// [`CreateEmbedAuthor`]: struct.CreateEmbedAuthor.html pub fn author(mut self, f: F) -> Self where F: FnOnce(CreateEmbedAuthor) -> CreateEmbedAuthor { - let map = utils::hashmap_to_json_map(f(CreateEmbedAuthor::default()).0); + let map = utils::vecmap_to_json_map(f(CreateEmbedAuthor::default()).0); self.0.insert("author", Value::Object(map)); @@ -157,7 +157,7 @@ impl CreateEmbed { pub fn footer(mut self, f: F) -> Self where F: FnOnce(CreateEmbedFooter) -> CreateEmbedFooter { let footer = f(CreateEmbedFooter::default()).0; - let map = utils::hashmap_to_json_map(footer); + let map = utils::vecmap_to_json_map(footer); self.0.insert("footer", Value::Object(map)); @@ -309,7 +309,7 @@ impl CreateEmbed { impl Default for CreateEmbed { /// Creates a builder with default values, setting the `type` to `rich`. fn default() -> CreateEmbed { - let mut map = HashMap::new(); + let mut map = VecMap::new(); map.insert("type", Value::String("rich".to_string())); CreateEmbed(map) @@ -380,7 +380,7 @@ impl From for CreateEmbed { /// [`CreateEmbed::author`]: struct.CreateEmbed.html#method.author /// [`name`]: #method.name #[derive(Clone, Debug, Default)] -pub struct CreateEmbedAuthor(pub HashMap<&'static str, Value>); +pub struct CreateEmbedAuthor(pub VecMap<&'static str, Value>); impl CreateEmbedAuthor { /// Set the URL of the author's icon. @@ -413,7 +413,7 @@ impl CreateEmbedAuthor { /// [`Embed`]: ../model/struct.Embed.html /// [`CreateEmbed::footer`]: struct.CreateEmbed.html#method.footer #[derive(Clone, Debug, Default)] -pub struct CreateEmbedFooter(pub HashMap<&'static str, Value>); +pub struct CreateEmbedFooter(pub VecMap<&'static str, Value>); impl CreateEmbedFooter { /// Set the icon URL's value. This only supports HTTP(S). diff --git a/src/builder/create_invite.rs b/src/builder/create_invite.rs index d91a0e63232..5cc0662617c 100644 --- a/src/builder/create_invite.rs +++ b/src/builder/create_invite.rs @@ -1,7 +1,7 @@ use internal::prelude::*; use serde_json::Value; -use std::collections::HashMap; use std::default::Default; +use super::VecMap; /// A builder to create a [`RichInvite`] for use via [`GuildChannel::create_invite`]. /// @@ -62,7 +62,7 @@ use std::default::Default; /// [`GuildChannel::create_invite`]: ../model/struct.GuildChannel.html#method.create_invite /// [`RichInvite`]: ../model/struct.Invite.html #[derive(Clone, Debug)] -pub struct CreateInvite(pub HashMap<&'static str, Value>); +pub struct CreateInvite(pub VecMap<&'static str, Value>); impl CreateInvite { /// The duration that the invite will be valid for. @@ -207,7 +207,7 @@ impl Default for CreateInvite { /// let invite_builder = CreateInvite::default(); /// ``` fn default() -> CreateInvite { - let mut map = HashMap::new(); + let mut map = VecMap::new(); map.insert("validate", Value::Null); CreateInvite(map) diff --git a/src/builder/create_message.rs b/src/builder/create_message.rs index a8f22f9dee2..4e1bd4b204b 100644 --- a/src/builder/create_message.rs +++ b/src/builder/create_message.rs @@ -1,8 +1,7 @@ use internal::prelude::*; use model::channel::ReactionType; -use std::collections::HashMap; use std::fmt::Display; -use super::CreateEmbed; +use super::{CreateEmbed, VecMap}; use utils; /// A builder to specify the contents of an [`http::send_message`] request, @@ -41,7 +40,7 @@ use utils; /// [`embed`]: #method.embed /// [`http::send_message`]: ../http/fn.send_message.html #[derive(Clone, Debug)] -pub struct CreateMessage(pub HashMap<&'static str, Value>, pub Option>); +pub struct CreateMessage(pub VecMap<&'static str, Value>, pub Option>); impl CreateMessage { /// Set the content of the message. @@ -56,7 +55,7 @@ impl CreateMessage { /// Set an embed for the message. pub fn embed(mut self, f: F) -> Self where F: FnOnce(CreateEmbed) -> CreateEmbed { - let map = utils::hashmap_to_json_map(f(CreateEmbed::default()).0); + let map = utils::vecmap_to_json_map(f(CreateEmbed::default()).0); let embed = Value::Object(map); self.0.insert("embed", embed); @@ -90,7 +89,7 @@ impl Default for CreateMessage { /// [`Message`]: ../model/struct.Message.html /// [`tts`]: #method.tts fn default() -> CreateMessage { - let mut map = HashMap::default(); + let mut map = VecMap::new(); map.insert("tts", Value::Bool(false)); CreateMessage(map, None) diff --git a/src/builder/edit_channel.rs b/src/builder/edit_channel.rs index 96455e2b5b4..fca9a51279b 100644 --- a/src/builder/edit_channel.rs +++ b/src/builder/edit_channel.rs @@ -1,5 +1,5 @@ use internal::prelude::*; -use std::collections::HashMap; +use super::VecMap; /// A builder to edit a [`GuildChannel`] for use via [`GuildChannel::edit`] /// @@ -19,7 +19,7 @@ use std::collections::HashMap; /// [`GuildChannel`]: ../model/struct.GuildChannel.html /// [`GuildChannel::edit`]: ../model/struct.GuildChannel.html#method.edit #[derive(Clone, Debug, Default)] -pub struct EditChannel(pub HashMap<&'static str, Value>); +pub struct EditChannel(pub VecMap<&'static str, Value>); impl EditChannel { /// The bitrate of the channel in bits. diff --git a/src/builder/edit_guild.rs b/src/builder/edit_guild.rs index b15658c8a80..bb03c53460f 100644 --- a/src/builder/edit_guild.rs +++ b/src/builder/edit_guild.rs @@ -1,6 +1,6 @@ use internal::prelude::*; use model::prelude::*; -use std::collections::HashMap; +use super::VecMap; /// A builder to optionally edit certain fields of a [`Guild`]. This is meant /// for usage with [`Guild::edit`]. @@ -12,7 +12,7 @@ use std::collections::HashMap; /// [`Guild`]: ../model/struct.Guild.html /// [Manage Guild]: ../model/permissions/constant.MANAGE_GUILD.html #[derive(Clone, Debug, Default)] -pub struct EditGuild(pub HashMap<&'static str, Value>); +pub struct EditGuild(pub VecMap<&'static str, Value>); impl EditGuild { /// Set the "AFK voice channel" that users are to move to if they have been diff --git a/src/builder/edit_member.rs b/src/builder/edit_member.rs index ee04829b966..f5bf2e4dbd3 100644 --- a/src/builder/edit_member.rs +++ b/src/builder/edit_member.rs @@ -1,6 +1,6 @@ use internal::prelude::*; use model::id::{ChannelId, RoleId}; -use std::collections::HashMap; +use super::VecMap; /// A builder which edits the properties of a [`Member`], to be used in /// conjunction with [`Member::edit`]. @@ -8,7 +8,7 @@ use std::collections::HashMap; /// [`Member`]: ../model/struct.Member.html /// [`Member::edit`]: ../model/struct.Member.html#method.edit #[derive(Clone, Debug, Default)] -pub struct EditMember(pub HashMap<&'static str, Value>); +pub struct EditMember(pub VecMap<&'static str, Value>); impl EditMember { /// Whether to deafen the member. diff --git a/src/builder/edit_profile.rs b/src/builder/edit_profile.rs index d3888044f83..efa682fe0b6 100644 --- a/src/builder/edit_profile.rs +++ b/src/builder/edit_profile.rs @@ -1,12 +1,12 @@ use internal::prelude::*; -use std::collections::HashMap; +use super::VecMap; /// A builder to edit the current user's settings, to be used in conjunction /// with [`CurrentUser::edit`]. /// /// [`CurrentUser::edit`]: ../model/struct.CurrentUser.html#method.edit #[derive(Clone, Debug, Default)] -pub struct EditProfile(pub HashMap<&'static str, Value>); +pub struct EditProfile(pub VecMap<&'static str, Value>); impl EditProfile { /// Sets the avatar of the current user. `None` can be passed to remove an diff --git a/src/builder/edit_role.rs b/src/builder/edit_role.rs index 88c876f91ed..059653da777 100644 --- a/src/builder/edit_role.rs +++ b/src/builder/edit_role.rs @@ -1,7 +1,7 @@ use internal::prelude::*; -use std::collections::HashMap; use model::guild::Role; use model::Permissions; +use super::VecMap; /// A builer to create or edit a [`Role`] for use via a number of model methods. /// @@ -40,14 +40,14 @@ use model::Permissions; /// [`Role`]: ../model/struct.Role.html /// [`Role::edit`]: ../model/struct.Role.html#method.edit #[derive(Clone, Debug, Default)] -pub struct EditRole(pub HashMap<&'static str, Value>); +pub struct EditRole(pub VecMap<&'static str, Value>); impl EditRole { /// Creates a new builder with the values of the given [`Role`]. /// /// [`Role`]: ../model/struct.Role.html pub fn new(role: &Role) -> Self { - let mut map = HashMap::new(); + let mut map = VecMap::with_capacity(8); #[cfg(feature = "utils")] { diff --git a/src/builder/execute_webhook.rs b/src/builder/execute_webhook.rs index fb48e96554c..9e7579cb762 100644 --- a/src/builder/execute_webhook.rs +++ b/src/builder/execute_webhook.rs @@ -1,6 +1,6 @@ use serde_json::Value; -use std::collections::HashMap; use std::default::Default; +use super::VecMap; /// A builder to create the inner content of a [`Webhook`]'s execution. /// @@ -47,7 +47,7 @@ use std::default::Default; /// [`Webhook::execute`]: ../model/struct.Webhook.html#method.execute /// [`execute_webhook`]: ../http/fn.execute_webhook.html #[derive(Clone, Debug)] -pub struct ExecuteWebhook(pub HashMap<&'static str, Value>); +pub struct ExecuteWebhook(pub VecMap<&'static str, Value>); impl ExecuteWebhook { /// Override the default avatar of the webhook with an image URL. @@ -179,7 +179,7 @@ impl Default for ExecuteWebhook { /// [`Webhook`]: ../model/struct.Webhook.html /// [`tts`]: #method.tts fn default() -> ExecuteWebhook { - let mut map = HashMap::new(); + let mut map = VecMap::new(); map.insert("tts", Value::Bool(false)); ExecuteWebhook(map) diff --git a/src/builder/get_messages.rs b/src/builder/get_messages.rs index 1032a2f95bb..8b347282390 100644 --- a/src/builder/get_messages.rs +++ b/src/builder/get_messages.rs @@ -1,5 +1,5 @@ use model::id::MessageId; -use std::collections::HashMap; +use super::VecMap; /// Builds a request for a request to the API to retrieve messages. /// @@ -50,7 +50,7 @@ use std::collections::HashMap; /// /// [`GuildChannel::messages`]: ../model/struct.GuildChannel.html#method.messages #[derive(Clone, Debug, Default)] -pub struct GetMessages(pub HashMap<&'static str, u64>); +pub struct GetMessages(pub VecMap<&'static str, u64>); impl GetMessages { /// Indicates to retrieve the messages after a specific message, given by diff --git a/src/builder/mod.rs b/src/builder/mod.rs index 3c59c0d26e5..adb44a18a84 100644 --- a/src/builder/mod.rs +++ b/src/builder/mod.rs @@ -15,7 +15,9 @@ mod edit_profile; mod edit_role; mod execute_webhook; mod get_messages; +mod vec_map; +pub use self::vec_map::VecMap; pub use self::create_embed::{CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter}; pub use self::create_invite::CreateInvite; pub use self::create_message::CreateMessage; diff --git a/src/builder/vec_map.rs b/src/builder/vec_map.rs new file mode 100644 index 00000000000..786bf1443c3 --- /dev/null +++ b/src/builder/vec_map.rs @@ -0,0 +1,120 @@ +// Most of this is +// shamelessly copied from https://github.com/hyperium/hyper/blob/master/src/header/internals/vec_map.rs + +/// Like `HashMap` but solely uses a vector instead. +/// +/// note: This is for internal use. +#[derive(Clone, Debug, Default)] +pub struct VecMap(Vec<(K, V)>); + +impl VecMap { + pub fn new() -> Self { + VecMap(Vec::new()) + } + + pub fn with_capacity(cap: usize) -> Self { + VecMap(Vec::with_capacity(cap)) + } + + #[inline] + pub fn insert(&mut self, key: K, value: V) { + self.0.push((key, value)); + } + + pub fn remove>(&mut self, key: &Q) -> Option { + self.pos(key).map(|pos| self.0.remove(pos)).map(|entry| entry.1) + } + + pub fn entry(&mut self, key: K) -> Entry { + match self.pos(&key) { + Some(pos) => Entry::Occupied(OccupiedEntry { + vec: &mut self.0, + pos: pos, + }), + None => Entry::Vacant(VacantEntry { + vec: &mut self.0, + key: key, + }) + } + } + + pub fn get + ?Sized>(&self, key: &Q) -> Option<&V> { + self.iter().find(|entry| key == &entry.0).map(|entry| &entry.1) + } + + #[inline] + pub fn iter(&self) -> ::std::slice::Iter<(K, V)> { + self.into_iter() + } + + fn pos + ?Sized>(&self, key: &Q) -> Option { + self.iter().position(|entry| key == &entry.0) + } +} + +impl IntoIterator for VecMap { + type Item = (K, V); + type IntoIter = ::std::vec::IntoIter<(K, V)>; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + +impl<'a, K, V> IntoIterator for &'a VecMap { + type Item = &'a (K, V); + type IntoIter = ::std::slice::Iter<'a, (K, V)>; + + fn into_iter(self) -> Self::IntoIter { + self.0.iter() + } +} + +pub enum Entry<'a, K: 'a, V: 'a> { + Vacant(VacantEntry<'a, K, V>), + Occupied(OccupiedEntry<'a, K, V>) +} + +impl<'a, K, V> Entry<'a, K, V> { + pub fn or_insert(self, val: V) -> &'a mut V { + use self::Entry::*; + + match self { + Vacant(entry) => entry.insert(val), + Occupied(entry) => entry.into_mut(), + } + } + + pub fn or_insert_with V>(self, val: F) -> &'a mut V { + use self::Entry::*; + + match self { + Vacant(entry) => entry.insert(val()), + Occupied(entry) => entry.into_mut(), + } + } +} + +pub struct VacantEntry<'a, K: 'a, V: 'a> { + vec: &'a mut Vec<(K, V)>, + key: K, +} + +impl<'a, K, V> VacantEntry<'a, K, V> { + pub fn insert(self, val: V) -> &'a mut V { + self.vec.push((self.key, val)); + let pos = self.vec.len() - 1; + &mut self.vec[pos].1 + } +} + +pub struct OccupiedEntry<'a, K: 'a, V: 'a> { + vec: &'a mut Vec<(K, V)>, + pos: usize, +} + +impl<'a, K, V> OccupiedEntry<'a, K, V> { + pub fn into_mut(self) -> &'a mut V { + &mut self.vec[self.pos].1 + } +} \ No newline at end of file diff --git a/src/client/context.rs b/src/client/context.rs index b15e172437c..a7bd903c8e3 100644 --- a/src/client/context.rs +++ b/src/client/context.rs @@ -10,7 +10,7 @@ use builder::EditProfile; #[cfg(feature = "builder")] use internal::prelude::*; #[cfg(feature = "builder")] -use std::collections::HashMap; +use builder::VecMap; #[cfg(all(feature = "builder", feature = "cache"))] use super::CACHE; #[cfg(feature = "builder")] @@ -85,7 +85,7 @@ impl Context { /// ``` #[cfg(feature = "builder")] pub fn edit_profile EditProfile>(&self, f: F) -> Result { - let mut map = HashMap::with_capacity(2); + let mut map = VecMap::with_capacity(2); feature_cache! { { @@ -107,7 +107,7 @@ impl Context { } } - let edited = utils::hashmap_to_json_map(f(EditProfile(map)).0); + let edited = utils::vecmap_to_json_map(f(EditProfile(map)).0); http::edit_profile(&edited) } diff --git a/src/model/channel/channel_category.rs b/src/model/channel/channel_category.rs index f6df3429dd2..7d6c900c1bd 100644 --- a/src/model/channel/channel_category.rs +++ b/src/model/channel/channel_category.rs @@ -1,4 +1,6 @@ use model::prelude::*; +#[cfg(feature = "model")] +use builder::VecMap; #[cfg(all(feature = "builder", feature = "model"))] use builder::EditChannel; @@ -93,12 +95,12 @@ impl ChannelCategory { } } - let mut map = HashMap::new(); + let mut map = VecMap::new(); map.insert("name", Value::String(self.name.clone())); map.insert("position", Value::Number(Number::from(self.position))); map.insert("type", Value::String(self.kind.name().to_string())); - let map = serenity_utils::hashmap_to_json_map(f(EditChannel(map)).0); + let map = serenity_utils::vecmap_to_json_map(f(EditChannel(map)).0); http::edit_channel(self.id.0, &map).map(|channel| { let GuildChannel { diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs index 7ec6dfe7fb0..8ebdf2ad9de 100644 --- a/src/model/channel/channel_id.rs +++ b/src/model/channel/channel_id.rs @@ -191,7 +191,7 @@ impl ChannelId { /// [Manage Channel]: permissions/constant.MANAGE_CHANNELS.html #[inline] pub fn edit EditChannel>(&self, f: F) -> Result { - let map = utils::hashmap_to_json_map(f(EditChannel::default()).0); + let map = utils::vecmap_to_json_map(f(EditChannel::default()).0); http::edit_channel(self.0, &map) } @@ -219,7 +219,7 @@ impl ChannelId { where F: FnOnce(CreateMessage) -> CreateMessage, M: Into { let msg = f(CreateMessage::default()); - if let Some(content) = msg.0.get("content") { + if let Some(content) = msg.0.get(&"content") { if let Value::String(ref content) = *content { if let Some(length_over) = Message::overflow_length(content) { return Err(Error::Model(ModelError::MessageTooLong(length_over))); @@ -227,7 +227,7 @@ impl ChannelId { } } - let map = utils::hashmap_to_json_map(msg.0); + let map = utils::vecmap_to_json_map(msg.0); http::edit_message(self.0, message_id.into().0, &Value::Object(map)) } @@ -282,13 +282,13 @@ impl ChannelId { pub fn messages(&self, f: F) -> Result> where F: FnOnce(GetMessages) -> GetMessages { let mut map = f(GetMessages::default()).0; - let mut query = format!("?limit={}", map.remove("limit").unwrap_or(50)); + let mut query = format!("?limit={}", map.remove(&"limit").unwrap_or(50)); - if let Some(after) = map.remove("after") { + if let Some(after) = map.remove(&"after") { write!(query, "&after={}", after)?; - } else if let Some(around) = map.remove("around") { + } else if let Some(around) = map.remove(&"around") { write!(query, "&around={}", around)?; - } else if let Some(before) = map.remove("before") { + } else if let Some(before) = map.remove(&"before") { write!(query, "&before={}", before)?; } @@ -453,7 +453,7 @@ impl ChannelId { where F: FnOnce(CreateMessage) -> CreateMessage, T: Into> { let mut msg = f(CreateMessage::default()); - if let Some(content) = msg.0.get("content") { + if let Some(content) = msg.0.get(&"content") { if let Value::String(ref content) = *content { if let Some(length_over) = Message::overflow_length(content) { return Err(Error::Model(ModelError::MessageTooLong(length_over))); @@ -461,8 +461,8 @@ impl ChannelId { } } - let _ = msg.0.remove("embed"); - let map = utils::hashmap_to_json_map(msg.0); + let _ = msg.0.remove(&"embed"); + let map = utils::vecmap_to_json_map(msg.0); http::send_files(self.0, files, map) } @@ -489,7 +489,7 @@ impl ChannelId { pub fn send_message(&self, f: F) -> Result where F: FnOnce(CreateMessage) -> CreateMessage { let msg = f(CreateMessage::default()); - let map = utils::hashmap_to_json_map(msg.0); + let map = utils::vecmap_to_json_map(msg.0); Message::check_content_length(&map)?; Message::check_embed_length(&map)?; diff --git a/src/model/channel/embed.rs b/src/model/channel/embed.rs index 3cbe5ace674..5359ed51754 100644 --- a/src/model/channel/embed.rs +++ b/src/model/channel/embed.rs @@ -88,7 +88,7 @@ impl Embed { #[inline] pub fn fake(f: F) -> Value where F: FnOnce(CreateEmbed) -> CreateEmbed { - let map = utils::hashmap_to_json_map(f(CreateEmbed::default()).0); + let map = utils::vecmap_to_json_map(f(CreateEmbed::default()).0); Value::Object(map) } diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs index 9b8371d2876..c59831417d6 100644 --- a/src/model/channel/guild_channel.rs +++ b/src/model/channel/guild_channel.rs @@ -4,7 +4,7 @@ use model::prelude::*; #[cfg(all(feature = "cache", feature = "model"))] use CACHE; #[cfg(feature = "model")] -use builder::{CreateInvite, CreateMessage, EditChannel, GetMessages}; +use builder::{VecMap, CreateInvite, CreateMessage, EditChannel, GetMessages}; #[cfg(feature = "model")] use http::{self, AttachmentType}; #[cfg(all(feature = "cache", feature = "model"))] @@ -116,7 +116,7 @@ impl GuildChannel { } } - let map = serenity_utils::hashmap_to_json_map(f(CreateInvite::default()).0); + let map = serenity_utils::vecmap_to_json_map(f(CreateInvite::default()).0); http::create_invite(self.id.0, &map) } @@ -312,12 +312,12 @@ impl GuildChannel { } } - let mut map = HashMap::new(); + let mut map = VecMap::new(); map.insert("name", Value::String(self.name.clone())); map.insert("position", Value::Number(Number::from(self.position))); map.insert("type", Value::String(self.kind.name().to_string())); - let edited = serenity_utils::hashmap_to_json_map(f(EditChannel(map)).0); + let edited = serenity_utils::vecmap_to_json_map(f(EditChannel(map)).0); match http::edit_channel(self.id.0, &edited) { Ok(channel) => { diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs index f139aab3300..315dc242e89 100644 --- a/src/model/channel/message.rs +++ b/src/model/channel/message.rs @@ -229,7 +229,7 @@ impl Message { builder = builder.tts(true); } - let map = serenity_utils::hashmap_to_json_map(f(builder).0); + let map = serenity_utils::vecmap_to_json_map(f(builder).0); match http::edit_message(self.channel_id.0, self.id.0, &Value::Object(map)) { Ok(edited) => { diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index 2453fc6953a..48ad55b63e1 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -174,7 +174,7 @@ impl GuildId { /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html #[inline] pub fn create_role EditRole>(&self, f: F) -> Result { - let map = utils::hashmap_to_json_map(f(EditRole::default()).0); + let map = utils::vecmap_to_json_map(f(EditRole::default()).0); http::create_role(self.0, &map) } @@ -237,7 +237,7 @@ impl GuildId { /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[inline] pub fn edit EditGuild>(&mut self, f: F) -> Result { - let map = utils::hashmap_to_json_map(f(EditGuild::default()).0); + let map = utils::vecmap_to_json_map(f(EditGuild::default()).0); http::edit_guild(self.0, &map) } @@ -276,7 +276,7 @@ impl GuildId { #[inline] pub fn edit_member(&self, user_id: U, f: F) -> Result<()> where F: FnOnce(EditMember) -> EditMember, U: Into { - let map = utils::hashmap_to_json_map(f(EditMember::default()).0); + let map = utils::vecmap_to_json_map(f(EditMember::default()).0); http::edit_member(self.0, user_id.into().0, &map) } @@ -312,7 +312,7 @@ impl GuildId { #[inline] pub fn edit_role(&self, role_id: R, f: F) -> Result where F: FnOnce(EditRole) -> EditRole, R: Into { - let map = utils::hashmap_to_json_map(f(EditRole::default()).0); + let map = utils::vecmap_to_json_map(f(EditRole::default()).0); http::edit_role(self.0, role_id.into().0, &map) } diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index 166eb55399d..f2e798aba07 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -109,7 +109,7 @@ impl Member { self.roles.extend_from_slice(role_ids); let builder = EditMember::default().roles(&self.roles); - let map = utils::hashmap_to_json_map(builder.0); + let map = utils::vecmap_to_json_map(builder.0); match http::edit_member(self.guild_id.0, self.user.read().id.0, &map) { Ok(()) => Ok(()), @@ -227,7 +227,7 @@ impl Member { /// [`EditMember`]: ../builder/struct.EditMember.html #[cfg(feature = "cache")] pub fn edit EditMember>(&self, f: F) -> Result<()> { - let map = utils::hashmap_to_json_map(f(EditMember::default()).0); + let map = utils::vecmap_to_json_map(f(EditMember::default()).0); http::edit_member(self.guild_id.0, self.user.read().id.0, &map) } @@ -352,7 +352,7 @@ impl Member { self.roles.retain(|r| !role_ids.contains(r)); let builder = EditMember::default().roles(&self.roles); - let map = utils::hashmap_to_json_map(builder.0); + let map = utils::vecmap_to_json_map(builder.0); match http::edit_member(self.guild_id.0, self.user.read().id.0, &map) { Ok(()) => Ok(()), diff --git a/src/model/invite.rs b/src/model/invite.rs index ed3455cc7c4..5429656b048 100644 --- a/src/model/invite.rs +++ b/src/model/invite.rs @@ -75,7 +75,7 @@ impl Invite { } } - let map = utils::hashmap_to_json_map(f(CreateInvite::default()).0); + let map = utils::vecmap_to_json_map(f(CreateInvite::default()).0); http::create_invite(channel_id.0, &map) } diff --git a/src/model/user.rs b/src/model/user.rs index 414f7416391..fc1ec3a5377 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -10,7 +10,7 @@ use model::misc::Mentionable; #[cfg(all(feature = "cache", feature = "model"))] use CACHE; #[cfg(feature = "model")] -use builder::{CreateMessage, EditProfile}; +use builder::{VecMap, CreateMessage, EditProfile}; #[cfg(feature = "model")] use chrono::NaiveDateTime; #[cfg(feature = "model")] @@ -90,14 +90,14 @@ impl CurrentUser { /// ``` pub fn edit(&mut self, f: F) -> Result<()> where F: FnOnce(EditProfile) -> EditProfile { - let mut map = HashMap::new(); + let mut map = VecMap::new(); map.insert("username", Value::String(self.name.clone())); if let Some(email) = self.email.as_ref() { map.insert("email", Value::String(email.clone())); } - let map = utils::hashmap_to_json_map(f(EditProfile(map)).0); + let map = utils::vecmap_to_json_map(f(EditProfile(map)).0); match http::edit_profile(&map) { Ok(new) => { diff --git a/src/model/webhook.rs b/src/model/webhook.rs index d83450226a3..d9a4483352f 100644 --- a/src/model/webhook.rs +++ b/src/model/webhook.rs @@ -187,7 +187,7 @@ impl Webhook { wait: bool, f: F) -> Result> { - let map = utils::hashmap_to_json_map(f(ExecuteWebhook::default()).0); + let map = utils::vecmap_to_json_map(f(ExecuteWebhook::default()).0); http::execute_webhook(self.id.0, &self.token, wait, &map) } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 907d3a37830..74d37de4d37 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -11,6 +11,7 @@ pub use self::message_builder::{Content, ContentModifier, MessageBuilder}; #[cfg(feature = "builder")] pub use super::builder; +use builder::VecMap; use base64; use internal::prelude::*; use model::id::EmojiId; @@ -39,6 +40,17 @@ pub fn hashmap_to_json_map(map: HashMap) json_map } +/// Converts a VecMap into a final `serde_json::Map` representation. +pub fn vecmap_to_json_map(map: VecMap) -> Map { + let mut json_map = Map::new(); + + for (key, value) in map { + json_map.insert(key.to_string(), value); + } + + json_map +} + /// Determines if a name is NSFW. /// /// This checks that the name is either `"nsfw"` or, for names longer than that, diff --git a/tests/test_create_embed.rs b/tests/test_create_embed.rs index 43e4bb9a5ed..e4dec372bb7 100644 --- a/tests/test_create_embed.rs +++ b/tests/test_create_embed.rs @@ -51,7 +51,7 @@ fn test_from_embed() { .title("still a hakase") .url("https://i.imgur.com/XfWpfCV.gif"); - let built = Value::Object(utils::hashmap_to_json_map(builder.0)); + let built = Value::Object(utils::vecmap_to_json_map(builder.0)); let obj = json!({ "color": 0xFF0011,