aboutsummaryrefslogtreecommitdiff
path: root/src/builder/edit_role.rs
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-10-18 08:34:06 -0700
committerZeyla Hellyer <[email protected]>2017-10-18 08:34:06 -0700
commit9908999a6bae1585bb70b7814f13b49bf99b6c32 (patch)
treed789f716400502ae0d124933f5c4d927867e7033 /src/builder/edit_role.rs
parentFix some compilation feature targets, fix lints (diff)
downloadserenity-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_role.rs')
-rw-r--r--src/builder/edit_role.rs62
1 files changed, 24 insertions, 38 deletions
diff --git a/src/builder/edit_role.rs b/src/builder/edit_role.rs
index 0ef1b35..0906329 100644
--- a/src/builder/edit_role.rs
+++ b/src/builder/edit_role.rs
@@ -1,6 +1,7 @@
use std::default::Default;
use internal::prelude::*;
use model::{permissions, Permissions, Role};
+use std::collections::HashMap;
/// A builer to create or edit a [`Role`] for use via a number of model methods.
///
@@ -39,48 +40,38 @@ use model::{permissions, Permissions, Role};
/// [`Role`]: ../model/struct.Role.html
/// [`Role::edit`]: ../model/struct.Role.html#method.edit
#[derive(Clone, Debug)]
-pub struct EditRole(pub JsonMap);
+pub struct EditRole(pub HashMap<&'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 = Map::new();
+ let mut map = HashMap::new();
#[cfg(feature = "utils")]
{
- map.insert(
- "color".to_string(),
- Value::Number(Number::from(role.colour.0)),
- );
+ map.insert("color", Value::Number(Number::from(role.colour.0)));
}
#[cfg(not(feature = "utils"))]
{
- map.insert("color".to_string(), Value::Number(Number::from(role.colour)));
+ map.insert("color", Value::Number(Number::from(role.colour)));
}
- map.insert("hoist".to_string(), Value::Bool(role.hoist));
- map.insert("managed".to_string(), Value::Bool(role.managed));
- map.insert("mentionable".to_string(), Value::Bool(role.mentionable));
- map.insert("name".to_string(), Value::String(role.name.clone()));
- map.insert(
- "permissions".to_string(),
- Value::Number(Number::from(role.permissions.bits())),
- );
- map.insert(
- "position".to_string(),
- Value::Number(Number::from(role.position)),
- );
+ map.insert("hoist", Value::Bool(role.hoist));
+ map.insert("managed", Value::Bool(role.managed));
+ map.insert("mentionable", Value::Bool(role.mentionable));
+ map.insert("name", Value::String(role.name.clone()));
+ map.insert("permissions",Value::Number(Number::from(role.permissions.bits())));
+ map.insert("position", Value::Number(Number::from(role.position)));
EditRole(map)
}
/// Sets the colour of the role.
pub fn colour(mut self, colour: u64) -> Self {
- self.0
- .insert("color".to_string(), Value::Number(Number::from(colour)));
+ self.0.insert("color", Value::Number(Number::from(colour)));
self
}
@@ -88,15 +79,14 @@ impl EditRole {
/// Whether or not to hoist the role above lower-positioned role in the user
/// list.
pub fn hoist(mut self, hoist: bool) -> Self {
- self.0.insert("hoist".to_string(), Value::Bool(hoist));
+ self.0.insert("hoist", Value::Bool(hoist));
self
}
/// Whether or not to make the role mentionable, notifying its users.
pub fn mentionable(mut self, mentionable: bool) -> Self {
- self.0
- .insert("mentionable".to_string(), Value::Bool(mentionable));
+ self.0.insert("mentionable", Value::Bool(mentionable));
self
}
@@ -104,17 +94,14 @@ impl EditRole {
/// The name of the role to set.
pub fn name(mut self, name: &str) -> Self {
self.0
- .insert("name".to_string(), Value::String(name.to_string()));
+ .insert("name", Value::String(name.to_string()));
self
}
/// The set of permissions to assign the role.
pub fn permissions(mut self, permissions: Permissions) -> Self {
- self.0.insert(
- "permissions".to_string(),
- Value::Number(Number::from(permissions.bits())),
- );
+ self.0.insert("permissions", Value::Number(Number::from(permissions.bits())));
self
}
@@ -122,8 +109,7 @@ impl EditRole {
/// The position to assign the role in the role list. This correlates to the
/// role's position in the user list.
pub fn position(mut self, position: u8) -> Self {
- self.0
- .insert("position".to_string(), Value::Number(Number::from(position)));
+ self.0.insert("position", Value::Number(Number::from(position)));
self
}
@@ -143,15 +129,15 @@ impl Default for EditRole {
///
/// [general permissions set]: ../model/permissions/constant.PRESET_GENERAL.html
fn default() -> EditRole {
- let mut map = Map::new();
+ let mut map = HashMap::new();
let permissions = Number::from(permissions::PRESET_GENERAL.bits());
- map.insert("color".to_string(), Value::Number(Number::from(10_070_709)));
- map.insert("hoist".to_string(), Value::Bool(false));
- map.insert("mentionable".to_string(), Value::Bool(false));
- map.insert("name".to_string(), Value::String("new role".to_string()));
- map.insert("permissions".to_string(), Value::Number(permissions));
- map.insert("position".to_string(), Value::Number(Number::from(1)));
+ map.insert("color", Value::Number(Number::from(10_070_709)));
+ map.insert("hoist", Value::Bool(false));
+ map.insert("mentionable", Value::Bool(false));
+ map.insert("name", Value::String("new role".to_string()));
+ map.insert("permissions", Value::Number(permissions));
+ map.insert("position", Value::Number(Number::from(1)));
EditRole(map)
}