Skip to content

Commit

Permalink
Replace manual Hash/Eq implementation on User/Role (#2793)
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Mar 12, 2024
1 parent 6edcf11 commit 032431c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 35 deletions.
4 changes: 3 additions & 1 deletion src/model/colour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@
/// ```
///
/// [`Role`]: crate::model::guild::Role
#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[derive(
Clone, Copy, Debug, Default, Eq, Ord, Hash, PartialEq, PartialOrd, Deserialize, Serialize,
)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
pub struct Colour(pub u32);

Expand Down
2 changes: 1 addition & 1 deletion src/model/guild/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ impl fmt::Display for Member {
/// [link](https://discord.com/developers/docs/interactions/receiving-and-responding#message-interaction-object))
#[bool_to_bitflags::bool_to_bitflags]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[derive(Clone, Debug, Hash, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[non_exhaustive]
pub struct PartialMember {
/// Indicator of whether the member can hear in voice channels.
Expand Down
20 changes: 6 additions & 14 deletions src/model/guild/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::model::utils::is_false;
/// [Discord docs](https://discord.com/developers/docs/topics/permissions#role-object).
#[bool_to_bitflags::bool_to_bitflags]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[non_exhaustive]
pub struct Role {
/// The Id of the role. Can be used to calculate the role's creation date.
Expand Down Expand Up @@ -130,7 +130,11 @@ impl fmt::Display for Role {
}
}

impl Eq for Role {}
impl PartialOrd for Role {
fn partial_cmp(&self, other: &Role) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Role {
fn cmp(&self, other: &Role) -> Ordering {
Expand All @@ -142,18 +146,6 @@ impl Ord for Role {
}
}

impl PartialEq for Role {
fn eq(&self, other: &Role) -> bool {
self.id == other.id
}
}

impl PartialOrd for Role {
fn partial_cmp(&self, other: &Role) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl From<Role> for RoleId {
/// Gets the Id of a role.
fn from(role: Role) -> RoleId {
Expand Down
4 changes: 2 additions & 2 deletions src/model/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::utils;

/// Hides the implementation detail of ImageHash as an enum.
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Copy, PartialEq, Eq)]
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
enum ImageHashInner {
Normal { hash: [u8; 16], is_animated: bool },
Clyde,
Expand All @@ -37,7 +37,7 @@ enum ImageHashInner {
/// ```

#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Copy, PartialEq, Eq)]
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub struct ImageHash(ImageHashInner);

impl ImageHash {
Expand Down
18 changes: 1 addition & 17 deletions src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl OnlineStatus {
/// additional partial member field documented [here](https://discord.com/developers/docs/topics/gateway-events#message-create).
#[bool_to_bitflags::bool_to_bitflags]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, serde::Deserialize, serde::Serialize)]
#[non_exhaustive]
pub struct User {
/// The unique Id of the user. Can be used to calculate the account's creation date.
Expand Down Expand Up @@ -351,22 +351,6 @@ bitflags! {
}
}

use std::hash::{Hash, Hasher};

impl PartialEq for User {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}

impl Eq for User {}

impl Hash for User {
fn hash<H: Hasher>(&self, hasher: &mut H) {
self.id.hash(hasher);
}
}

#[cfg(feature = "model")]
impl User {
/// Returns the formatted URL of the user's icon, if one exists.
Expand Down

0 comments on commit 032431c

Please sign in to comment.