From a8acd6138741a6e5268141ac4ce902561931d353 Mon Sep 17 00:00:00 2001 From: Austin Hellyer Date: Fri, 13 Jan 2017 03:57:12 -0800 Subject: [PATCH] Convert Colour to be a tuple struct The struct only has one field (`value`) anyway. --- src/model/guild.rs | 2 +- src/utils/builder/create_embed.rs | 2 +- src/utils/builder/edit_role.rs | 2 +- src/utils/colour.rs | 37 +++++++++++++------------------ tests/test_colour.rs | 20 ++++++++--------- 5 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/model/guild.rs b/src/model/guild.rs index a7a7edce6c5..7b8ed606438 100644 --- a/src/model/guild.rs +++ b/src/model/guild.rs @@ -1069,7 +1069,7 @@ impl Member { .collect::>(); roles.sort_by(|a, b| b.cmp(a)); - roles.iter().find(|r| r.colour.value != default.value).map(|r| r.colour) + roles.iter().find(|r| r.colour.0 != default.0).map(|r| r.colour) } /// Calculates the member's display name. diff --git a/src/utils/builder/create_embed.rs b/src/utils/builder/create_embed.rs index d33db8a494d..86d5b11c4a5 100644 --- a/src/utils/builder/create_embed.rs +++ b/src/utils/builder/create_embed.rs @@ -63,7 +63,7 @@ impl CreateEmbed { /// Set the colour of the left-hand side of the embed. pub fn colour>(mut self, colour: C) -> Self { - self.0.insert("color".to_owned(), Value::U64(colour.into().value as u64)); + self.0.insert("color".to_owned(), Value::U64(colour.into().0 as u64)); CreateEmbed(self.0) } diff --git a/src/utils/builder/edit_role.rs b/src/utils/builder/edit_role.rs index b4be6d4c1fd..cea0c754358 100644 --- a/src/utils/builder/edit_role.rs +++ b/src/utils/builder/edit_role.rs @@ -37,7 +37,7 @@ impl EditRole { /// Creates a new builder with the values of the given [`Role`]. pub fn new(role: &Role) -> Self { EditRole(ObjectBuilder::new() - .insert("color", role.colour.value) + .insert("color", role.colour.0) .insert("hoist", role.hoist) .insert("managed", role.managed) .insert("mentionable", role.mentionable) diff --git a/src/utils/colour.rs b/src/utils/colour.rs index acf374f43e2..69f549de0ce 100644 --- a/src/utils/colour.rs +++ b/src/utils/colour.rs @@ -65,11 +65,7 @@ macro_rules! colour { /// [`dark_teal`]: #method.dark_teal /// [`get_g`]: #method.get_g #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] -pub struct Colour { - /// The raw inner 32-bit unsigned integer value of this Colour. This is - /// worked with to generate values such as the red component value. - pub value: u32, -} +pub struct Colour(pub u32); impl Colour { /// Generates a new Colour with the given integer value set. @@ -88,10 +84,9 @@ impl Colour { /// ``` /// /// [`get_tuple`]: #method.get_tuple + #[inline] pub fn new(value: u32) -> Colour { - Colour { - value: value, - } + Colour(value) } /// Generates a new Colour from an RGB value, creating an inner u32 @@ -104,8 +99,8 @@ impl Colour { /// ```rust /// use serenity::utils::Colour; /// - /// assert!(Colour::from_rgb(255, 0, 0).value == 0xFF0000); - /// assert!(Colour::from_rgb(217, 23, 211).value == 0xD917D3); + /// assert!(Colour::from_rgb(255, 0, 0).0 == 0xFF0000); + /// assert!(Colour::from_rgb(217, 23, 211).0 == 0xD917D3); /// ``` /// /// And you can then retrieve those same RGB values via its methods: @@ -125,14 +120,14 @@ impl Colour { uint = (uint << 8) | (g as u32); uint = (uint << 8) | (b as u32); - Colour::new(uint) + Colour(uint) } #[doc(hidden)] pub fn decode(value: Value) -> Result { match value { - Value::U64(v) => Ok(Colour::new(v as u32)), - Value::I64(v) => Ok(Colour::new(v as u32)), + Value::U64(v) => Ok(Colour(v as u32)), + Value::I64(v) => Ok(Colour(v as u32)), other => Err(Error::Decode("Expected valid colour", other)), } } @@ -147,7 +142,7 @@ impl Colour { /// assert_eq!(Colour::new(6573123).get_r(), 100); /// ``` pub fn get_r(&self) -> u8 { - ((self.value >> 16) & 255) as u8 + ((self.0 >> 16) & 255) as u8 } /// Returns the green RGB component of this Colour. @@ -160,7 +155,7 @@ impl Colour { /// assert_eq!(Colour::new(6573123).get_g(), 76); /// ``` pub fn get_g(&self) -> u8 { - ((self.value >> 8) & 255) as u8 + ((self.0 >> 8) & 255) as u8 } /// Returns the blue RGB component of this Colour. @@ -172,7 +167,7 @@ impl Colour { /// /// assert_eq!(Colour::new(6573123).get_b(), 67); pub fn get_b(&self) -> u8 { - (self.value & 255) as u8 + (self.0 & 255) as u8 } /// Returns a tuple of the red, green, and blue components of this Colour. @@ -211,7 +206,7 @@ impl From for Colour { /// assert_eq!(Colour::from(0xDEA584).get_tuple(), (222, 165, 132)); /// ``` fn from(value: i32) -> Colour { - Colour::new(value as u32) + Colour(value as u32) } } @@ -228,7 +223,7 @@ impl From for Colour { /// assert_eq!(Colour::from(6573123u32).get_r(), 100); /// ``` fn from(value: u32) -> Colour { - Colour::new(value) + Colour(value) } } @@ -245,7 +240,7 @@ impl From for Colour { /// assert_eq!(Colour::from(6573123u64).get_r(), 100); /// ``` fn from(value: u64) -> Colour { - Colour::new(value as u32) + Colour(value as u32) } } @@ -309,8 +304,6 @@ colour! { impl Default for Colour { /// Creates a default value for a `Colour`, setting the inner value to `0`. fn default() -> Colour { - Colour { - value: 0, - } + Colour(0) } } diff --git a/tests/test_colour.rs b/tests/test_colour.rs index fcf74632a01..110097a573f 100644 --- a/tests/test_colour.rs +++ b/tests/test_colour.rs @@ -5,16 +5,16 @@ use std::u32; #[test] fn new() { - assert_eq!(Colour::new(1).value, 1); - assert_eq!(Colour::new(u32::MIN).value, u32::MIN); - assert_eq!(Colour::new(u32::MAX).value, u32::MAX); + assert_eq!(Colour::new(1).0, 1); + assert_eq!(Colour::new(u32::MIN).0, u32::MIN); + assert_eq!(Colour::new(u32::MAX).0, u32::MAX); } #[test] fn from_rgb() { - assert_eq!(Colour::from_rgb(255, 0, 0).value, 0xFF0000); - assert_eq!(Colour::from_rgb(0, 255, 0).value, 0x00FF00); - assert_eq!(Colour::from_rgb(0, 0, 255).value, 0x0000FF); + assert_eq!(Colour::from_rgb(255, 0, 0).0, 0xFF0000); + assert_eq!(Colour::from_rgb(0, 255, 0).0, 0x00FF00); + assert_eq!(Colour::from_rgb(0, 0, 255).0, 0x0000FF); } #[test] @@ -39,12 +39,12 @@ fn get_tuple() { #[test] fn default() { - assert_eq!(Colour::default().value, 0); + assert_eq!(Colour::default().0, 0); } #[test] fn from() { - assert_eq!(Colour::from(7i32).value, 7); - assert_eq!(Colour::from(7u32).value, 7); - assert_eq!(Colour::from(7u64).value, 7); + assert_eq!(Colour::from(7i32).0, 7); + assert_eq!(Colour::from(7u32).0, 7); + assert_eq!(Colour::from(7u64).0, 7); }