Skip to content

Commit

Permalink
Eliminate VecMap
Browse files Browse the repository at this point in the history
This was made for performance gains in the past, but with the recent transition of the standard library HashMap to a Swiss-table implementation, it no longer suits a purpose.
  • Loading branch information
arqunis committed May 19, 2019
1 parent 84a863c commit 9450d4b
Show file tree
Hide file tree
Showing 23 changed files with 73 additions and 198 deletions.
26 changes: 16 additions & 10 deletions src/builder/create_embed.rs
Expand Up @@ -15,11 +15,15 @@
//! [`ExecuteWebhook::embeds`]: struct.ExecuteWebhook.html#method.embeds
//! [here]: https://discordapp.com/developers/docs/resources/channel#embed-object

use chrono::{DateTime, TimeZone};
use crate::internal::prelude::*;
use crate::model::channel::Embed;
use crate::utils;

use chrono::{DateTime, TimeZone};
use serde_json::{json, Value};
use crate::utils::{self, VecMap};

use std::fmt::Display;
use std::collections::HashMap;

#[cfg(feature = "utils")]
use crate::utils::Colour;
Expand All @@ -36,7 +40,7 @@ use crate::utils::Colour;
/// [`Embed`]: ../model/channel/struct.Embed.html
/// [`ExecuteWebhook::embeds`]: struct.ExecuteWebhook.html#method.embeds
#[derive(Clone, Debug)]
pub struct CreateEmbed(pub VecMap<&'static str, Value>);
pub struct CreateEmbed(pub HashMap<&'static str, Value>);

impl CreateEmbed {
/// Set the author of the embed.
Expand All @@ -50,7 +54,7 @@ impl CreateEmbed {
let mut author = CreateEmbedAuthor::default();
f(&mut author);

let map = utils::vecmap_to_json_map(author.0);
let map = utils::hashmap_to_json_map(author.0);

self.0.insert("author", Value::Object(map));
self
Expand Down Expand Up @@ -167,7 +171,7 @@ impl CreateEmbed {
let mut create_embed_footer = CreateEmbedFooter::default();
f(&mut create_embed_footer);
let footer = create_embed_footer.0;
let map = utils::vecmap_to_json_map(footer);
let map = utils::hashmap_to_json_map(footer);

self.0.insert("footer", Value::Object(map));
self
Expand Down Expand Up @@ -333,7 +337,9 @@ impl CreateEmbed {
/// [`image`]: #method.image
#[inline]
pub fn attachment<S: ToString>(&mut self, filename: S) -> &mut Self {
self.image(format_args!("attachment://{}", filename));
let mut filename = filename.to_string();
filename.insert_str(0, "attachment://");
self.url_object("image", filename);

self
}
Expand All @@ -342,7 +348,7 @@ impl CreateEmbed {
impl Default for CreateEmbed {
/// Creates a builder with default values, setting the `type` to `rich`.
fn default() -> CreateEmbed {
let mut map = VecMap::new();
let mut map = HashMap::new();
map.insert("type", Value::String("rich".to_string()));

CreateEmbed(map)
Expand Down Expand Up @@ -426,7 +432,7 @@ impl From<Embed> for CreateEmbed {
/// [`CreateEmbed::author`]: struct.CreateEmbed.html#method.author
/// [`name`]: #method.name
#[derive(Clone, Debug, Default)]
pub struct CreateEmbedAuthor(pub VecMap<&'static str, Value>);
pub struct CreateEmbedAuthor(pub HashMap<&'static str, Value>);

impl CreateEmbedAuthor {
/// Set the URL of the author's icon.
Expand Down Expand Up @@ -456,7 +462,7 @@ impl CreateEmbedAuthor {
/// [`Embed`]: ../model/channel/struct.Embed.html
/// [`CreateEmbed::footer`]: struct.CreateEmbed.html#method.footer
#[derive(Clone, Debug, Default)]
pub struct CreateEmbedFooter(pub VecMap<&'static str, Value>);
pub struct CreateEmbedFooter(pub HashMap<&'static str, Value>);

impl CreateEmbedFooter {
/// Set the icon URL's value. This only supports HTTP(S).
Expand Down Expand Up @@ -558,7 +564,7 @@ mod test {
builder.title("still a hakase");
builder.url("https://i.imgur.com/XfWpfCV.gif");

let built = Value::Object(utils::vecmap_to_json_map(builder.0));
let built = Value::Object(utils::hashmap_to_json_map(builder.0));

let obj = json!({
"color": 0xFF0011,
Expand Down
6 changes: 3 additions & 3 deletions src/builder/create_invite.rs
@@ -1,5 +1,5 @@
use crate::internal::prelude::*;
use crate::utils::VecMap;
use std::collections::HashMap;
use serde_json::Value;

/// A builder to create a [`RichInvite`] for use via [`GuildChannel::create_invite`].
Expand Down Expand Up @@ -67,7 +67,7 @@ use serde_json::Value;
/// [`GuildChannel::create_invite`]: ../model/channel/struct.GuildChannel.html#method.create_invite
/// [`RichInvite`]: ../model/invite/struct.RichInvite.html
#[derive(Clone, Debug)]
pub struct CreateInvite(pub VecMap<&'static str, Value>);
pub struct CreateInvite(pub HashMap<&'static str, Value>);

impl CreateInvite {
/// The duration that the invite will be valid for.
Expand Down Expand Up @@ -226,7 +226,7 @@ impl Default for CreateInvite {
/// let invite_builder = CreateInvite::default();
/// ```
fn default() -> CreateInvite {
let mut map = VecMap::new();
let mut map = HashMap::new();
map.insert("validate", Value::Null);

CreateInvite(map)
Expand Down
10 changes: 6 additions & 4 deletions src/builder/create_message.rs
Expand Up @@ -2,7 +2,9 @@ use crate::internal::prelude::*;
use crate::http::AttachmentType;
use crate::model::channel::ReactionType;
use super::CreateEmbed;
use crate::utils::{self, VecMap};
use crate::utils;

use std::collections::HashMap;

/// A builder to specify the contents of an [`http::send_message`] request,
/// primarily meant for use through [`ChannelId::send_message`].
Expand Down Expand Up @@ -51,7 +53,7 @@ use crate::utils::{self, VecMap};
/// [`embed`]: #method.embed
/// [`http::send_message`]: ../http/fn.send_message.html
#[derive(Clone, Debug)]
pub struct CreateMessage<'a>(pub VecMap<&'static str, Value>, pub Option<Vec<ReactionType>>, pub Vec<AttachmentType<'a>>);
pub struct CreateMessage<'a>(pub HashMap<&'static str, Value>, pub Option<Vec<ReactionType>>, pub Vec<AttachmentType<'a>>);

impl<'a> CreateMessage<'a> {
/// Set the content of the message.
Expand All @@ -72,7 +74,7 @@ impl<'a> CreateMessage<'a> {
where F: FnOnce(&mut CreateEmbed) -> &mut CreateEmbed {
let mut embed = CreateEmbed::default();
f(&mut embed);
let map = utils::vecmap_to_json_map(embed.0);
let map = utils::hashmap_to_json_map(embed.0);
let embed = Value::Object(map);

self.0.insert("embed", embed);
Expand Down Expand Up @@ -129,7 +131,7 @@ impl<'a> Default for CreateMessage<'a> {
/// [`Message`]: ../model/channel/struct.Message.html
/// [`tts`]: #method.tts
fn default() -> CreateMessage<'a> {
let mut map = VecMap::new();
let mut map = HashMap::new();
map.insert("tts", Value::Bool(false));

CreateMessage(map, None, Vec::new())
Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_channel.rs
@@ -1,6 +1,6 @@
use crate::internal::prelude::*;
use crate::utils::VecMap;
use crate::model::id::ChannelId;
use std::collections::HashMap;

/// A builder to edit a [`GuildChannel`] for use via [`GuildChannel::edit`]
///
Expand All @@ -20,7 +20,7 @@ use crate::model::id::ChannelId;
/// [`GuildChannel`]: ../model/channel/struct.GuildChannel.html
/// [`GuildChannel::edit`]: ../model/channel/struct.GuildChannel.html#method.edit
#[derive(Clone, Debug, Default)]
pub struct EditChannel(pub VecMap<&'static str, Value>);
pub struct EditChannel(pub HashMap<&'static str, Value>);

impl EditChannel {
/// The bitrate of the channel in bits.
Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_guild.rs
@@ -1,6 +1,6 @@
use crate::internal::prelude::*;
use crate::model::prelude::*;
use crate::utils::VecMap;
use std::collections::HashMap;

/// A builder to optionally edit certain fields of a [`Guild`]. This is meant
/// for usage with [`Guild::edit`].
Expand All @@ -12,7 +12,7 @@ use crate::utils::VecMap;
/// [`Guild`]: ../model/guild/struct.Guild.html
/// [Manage Guild]: ../model/permissions/struct.Permissions.html#associatedconstant.MANAGE_GUILD
#[derive(Clone, Debug, Default)]
pub struct EditGuild(pub VecMap<&'static str, 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
Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_member.rs
@@ -1,14 +1,14 @@
use crate::internal::prelude::*;
use crate::model::id::{ChannelId, RoleId};
use crate::utils::VecMap;
use std::collections::HashMap;

/// A builder which edits the properties of a [`Member`], to be used in
/// conjunction with [`Member::edit`].
///
/// [`Member`]: ../model/guild/struct.Member.html
/// [`Member::edit`]: ../model/guild/struct.Member.html#method.edit
#[derive(Clone, Debug, Default)]
pub struct EditMember(pub VecMap<&'static str, Value>);
pub struct EditMember(pub HashMap<&'static str, Value>);

impl EditMember {
/// Whether to deafen the member.
Expand Down
8 changes: 5 additions & 3 deletions src/builder/edit_message.rs
@@ -1,6 +1,8 @@
use crate::internal::prelude::*;
use super::CreateEmbed;
use crate::utils::{self, VecMap};
use crate::utils;

use std::collections::HashMap;

/// A builder to specify the fields to edit in an existing message.
///
Expand Down Expand Up @@ -30,7 +32,7 @@ use crate::utils::{self, VecMap};
///
/// [`Message`]: ../model/channel/struct.Message.html
#[derive(Clone, Debug, Default)]
pub struct EditMessage(pub VecMap<&'static str, Value>);
pub struct EditMessage(pub HashMap<&'static str, Value>);

impl EditMessage {
/// Set the content of the message.
Expand All @@ -47,7 +49,7 @@ impl EditMessage {
where F: FnOnce(&mut CreateEmbed) -> &mut CreateEmbed {
let mut create_embed = CreateEmbed::default();
f(&mut create_embed);
let map = utils::vecmap_to_json_map(create_embed.0);
let map = utils::hashmap_to_json_map(create_embed.0);
let embed = Value::Object(map);

self.0.insert("embed", embed);
Expand Down
4 changes: 2 additions & 2 deletions src/builder/edit_profile.rs
@@ -1,12 +1,12 @@
use crate::internal::prelude::*;
use crate::utils::VecMap;
use std::collections::HashMap;

/// A builder to edit the current user's settings, to be used in conjunction
/// with [`CurrentUser::edit`].
///
/// [`CurrentUser::edit`]: ../model/user/struct.CurrentUser.html#method.edit
#[derive(Clone, Debug, Default)]
pub struct EditProfile(pub VecMap<&'static str, Value>);
pub struct EditProfile(pub HashMap<&'static str, Value>);

impl EditProfile {
/// Sets the avatar of the current user. `None` can be passed to remove an
Expand Down
6 changes: 3 additions & 3 deletions src/builder/edit_role.rs
Expand Up @@ -3,7 +3,7 @@ use crate::model::{
guild::Role,
Permissions
};
use crate::utils::VecMap;
use std::collections::HashMap;

/// A builder to create or edit a [`Role`] for use via a number of model methods.
///
Expand Down Expand Up @@ -44,14 +44,14 @@ use crate::utils::VecMap;
/// [`Role`]: ../model/guild/struct.Role.html
/// [`Role::edit`]: ../model/guild/struct.Role.html#method.edit
#[derive(Clone, Debug, Default)]
pub struct EditRole(pub VecMap<&'static str, Value>);
pub struct EditRole(pub HashMap<&'static str, Value>);

impl EditRole {
/// Creates a new builder with the values of the given [`Role`].
///
/// [`Role`]: ../model/guild/struct.Role.html
pub fn new(role: &Role) -> Self {
let mut map = VecMap::with_capacity(8);
let mut map = HashMap::with_capacity(8);

#[cfg(feature = "utils")]
{
Expand Down
6 changes: 3 additions & 3 deletions src/builder/execute_webhook.rs
@@ -1,5 +1,5 @@
use serde_json::Value;
use crate::utils::VecMap;
use std::collections::HashMap;

/// A builder to create the inner content of a [`Webhook`]'s execution.
///
Expand Down Expand Up @@ -48,7 +48,7 @@ use crate::utils::VecMap;
/// [`Webhook::execute`]: ../model/webhook/struct.Webhook.html#method.execute
/// [`execute_webhook`]: ../http/raw/struct.Http.html#method.execute_webhook
#[derive(Clone, Debug)]
pub struct ExecuteWebhook(pub VecMap<&'static str, Value>);
pub struct ExecuteWebhook(pub HashMap<&'static str, Value>);

impl ExecuteWebhook {
/// Override the default avatar of the webhook with an image URL.
Expand Down Expand Up @@ -195,7 +195,7 @@ impl Default for ExecuteWebhook {
/// [`Webhook`]: ../model/webhook/struct.Webhook.html
/// [`tts`]: #method.tts
fn default() -> ExecuteWebhook {
let mut map = VecMap::new();
let mut map = HashMap::new();
map.insert("tts", Value::Bool(false));

ExecuteWebhook(map)
Expand Down
4 changes: 2 additions & 2 deletions src/builder/get_messages.rs
@@ -1,5 +1,5 @@
use crate::model::id::MessageId;
use crate::utils::VecMap;
use std::collections::HashMap;

/// Builds a request for a request to the API to retrieve messages.
///
Expand Down Expand Up @@ -48,7 +48,7 @@ use crate::utils::VecMap;
///
/// [`GuildChannel::messages`]: ../model/channel/struct.GuildChannel.html#method.messages
#[derive(Clone, Debug, Default)]
pub struct GetMessages(pub VecMap<&'static str, u64>);
pub struct GetMessages(pub HashMap<&'static str, u64>);

impl GetMessages {
/// Indicates to retrieve the messages after a specific message, given by
Expand Down
8 changes: 4 additions & 4 deletions src/model/channel/channel_category.rs
@@ -1,11 +1,11 @@
use crate::{model::prelude::*};
use crate::model::prelude::*;

#[cfg(feature = "client")]
use crate::client::Context;
#[cfg(all(feature = "builder", feature = "model"))]
use crate::builder::EditChannel;
#[cfg(all(feature = "model", feature = "utils"))]
use crate::utils::{self as serenity_utils, VecMap};
use crate::utils as serenity_utils;
#[cfg(feature = "http")]
use crate::http::Http;

Expand Down Expand Up @@ -102,14 +102,14 @@ impl ChannelCategory {
}
}

let mut map = VecMap::new();
let mut map = HashMap::new();
map.insert("name", Value::String(self.name.clone()));
map.insert("position", Value::Number(Number::from(self.position)));


let mut edit_channel = EditChannel::default();
f(&mut edit_channel);
let map = serenity_utils::vecmap_to_json_map(edit_channel.0);
let map = serenity_utils::hashmap_to_json_map(edit_channel.0);

context.http.edit_channel(self.id.0, &map).map(|channel| {
let GuildChannel {
Expand Down
8 changes: 4 additions & 4 deletions src/model/channel/channel_id.rs
Expand Up @@ -262,7 +262,7 @@ impl ChannelId {
let mut channel = EditChannel::default();
f(&mut channel);

let map = utils::vecmap_to_json_map(channel.0);
let map = utils::hashmap_to_json_map(channel.0);

http.as_ref().edit_channel(self.0, &map)
}
Expand Down Expand Up @@ -306,7 +306,7 @@ impl ChannelId {
}
}

let map = utils::vecmap_to_json_map(msg.0);
let map = utils::hashmap_to_json_map(msg.0);

http.as_ref().edit_message(self.0, message_id.0, &Value::Object(map))
}
Expand Down Expand Up @@ -617,7 +617,7 @@ impl ChannelId {
msg.0.insert("payload_json", json!({ "embed": e }));
}

let map = utils::vecmap_to_json_map(msg.0.clone());
let map = utils::hashmap_to_json_map(msg.0.clone());
http.as_ref().send_files(self.0, files, map)
}

Expand Down Expand Up @@ -652,7 +652,7 @@ impl ChannelId {
}
}

let map = utils::vecmap_to_json_map(msg.0.clone());
let map = utils::hashmap_to_json_map(msg.0.clone());

Message::check_content_length(&map)?;
Message::check_embed_length(&map)?;
Expand Down
2 changes: 1 addition & 1 deletion src/model/channel/embed.rs
Expand Up @@ -95,7 +95,7 @@ impl Embed {
where F: FnOnce(&mut CreateEmbed) -> &mut CreateEmbed {
let mut create_embed = CreateEmbed::default();
f(&mut create_embed);
let map = utils::vecmap_to_json_map(create_embed.0);
let map = utils::hashmap_to_json_map(create_embed.0);

Value::Object(map)
}
Expand Down

0 comments on commit 9450d4b

Please sign in to comment.