diff options
| author | Zeyla Hellyer <[email protected]> | 2017-05-02 22:01:37 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-05-02 22:01:37 -0700 |
| commit | 9a69b76bc0870f609a9dddca1e2583c524c4cf37 (patch) | |
| tree | e853cc96112f15629c3cfb321cf2cb5926d28d90 /src | |
| parent | Use constant for preset permissions (diff) | |
| download | serenity-9a69b76bc0870f609a9dddca1e2583c524c4cf37.tar.xz serenity-9a69b76bc0870f609a9dddca1e2583c524c4cf37.zip | |
Return preset from Member::find_guild if possible
If a value is present for `Member::guild_id` when calling
`Member::find_guild`, return that value instead of searching the cache.
This can potentially save a large amount of calculations and time.
Diffstat (limited to 'src')
| -rw-r--r-- | src/model/guild/member.rs | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index 630610f..5da7d7c 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -163,15 +163,24 @@ impl Member { /// Finds the Id of the [`Guild`] that the member is in. /// + /// Returns the value of [`Member::guild_id`] if present. Otherwise searches + /// the [`Cache`] for the Id. + /// /// # Errors /// /// Returns a [`ClientError::GuildNotFound`] if the guild could not be /// found. /// + /// [`Cache`]: ../ext/cache/struct.Cache.html /// [`ClientError::GuildNotFound`]: ../client/enum.ClientError.html#variant.GuildNotFound /// [`Guild`]: struct.Guild.html + /// [`Member::guild_id`]: struct.Member.html#structfield.guild_id #[cfg(feature="cache")] pub fn find_guild(&self) -> Result<GuildId> { + if let Some(guild_id) = self.guild_id { + return Ok(guild_id); + } + for guild in CACHE.read().unwrap().guilds.values() { let guild = guild.read().unwrap(); |