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 | |
| 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.
| -rw-r--r-- | src/client/error.rs | 4 | ||||
| -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 | ||||
| -rw-r--r-- | tests/test_formatters.rs | 2 | ||||
| -rw-r--r-- | tests/test_user.rs | 22 |
6 files changed, 24 insertions, 35 deletions
diff --git a/src/client/error.rs b/src/client/error.rs index 225f2d3..a3df57c 100644 --- a/src/client/error.rs +++ b/src/client/error.rs @@ -22,10 +22,8 @@ use ::model::{ChannelType, Permissions}; /// let mut client = Client::login(&token); /// /// client.on_member_unban(|context, guild_id, user| { -/// let discriminator = user.discriminator.parse::<u16>().unwrap(); -/// /// // If the user has an even discriminator, don't re-ban them. -/// if discriminator % 2 == 0 { +/// if user.discriminator % 2 == 0 { /// return; /// } /// 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 diff --git a/tests/test_formatters.rs b/tests/test_formatters.rs index 3b52dac..fa36832 100644 --- a/tests/test_formatters.rs +++ b/tests/test_formatters.rs @@ -49,7 +49,7 @@ fn test_mention() { id: UserId(6), avatar: None, bot: false, - discriminator: "4132".to_owned(), + discriminator: 4132, name: "fake".to_owned(), }; let member = Member { diff --git a/tests/test_user.rs b/tests/test_user.rs index dd51f44..790243e 100644 --- a/tests/test_user.rs +++ b/tests/test_user.rs @@ -7,7 +7,7 @@ fn gen() -> User { id: UserId(210), avatar: Some("abc".to_owned()), bot: true, - discriminator: "1432".to_owned(), + discriminator: 1432, name: "test".to_owned(), } } @@ -31,14 +31,14 @@ fn test_core() { fn default_avatars() { let mut user = gen(); - user.discriminator = "0".to_owned(); - assert!(user.default_avatar_url().unwrap().ends_with("0.png")); - user.discriminator = "1".to_owned(); - assert!(user.default_avatar_url().unwrap().ends_with("1.png")); - user.discriminator = "2".to_owned(); - assert!(user.default_avatar_url().unwrap().ends_with("2.png")); - user.discriminator = "3".to_owned(); - assert!(user.default_avatar_url().unwrap().ends_with("3.png")); - user.discriminator = "4".to_owned(); - assert!(user.default_avatar_url().unwrap().ends_with("4.png")); + user.discriminator = 0; + assert!(user.default_avatar_url().ends_with("0.png")); + user.discriminator = 1; + assert!(user.default_avatar_url().ends_with("1.png")); + user.discriminator = 2; + assert!(user.default_avatar_url().ends_with("2.png")); + user.discriminator = 3; + assert!(user.default_avatar_url().ends_with("3.png")); + user.discriminator = 4; + assert!(user.default_avatar_url().ends_with("4.png")); } |