diff options
| author | Austin Hellyer <[email protected]> | 2016-12-03 12:46:09 -0800 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2016-12-03 12:46:09 -0800 |
| commit | 143337ae717773f59562d67f85d0e9c44063a45b (patch) | |
| tree | 26553ee5a0caef0dbb8f8edb0effeec942ad92e1 /src/model | |
| parent | Fix Shard::set_presence example (diff) | |
| download | serenity-143337ae717773f59562d67f85d0e9c44063a45b.tar.xz serenity-143337ae717773f59562d67f85d0e9c44063a45b.zip | |
Add Role Eq/Ord impls
Roles can now be compared directly. This is a heirarchy check.
This checks the following between two roles, A and B:
- A's position is greater than B;
- if not, then A's Id is greater than B;
if these two conditions are false, then B is greater than A. If the two
conditions are both true, then they are of equivilance.
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/guild.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/model/guild.rs b/src/model/guild.rs index 7a3410f..6c5fab2 100644 --- a/src/model/guild.rs +++ b/src/model/guild.rs @@ -1,3 +1,4 @@ +use std::cmp::Ordering; use std::collections::HashMap; use std::fmt; use super::utils::{ @@ -1099,3 +1100,35 @@ impl fmt::Display for Role { fmt::Display::fmt(&self.mention(), f) } } + +impl Eq for Role {} + +impl Ord for Role { + fn cmp(&self, other: &Role) -> Ordering { + if self.position > other.position { + Ordering::Greater + } else if self.position == other.position { + if self.id > other.id { + Ordering::Greater + } else if self.id == other.id { + Ordering::Equal + } else { + Ordering::Less + } + } else { + Ordering::Less + } + } +} + +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)) + } +} |