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_profile.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_profile.rs')
| -rw-r--r-- | src/builder/edit_profile.rs | 20 |
1 files changed, 7 insertions, 13 deletions
diff --git a/src/builder/edit_profile.rs b/src/builder/edit_profile.rs index ae0bbd5..648bfbf 100644 --- a/src/builder/edit_profile.rs +++ b/src/builder/edit_profile.rs @@ -1,11 +1,12 @@ use internal::prelude::*; +use std::collections::HashMap; /// 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 JsonMap); +pub struct EditProfile(pub HashMap<&'static str, Value>); impl EditProfile { /// Sets the avatar of the current user. `None` can be passed to remove an @@ -44,8 +45,7 @@ impl EditProfile { /// [`utils::read_image`]: ../fn.read_image.html pub fn avatar(mut self, avatar: Option<&str>) -> Self { let avatar = avatar.map_or(Value::Null, |x| Value::String(x.to_string())); - - self.0.insert("avatar".to_string(), avatar); + self.0.insert("avatar", avatar); self } @@ -61,8 +61,7 @@ impl EditProfile { /// /// [provided]: #method.password pub fn email(mut self, email: &str) -> Self { - self.0 - .insert("email".to_string(), Value::String(email.to_string())); + self.0.insert("email", Value::String(email.to_string())); self } @@ -74,10 +73,7 @@ impl EditProfile { /// /// [provided]: #method.password pub fn new_password(mut self, new_password: &str) -> Self { - self.0.insert( - "new_password".to_string(), - Value::String(new_password.to_string()), - ); + self.0.insert("new_password", Value::String(new_password.to_string())); self } @@ -88,8 +84,7 @@ impl EditProfile { /// [modifying the password]: #method.new_password /// [modifying the associated email address]: #method.email pub fn password(mut self, password: &str) -> Self { - self.0 - .insert("password".to_string(), Value::String(password.to_string())); + self.0.insert("password", Value::String(password.to_string())); self } @@ -101,8 +96,7 @@ impl EditProfile { /// If there are no available discriminators with the requested username, /// an error will occur. pub fn username(mut self, username: &str) -> Self { - self.0 - .insert("username".to_string(), Value::String(username.to_string())); + self.0.insert("username", Value::String(username.to_string())); self } |