From 9a69b76bc0870f609a9dddca1e2583c524c4cf37 Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Tue, 2 May 2017 22:01:37 -0700 Subject: 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. --- src/model/guild/member.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/model/guild') 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 { + 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(); -- cgit v1.2.3