diff options
| author | Zeyla Hellyer <[email protected]> | 2017-04-25 15:48:13 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-04-25 15:48:13 -0700 |
| commit | 0f41ffc811827fdd45e4e631884909e33fa8769e (patch) | |
| tree | 40c6d5091097ee86ed72e9868f77350bdaed078a /src/model | |
| parent | Fix decoding for `CurrentUser.discriminator` (diff) | |
| download | serenity-0f41ffc811827fdd45e4e631884909e33fa8769e.tar.xz serenity-0f41ffc811827fdd45e4e631884909e33fa8769e.zip | |
Make `User.discriminator` a u16
Change the User struct's `discriminator` field to a u16 for performance.
The User struct's `discriminator` field was previously a u16 but changed
to a `String` for ease-of-use. Lately the library has been gearing more
towards performance where possible while not sacrificing ergonomics
_too much_ in most scenarios.
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/channel/message.rs | 3 | ||||
| -rw-r--r-- | src/model/guild/mod.rs | 9 | ||||
| -rw-r--r-- | src/model/user.rs | 19 |
3 files changed, 11 insertions, 20 deletions
diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs index 5dc59a1..13178ba 100644 --- a/src/model/channel/message.rs +++ b/src/model/channel/message.rs @@ -1,3 +1,4 @@ +use std::fmt::Write; use std::mem; use ::constants; use ::client::rest; @@ -199,7 +200,7 @@ impl Message { at_distinct.push('@'); at_distinct.push_str(&u.name); at_distinct.push('#'); - at_distinct.push_str(&u.discriminator); + let _ = write!(at_distinct, "{}", u.discriminator); result = result.replace(&u.mention(), &at_distinct); } diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index bd9a7ee..0daeec1 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -686,12 +686,13 @@ impl Guild { /// /// [`Member`]: struct.Member.html pub fn member_named(&self, name: &str) -> Option<&Member> { - let hash_pos = name.find('#'); - - let (name, discrim) = if let Some(pos) = hash_pos { + let (name, discrim) = if let Some(pos) = name.find('#') { let split = name.split_at(pos); - (split.0, Some(split.1)) + match split.1.parse::<u16>() { + Ok(discrim_int) => (split.0, Some(discrim_int)), + Err(_) => (name, None), + } } else { (&name[..], None) }; diff --git a/src/model/user.rs b/src/model/user.rs index 686d394..040effa 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -318,7 +318,8 @@ pub struct User { /// the same [`name`]. The name+discriminator pair is always unique. /// /// [`name`]: #structfield.name - pub discriminator: String, + #[serde(deserialize_with="deserialize_u16")] + pub discriminator: u16, /// The account's username. Changing username will trigger a discriminator /// change if the username+discriminator pair becomes non-unique. #[serde(rename="username")] @@ -366,20 +367,8 @@ impl User { /// Returns the formatted URL to the user's default avatar URL. /// /// This will produce a PNG URL. - /// - /// # Errors - /// - /// Returns an [`Error::Num`] if there was an error parsing the - /// discriminator. Theoretically this is not possible. - /// - /// Returns an [`Error::Other`] if the remainder of the calculation - /// `discriminator % 5` can not be matched. This is also probably not going - /// to occur. - /// - /// [`Error::Num`]: ../enum.Error.html#variant.Num - /// [`Error::Other`]: ../enum.Error.html#variant.Other - pub fn default_avatar_url(&self) -> Result<String> { - Ok(cdn!("/embed/avatars/{}.png", self.discriminator.parse::<u16>()? % 5u16).to_owned()) + pub fn default_avatar_url(&self) -> String { + cdn!("/embed/avatars/{}.png", self.discriminator % 5u16).to_owned() } /// Sends a message to a user through a direct message channel. This is a |